2022-12-13 15:42:09 +03:00
|
|
|
import './index.css';
|
|
|
|
|
|
|
|
import './app.css';
|
|
|
|
|
2022-12-15 06:43:24 +03:00
|
|
|
import { login } from 'masto';
|
2022-12-13 15:42:09 +03:00
|
|
|
import { render } from 'preact';
|
|
|
|
import { useEffect, useState } from 'preact/hooks';
|
|
|
|
|
|
|
|
import Compose from './components/compose';
|
2023-01-11 08:28:42 +03:00
|
|
|
import { getCurrentAccount } from './utils/store-utils';
|
2022-12-15 12:11:15 +03:00
|
|
|
import useTitle from './utils/useTitle';
|
2022-12-13 15:42:09 +03:00
|
|
|
|
2022-12-13 16:54:16 +03:00
|
|
|
if (window.opener) {
|
|
|
|
console = window.opener.console;
|
|
|
|
}
|
|
|
|
|
2022-12-15 06:43:24 +03:00
|
|
|
(async () => {
|
|
|
|
if (window.masto) return;
|
|
|
|
console.warn('window.masto not found. Trying to log in...');
|
|
|
|
try {
|
2023-01-11 08:28:42 +03:00
|
|
|
const { instanceURL, accessToken } = getCurrentAccount();
|
2022-12-15 06:43:24 +03:00
|
|
|
window.masto = await login({
|
|
|
|
url: `https://${instanceURL}`,
|
|
|
|
accessToken,
|
2022-12-15 11:54:14 +03:00
|
|
|
disableVersionCheck: true,
|
2022-12-20 08:29:38 +03:00
|
|
|
timeout: 30_000,
|
2022-12-15 06:43:24 +03:00
|
|
|
});
|
|
|
|
console.info('Logged in successfully.');
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
alert('Failed to log in. Please try again.');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2022-12-13 15:42:09 +03:00
|
|
|
function App() {
|
|
|
|
const [uiState, setUIState] = useState('default');
|
|
|
|
|
|
|
|
const { editStatus, replyToStatus, draftStatus } = window.__COMPOSE__ || {};
|
|
|
|
|
2022-12-15 12:11:15 +03:00
|
|
|
useTitle(
|
|
|
|
editStatus
|
|
|
|
? 'Editing source status'
|
|
|
|
: replyToStatus
|
|
|
|
? `Replying to @${
|
|
|
|
replyToStatus.account?.acct || replyToStatus.account?.username
|
|
|
|
}`
|
|
|
|
: 'Compose',
|
|
|
|
);
|
2022-12-13 15:42:09 +03:00
|
|
|
|
2022-12-16 08:54:17 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (uiState === 'closed') {
|
|
|
|
try {
|
|
|
|
// Focus parent window
|
|
|
|
window.opener.focus();
|
|
|
|
} catch (e) {}
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
}, [uiState]);
|
|
|
|
|
2022-12-13 15:42:09 +03:00
|
|
|
if (uiState === 'closed') {
|
|
|
|
return (
|
2022-12-14 16:48:17 +03:00
|
|
|
<div class="box">
|
2022-12-13 15:42:09 +03:00
|
|
|
<p>You may close this page now.</p>
|
|
|
|
<p>
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
window.close();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Close window
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Compose
|
|
|
|
editStatus={editStatus}
|
|
|
|
replyToStatus={replyToStatus}
|
|
|
|
draftStatus={draftStatus}
|
|
|
|
standalone
|
2022-12-14 16:48:17 +03:00
|
|
|
hasOpener={window.opener}
|
2022-12-13 16:54:16 +03:00
|
|
|
onClose={(results) => {
|
|
|
|
const { newStatus, fn = () => {} } = results || {};
|
2022-12-13 15:42:09 +03:00
|
|
|
try {
|
2022-12-13 16:54:16 +03:00
|
|
|
if (newStatus) {
|
|
|
|
window.opener.__STATES__.reloadStatusPage++;
|
|
|
|
}
|
2022-12-13 15:42:09 +03:00
|
|
|
fn();
|
|
|
|
setUIState('closed');
|
|
|
|
} catch (e) {}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:48:17 +03:00
|
|
|
render(<App />, document.getElementById('app-standalone'));
|