this repo has no description
1import './index.css';
2import './app.css';
3import './polyfills';
4
5import { i18n } from '@lingui/core';
6import { I18nProvider } from '@lingui/react';
7import { Trans, useLingui } from '@lingui/react/macro';
8import { render } from 'preact';
9import { useEffect, useState } from 'preact/hooks';
10
11import ComposeSuspense from './components/compose-suspense';
12import Loader from './components/loader';
13import { initActivateLang } from './utils/lang';
14import { initStates } from './utils/states';
15import { getCurrentAccount } from './utils/store-utils';
16import useTitle from './utils/useTitle';
17
18initActivateLang();
19
20if (window.opener) {
21 console = window.opener.console;
22}
23
24function App() {
25 const { t } = useLingui();
26 const [uiState, setUIState] = useState('default');
27 const [isLoggedIn, setIsLoggedIn] = useState(null);
28
29 const { editStatus, replyToStatus, draftStatus } = window.__COMPOSE__ || {};
30
31 useTitle(
32 editStatus
33 ? t`Editing source status`
34 : replyToStatus
35 ? t`Replying to @${
36 replyToStatus.account?.acct || replyToStatus.account?.username
37 }`
38 : t`Compose`,
39 );
40
41 useEffect(() => {
42 const account = getCurrentAccount();
43 setIsLoggedIn(!!account);
44 if (account) {
45 initStates();
46 }
47 }, []);
48
49 useEffect(() => {
50 if (uiState === 'closed') {
51 try {
52 // Focus parent window
53 window.opener.focus();
54 } catch (e) {}
55 window.close();
56 }
57 }, [uiState]);
58
59 if (uiState === 'closed') {
60 return (
61 <div class="box">
62 <p>
63 <Trans>You may close this page now.</Trans>
64 </p>
65 <p>
66 <button
67 onClick={() => {
68 window.close();
69 }}
70 >
71 <Trans>Close window</Trans>
72 </button>
73 </p>
74 </div>
75 );
76 }
77
78 console.debug('OPEN COMPOSE');
79
80 if (isLoggedIn === false) {
81 return (
82 <div class="box">
83 <h1>
84 <Trans>Error</Trans>
85 </h1>
86 <p>
87 <Trans>Login required.</Trans>
88 </p>
89 <p>
90 <a href="/">
91 <Trans>Go home</Trans>
92 </a>
93 </p>
94 </div>
95 );
96 }
97
98 if (isLoggedIn) {
99 return (
100 <ComposeSuspense
101 editStatus={editStatus}
102 replyToStatus={replyToStatus}
103 draftStatus={draftStatus}
104 standalone
105 hasOpener={window.opener}
106 onClose={(results) => {
107 const { newStatus, fn = () => {} } = results || {};
108 try {
109 if (newStatus) {
110 window.opener.__STATES__.reloadStatusPage++;
111 }
112 fn();
113 setUIState('closed');
114 } catch (e) {}
115 }}
116 />
117 );
118 }
119
120 return (
121 <div class="box">
122 <Loader />
123 </div>
124 );
125}
126
127render(
128 <I18nProvider i18n={i18n}>
129 <App />
130 </I18nProvider>,
131 document.getElementById('app-standalone'),
132);