forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

gmail-compose-box.tsx256 lines, 9.8 KiBHistoryRawDownload
1import { createSignal, createEffect, Show } from "solid-js";
2import { Button } from "@/components/ui/button";
3import { Input } from "@/components/ui/input";
4import { RichTextEditor } from "@/components/ui/rich-text-editor";
5import { UserSearchSelect } from "@/components/users/user-search-select";
6import { ConfirmDialog } from "@/components/ui/confirm-dialog";
7import { IconAlert, IconSend, IconTrash, IconX } from "@/components/ui/icons";
8import { postMessage } from "@/api/messages";
9import { formatApiError } from "@/api/client";
10import { useAuth } from "@/stores/auth-context";
11import { useT } from "@/stores/preferences-context";
12import { suppressQuickActions } from "@/stores/quick-actions";
13
14interface GmailComposeBoxProps {
15 open: boolean;
16 onClose: () => void;
17 onSuccess: () => void;
18}
19
20export function GmailComposeBox(props: GmailComposeBoxProps) {
21 const t = useT();
22 const auth = useAuth();
23 const [recipient, setRecipient] = createSignal("");
24 const [subject, setSubject] = createSignal("");
25 const [body, setBody] = createSignal("");
26 const [label, setLabel] = createSignal("");
27 const [isMinimized, setIsMinimized] = createSignal(false);
28 const [pending, setPending] = createSignal(false);
29 const [error, setError] = createSignal("");
30
31 // The sheet docks at the bottom of a phone; the quick-action button would
32 // float over its editor.
33 createEffect(() => {
34 if (props.open) suppressQuickActions();
35 });
36
37 createEffect(() => {
38 if (props.open) {
39 setRecipient("");
40 setSubject("");
41 setBody("");
42 setLabel("");
43 setIsMinimized(false);
44 setError("");
45 }
46 });
47
48 const reset = () => {
49 setRecipient("");
50 setSubject("");
51 setBody("");
52 setLabel("");
53 setError("");
54 };
55
56 const handleClose = () => {
57 reset();
58 props.onClose();
59 };
60
61 // Closing throws the draft away, so anything typed is confirmed first.
62 const [confirmDiscard, setConfirmDiscard] = createSignal(false);
63 const hasDraft = () =>
64 !!recipient() ||
65 subject().trim() !== "" ||
66 label().trim() !== "" ||
67 body().replace(/<[^>]*>|&nbsp;/g, "").trim() !== "";
68 const requestClose = () => {
69 if (hasDraft()) setConfirmDiscard(true);
70 else handleClose();
71 };
72
73 // The backend needs a recipient and a subject (the body is optional), so
74 // "Gönder" stays off until both are there and says which one is missing.
75 const sendBlocker = () => {
76 if (!recipient()) return t("messages.sendHintRecipient");
77 if (!subject().trim()) return t("messages.sendHintSubject");
78 return "";
79 };
80
81 const handleSend = async (e: SubmitEvent) => {
82 e.preventDefault();
83 if (sendBlocker()) {
84 setError(sendBlocker());
85 return;
86 }
87 setError("");
88 setPending(true);
89
90 try {
91 await postMessage({
92 recipient_id: recipient(),
93 subject: subject().trim(),
94 body: body().trim() || undefined,
95 label: label().trim() || undefined,
96 });
97 props.onSuccess();
98 handleClose();
99 } catch (err) {
100 setError(formatApiError(err));
101 } finally {
102 setPending(false);
103 }
104 };
105
106 return (
107 <Show when={props.open}>
108 <div
109 role="dialog"
110 aria-label={t("messages.newMessage")}
111 class="fixed inset-x-0 bottom-0 z-50 w-full rounded-t-2xl border border-border-line bg-surface-base pb-[max(env(safe-area-inset-bottom),var(--android-nav-inset,0px))] shadow-2xl shadow-black/20 transition-all duration-200 sm:inset-x-auto sm:right-6 sm:w-[560px] sm:rounded-t-xl sm:pb-0"
112 style={{ "max-height": isMinimized() ? "44px" : "640px" }}
113 >
114 {/* Gmail Header */}
115 <div class="flex h-11 items-center justify-between rounded-t-2xl border-b sm:h-9 sm:rounded-t-xl border-border-hairline bg-surface-overlay px-4">
116 <div class="flex min-w-0 flex-1 items-center gap-2">
117 <span class="h-2.5 w-2.5 rounded-full bg-primary" />
118 <h3 class="min-w-0 truncate text-xs font-bold text-foreground">
119 {subject() ? subject() : t("messages.newMessage")}
120 </h3>
121 </div>
122 <div class="flex items-center gap-1">
123 <button
124 type="button"
125 class="flex h-10 w-10 items-center justify-center rounded-lg text-muted-foreground outline-hidden hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring sm:h-7 sm:w-7"
126 onClick={() => setIsMinimized(!isMinimized())}
127 title={isMinimized() ? t("messages.expand") : t("messages.minimize")}
128 aria-label={isMinimized() ? t("messages.expand") : t("messages.minimize")}
129 aria-expanded={!isMinimized()}
130 >
131 <span class="text-xs font-bold" aria-hidden="true">{isMinimized() ? "□" : "—"}</span>
132 </button>
133 <button
134 type="button"
135 class="flex h-10 w-10 items-center justify-center rounded-lg text-muted-foreground outline-hidden hover:bg-destructive/10 hover:text-destructive-text focus-visible:ring-2 focus-visible:ring-ring sm:h-7 sm:w-7"
136 onClick={requestClose}
137 title={t("common.cancel")}
138 aria-label={t("common.cancel")}
139 >
140 <IconX class="h-4 w-4" />
141 </button>
142 </div>
143 </div>
144
145 {/* Gmail Form Content */}
146 <Show when={!isMinimized()}>
147 <form class="flex h-[min(520px,60vh)] flex-col sm:h-[520px]" onSubmit={handleSend}>
148 {/* Recipient Line */}
149 <div class="flex items-center border-b border-border-hairline px-3 py-1.5 gap-2 text-xs">
150 <span class="w-12 font-medium text-muted-foreground shrink-0">
151 {t("messages.recipient")}:
152 </span>
153 <div class="flex-1 min-w-0">
154 <UserSearchSelect
155 id="gmail-compose-recipient"
156 value={recipient()}
157 onChange={setRecipient}
158 placeholder={t("messages.recipientPlaceholder")}
159 excludeIds={auth.user()?.id ? [auth.user()!.id] : []}
160 />
161 </div>
162 </div>
163
164 {/* Subject Line */}
165 <div class="flex items-center border-b border-border-hairline px-3 py-1 gap-2 text-xs">
166 <span class="w-12 font-medium text-muted-foreground shrink-0">
167 {t("messages.subject")}:
168 </span>
169 <Input
170 required
171 maxlength={200}
172 aria-label={t("messages.subject")}
173 placeholder={t("messages.subjectPlaceholder")}
174 class="h-8 border-none bg-transparent shadow-none focus-visible:ring-0 text-xs flex-1"
175 value={subject()}
176 onInput={(e) => setSubject(e.currentTarget.value)}
177 />
178 </div>
179
180 {/* Optional Label Tag Line */}
181 <div class="flex items-center border-b border-border-hairline px-3 py-1 gap-2 text-xs">
182 <span class="w-12 font-medium text-muted-foreground shrink-0">
183 {t("messages.label")}:
184 </span>
185 <Input
186 maxlength={50}
187 aria-label={t("messages.label")}
188 placeholder={t("messages.labelsPlaceholder")}
189 class="h-8 border-none bg-transparent shadow-none focus-visible:ring-0 text-xs flex-1"
190 value={label()}
191 onInput={(e) => setLabel(e.currentTarget.value)}
192 />
193 </div>
194
195 {/* Gmail Rich Text Editor Area */}
196 <div class="flex-1 p-2.5 flex flex-col min-h-0">
197 <RichTextEditor
198 value={body()}
199 onChange={setBody}
200 placeholder={t("messages.bodyPlaceholder")}
201 class="flex-1 min-h-0"
202 />
203 </div>
204
205 <Show when={error()}>
206 <p class="px-3 text-xs font-medium text-destructive-text">{error()}</p>
207 </Show>
208
209 {/* Gmail Bottom Action Toolbar */}
210 <div class="flex items-center justify-between gap-3 border-t border-border-hairline bg-surface-tint px-3 py-2.5">
211 <div class="flex min-w-0 items-center gap-3">
212 <Button
213 type="submit"
214 size="sm"
215 class="h-9 shrink-0 rounded-lg px-5 font-semibold"
216 disabled={pending() || !!sendBlocker()}
217 aria-describedby={sendBlocker() ? "gmail-compose-send-hint" : undefined}
218 >
219 <IconSend class="h-4 w-4" />
220 {t("messages.send")}
221 </Button>
222 <Show when={sendBlocker() && !error()}>
223 <p id="gmail-compose-send-hint" class="min-w-0 text-[11px] leading-snug text-muted-foreground">
224 {sendBlocker()}
225 </p>
226 </Show>
227 </div>
228
229 <button
230 type="button"
231 class="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg text-muted-foreground outline-hidden transition-colors hover:bg-destructive/10 hover:text-destructive-text focus-visible:ring-2 focus-visible:ring-ring sm:h-8 sm:w-8"
232 onClick={requestClose}
233 title={t("messages.deleteDraft")}
234 aria-label={t("messages.deleteDraft")}
235 >
236 <IconTrash class="h-4 w-4" />
237 </button>
238 </div>
239 </form>
240 </Show>
241 </div>
242 <ConfirmDialog
243 open={confirmDiscard()}
244 onOpenChange={setConfirmDiscard}
245 title={t("messages.discardDraftTitle")}
246 summary={subject().trim() || t("messages.newMessage")}
247 description={t("messages.discardDraftHint")}
248 confirmLabel={t("sidePanel.discard")}
249 cancelLabel={t("sidePanel.keepEditing")}
250 variant="destructive"
251 icon={<IconAlert class="h-4 w-4" />}
252 onConfirm={handleClose}
253 />
254 </Show>
255 );
256}