phanpy/src/compose.jsx

132 lines
2.8 KiB
React
Raw Normal View History

import './index.css';
import './app.css';
2024-06-14 02:34:50 +02:00
import './polyfills';
2024-08-13 09:26:23 +02:00
import { i18n } from '@lingui/core';
import { t, Trans } from '@lingui/macro';
import { I18nProvider } from '@lingui/react';
import { render } from 'preact';
import { useEffect, useState } from 'preact/hooks';
import ComposeSuspense from './components/compose-suspense';
2024-08-24 12:15:24 +02:00
import Loader from './components/loader';
2024-08-13 09:26:23 +02:00
import { initActivateLang } from './utils/lang';
import { initStates } from './utils/states';
2024-08-24 12:15:24 +02:00
import { getCurrentAccount, setCurrentAccountID } from './utils/store-utils';
import useTitle from './utils/useTitle';
2024-08-13 09:26:23 +02:00
initActivateLang();
if (window.opener) {
console = window.opener.console;
}
function App() {
const [uiState, setUIState] = useState('default');
2024-08-24 12:15:24 +02:00
const [isLoggedIn, setIsLoggedIn] = useState(null);
const { editStatus, replyToStatus, draftStatus } = window.__COMPOSE__ || {};
useTitle(
editStatus
2024-08-13 09:26:23 +02:00
? t`Editing source status`
: replyToStatus
2024-08-13 09:26:23 +02:00
? t`Replying to @${
replyToStatus.account?.acct || replyToStatus.account?.username
}`
2024-08-13 09:26:23 +02:00
: t`Compose`,
);
useEffect(() => {
2024-08-24 12:15:24 +02:00
const account = getCurrentAccount();
setIsLoggedIn(!!account);
if (account) {
initStates();
}
}, []);
useEffect(() => {
if (uiState === 'closed') {
try {
// Focus parent window
window.opener.focus();
} catch (e) {}
window.close();
}
}, [uiState]);
if (uiState === 'closed') {
return (
2022-12-14 14:48:17 +01:00
<div class="box">
2024-08-13 09:26:23 +02:00
<p>
<Trans>You may close this page now.</Trans>
</p>
<p>
<button
onClick={() => {
window.close();
}}
>
2024-08-13 09:26:23 +02:00
<Trans>Close window</Trans>
</button>
</p>
</div>
);
}
2023-01-11 10:07:47 +01:00
console.debug('OPEN COMPOSE');
2024-08-24 12:15:24 +02:00
if (isLoggedIn === false) {
return (
<div class="box">
<h1>
<Trans>Error</Trans>
</h1>
<p>
<Trans>Login required.</Trans>
</p>
<p>
<a href="/">
<Trans>Go home</Trans>
</a>
</p>
</div>
);
}
if (isLoggedIn) {
return (
<ComposeSuspense
editStatus={editStatus}
replyToStatus={replyToStatus}
draftStatus={draftStatus}
standalone
hasOpener={window.opener}
onClose={(results) => {
const { newStatus, fn = () => {} } = results || {};
try {
if (newStatus) {
window.opener.__STATES__.reloadStatusPage++;
}
fn();
setUIState('closed');
} catch (e) {}
}}
/>
);
}
return (
2024-08-24 12:15:24 +02:00
<div class="box">
<Loader />
</div>
);
}
2024-08-13 09:26:23 +02:00
render(
<I18nProvider i18n={i18n}>
<App />
</I18nProvider>,
document.getElementById('app-standalone'),
);