this repo has no description
1import { useLingui } from '@lingui/react/macro';
2import { useEffect } from 'preact/hooks';
3import { useLocation, useNavigate } from 'react-router-dom';
4import { subscribe, useSnapshot } from 'valtio';
5
6import Accounts from '../pages/accounts';
7import Settings from '../pages/settings';
8import focusDeck from '../utils/focus-deck';
9import showToast from '../utils/show-toast';
10import states from '../utils/states';
11
12import AccountSheet from './account-sheet';
13import ComposeSuspense, { preload } from './compose-suspense';
14import Drafts from './drafts';
15import EmbedModal from './embed-modal';
16import GenericAccounts from './generic-accounts';
17import MediaAltModal from './media-alt-modal';
18import MediaModal from './media-modal';
19import Modal from './modal';
20import ReportModal from './report-modal';
21import ShortcutsSettings from './shortcuts-settings';
22
23subscribe(states, (changes) => {
24 for (const [action, path, value, prevValue] of changes) {
25 // When closing modal, focus on deck
26 if (/^show/i.test(path) && !value) {
27 focusDeck();
28 }
29 }
30});
31
32export default function Modals() {
33 const { t } = useLingui();
34 const snapStates = useSnapshot(states);
35 const navigate = useNavigate();
36 const location = useLocation();
37
38 useEffect(() => {
39 setTimeout(preload, 1000);
40 }, []);
41
42 return (
43 <>
44 {!!snapStates.showCompose && (
45 <Modal
46 class={`solid ${snapStates.composerState.minimized ? 'min' : ''}`}
47 minimized={!!snapStates.composerState.minimized}
48 >
49 <ComposeSuspense
50 replyToStatus={
51 typeof snapStates.showCompose !== 'boolean'
52 ? snapStates.showCompose.replyToStatus
53 : window.__COMPOSE__?.replyToStatus || null
54 }
55 editStatus={
56 states.showCompose?.editStatus ||
57 window.__COMPOSE__?.editStatus ||
58 null
59 }
60 draftStatus={
61 states.showCompose?.draftStatus ||
62 window.__COMPOSE__?.draftStatus ||
63 null
64 }
65 onClose={(results) => {
66 const { newStatus, instance, type, scheduledAt } = results || {};
67 states.showCompose = false;
68 window.__COMPOSE__ = null;
69 if (newStatus) {
70 states.reloadStatusPage++;
71 if (scheduledAt) states.reloadScheduledPosts++;
72 showToast({
73 text: {
74 post: scheduledAt
75 ? t`Post scheduled`
76 : t`Post published. Check it out.`,
77 reply: scheduledAt
78 ? t`Reply scheduled`
79 : t`Reply posted. Check it out.`,
80 edit: t`Post updated. Check it out.`,
81 }[type || 'post'],
82 delay: 1000,
83 duration: 10_000, // 10 seconds
84 onClick: (toast) => {
85 toast.hideToast();
86 states.prevLocation = location;
87 if (scheduledAt) {
88 navigate('/sp');
89 } else {
90 navigate(
91 instance
92 ? `/${instance}/s/${newStatus.id}`
93 : `/s/${newStatus.id}`,
94 );
95 }
96 },
97 });
98 }
99 }}
100 />
101 </Modal>
102 )}
103 {!!snapStates.showSettings && (
104 <Modal
105 onClose={() => {
106 states.showSettings = false;
107 }}
108 >
109 <Settings
110 onClose={() => {
111 states.showSettings = false;
112 }}
113 />
114 </Modal>
115 )}
116 {!!snapStates.showAccounts && (
117 <Modal
118 onClose={() => {
119 states.showAccounts = false;
120 }}
121 >
122 <Accounts
123 onClose={() => {
124 states.showAccounts = false;
125 }}
126 />
127 </Modal>
128 )}
129 {!!snapStates.showAccount && (
130 <Modal
131 onClose={() => {
132 states.showAccount = false;
133 }}
134 >
135 <AccountSheet
136 account={snapStates.showAccount?.account || snapStates.showAccount}
137 instance={snapStates.showAccount?.instance}
138 onClose={({ destination } = {}) => {
139 states.showAccount = false;
140 // states.showGenericAccounts = false;
141 // if (destination) {
142 // states.showAccounts = false;
143 // }
144 }}
145 />
146 </Modal>
147 )}
148 {!!snapStates.showDrafts && (
149 <Modal
150 onClose={() => {
151 states.showDrafts = false;
152 }}
153 >
154 <Drafts onClose={() => (states.showDrafts = false)} />
155 </Modal>
156 )}
157 {!!snapStates.showMediaModal && (
158 <Modal
159 onClick={(e) => {
160 if (
161 e.target === e.currentTarget ||
162 e.target.classList.contains('media')
163 ) {
164 states.showMediaModal = false;
165 }
166 }}
167 >
168 <MediaModal
169 mediaAttachments={snapStates.showMediaModal.mediaAttachments}
170 instance={snapStates.showMediaModal.instance}
171 index={snapStates.showMediaModal.mediaIndex}
172 statusID={snapStates.showMediaModal.statusID}
173 onClose={() => {
174 states.showMediaModal = false;
175 }}
176 />
177 </Modal>
178 )}
179 {!!snapStates.showShortcutsSettings && (
180 <Modal
181 onClose={() => {
182 states.showShortcutsSettings = false;
183 }}
184 >
185 <ShortcutsSettings
186 onClose={() => (states.showShortcutsSettings = false)}
187 />
188 </Modal>
189 )}
190 {!!snapStates.showGenericAccounts && (
191 <Modal
192 onClose={() => {
193 states.showGenericAccounts = false;
194 }}
195 >
196 <GenericAccounts
197 instance={snapStates.showGenericAccounts.instance}
198 excludeRelationshipAttrs={
199 snapStates.showGenericAccounts.excludeRelationshipAttrs
200 }
201 postID={snapStates.showGenericAccounts.postID}
202 onClose={() => (states.showGenericAccounts = false)}
203 blankCopy={snapStates.showGenericAccounts.blankCopy}
204 />
205 </Modal>
206 )}
207 {!!snapStates.showMediaAlt && (
208 <Modal
209 onClose={(e) => {
210 states.showMediaAlt = false;
211 }}
212 >
213 <MediaAltModal
214 alt={snapStates.showMediaAlt.alt || snapStates.showMediaAlt}
215 lang={snapStates.showMediaAlt?.lang}
216 onClose={() => {
217 states.showMediaAlt = false;
218 }}
219 />
220 </Modal>
221 )}
222 {!!snapStates.showEmbedModal && (
223 <Modal
224 class="solid"
225 onClose={() => {
226 states.showEmbedModal = false;
227 }}
228 >
229 <EmbedModal
230 html={snapStates.showEmbedModal.html}
231 url={snapStates.showEmbedModal.url}
232 width={snapStates.showEmbedModal.width}
233 height={snapStates.showEmbedModal.height}
234 onClose={() => {
235 states.showEmbedModal = false;
236 }}
237 />
238 </Modal>
239 )}
240 {!!snapStates.showReportModal && (
241 <Modal
242 onClose={() => {
243 states.showReportModal = false;
244 }}
245 >
246 <ReportModal
247 account={snapStates.showReportModal.account}
248 post={snapStates.showReportModal.post}
249 onClose={() => {
250 states.showReportModal = false;
251 }}
252 />
253 </Modal>
254 )}
255 </>
256 );
257}