forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

gmail-compose-box.tsx200 lines, 7.2 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 { IconSend, IconTrash, IconX } from "@/components/ui/icons";
7import { postMessage } from "@/api/messages";
8import { formatApiError } from "@/api/client";
9import { useAuth } from "@/stores/auth-context";
10import { useT } from "@/stores/preferences-context";
11
12interface GmailComposeBoxProps {
13 open: boolean;
14 onClose: () => void;
15 onSuccess: () => void;
16}
17
18export function GmailComposeBox(props: GmailComposeBoxProps) {
19 const t = useT();
20 const auth = useAuth();
21 const [recipient, setRecipient] = createSignal("");
22 const [subject, setSubject] = createSignal("");
23 const [body, setBody] = createSignal("");
24 const [label, setLabel] = createSignal("");
25 const [isMinimized, setIsMinimized] = createSignal(false);
26 const [pending, setPending] = createSignal(false);
27 const [error, setError] = createSignal("");
28
29 createEffect(() => {
30 if (props.open) {
31 setRecipient("");
32 setSubject("");
33 setBody("");
34 setLabel("");
35 setIsMinimized(false);
36 setError("");
37 }
38 });
39
40 const reset = () => {
41 setRecipient("");
42 setSubject("");
43 setBody("");
44 setLabel("");
45 setError("");
46 };
47
48 const handleClose = () => {
49 reset();
50 props.onClose();
51 };
52
53 const handleSend = async (e: SubmitEvent) => {
54 e.preventDefault();
55 if (!recipient()) {
56 setError(t("messages.selectRecipient"));
57 return;
58 }
59 setError("");
60 setPending(true);
61
62 try {
63 await postMessage({
64 recipient_id: recipient(),
65 subject: subject().trim(),
66 body: body().trim() || undefined,
67 label: label().trim() || undefined,
68 });
69 props.onSuccess();
70 handleClose();
71 } catch (err) {
72 setError(formatApiError(err));
73 } finally {
74 setPending(false);
75 }
76 };
77
78 return (
79 <Show when={props.open}>
80 <div
81 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"
82 style={{ "max-height": isMinimized() ? "44px" : "640px" }}
83 >
84 {/* Gmail Header */}
85 <div class="flex h-9 items-center justify-between rounded-t-xl border-b border-border-hairline bg-surface-overlay px-4">
86 <div class="flex min-w-0 flex-1 items-center gap-2">
87 <span class="h-2.5 w-2.5 rounded-full bg-primary" />
88 <h3 class="min-w-0 truncate text-xs font-bold text-foreground">
89 {subject() ? subject() : t("messages.newMessage")}
90 </h3>
91 </div>
92 <div class="flex items-center gap-1">
93 <button
94 type="button"
95 class="flex h-7 w-7 items-center justify-center rounded-lg text-muted-foreground hover:bg-muted hover:text-foreground"
96 onClick={() => setIsMinimized(!isMinimized())}
97 title={isMinimized() ? t("messages.expand") : t("messages.minimize")}
98 >
99 <span class="text-xs font-bold">{isMinimized() ? "□" : "—"}</span>
100 </button>
101 <button
102 type="button"
103 class="flex h-7 w-7 items-center justify-center rounded-lg text-muted-foreground hover:bg-destructive/10 hover:text-destructive-text"
104 onClick={handleClose}
105 title={t("common.cancel")}
106 >
107 <IconX class="h-4 w-4" />
108 </button>
109 </div>
110 </div>
111
112 {/* Gmail Form Content */}
113 <Show when={!isMinimized()}>
114 <form class="flex h-[min(520px,60vh)] flex-col sm:h-[520px]" onSubmit={handleSend}>
115 {/* Recipient Line */}
116 <div class="flex items-center border-b border-border-hairline px-3 py-1.5 gap-2 text-xs">
117 <span class="w-12 font-medium text-muted-foreground shrink-0">
118 {t("messages.recipient")}:
119 </span>
120 <div class="flex-1 min-w-0">
121 <UserSearchSelect
122 id="gmail-compose-recipient"
123 value={recipient()}
124 onChange={setRecipient}
125 placeholder={t("messages.recipientPlaceholder")}
126 excludeIds={auth.user()?.id ? [auth.user()!.id] : []}
127 />
128 </div>
129 </div>
130
131 {/* Subject Line */}
132 <div class="flex items-center border-b border-border-hairline px-3 py-1 gap-2 text-xs">
133 <span class="w-12 font-medium text-muted-foreground shrink-0">
134 {t("form.title")}:
135 </span>
136 <Input
137 required
138 maxlength={200}
139 placeholder="Konu"
140 class="h-8 border-none bg-transparent shadow-none focus-visible:ring-0 text-xs flex-1"
141 value={subject()}
142 onInput={(e) => setSubject(e.currentTarget.value)}
143 />
144 </div>
145
146 {/* Optional Label Tag Line */}
147 <div class="flex items-center border-b border-border-hairline px-3 py-1 gap-2 text-xs">
148 <span class="w-12 font-medium text-muted-foreground shrink-0">
149 Etiket:
150 </span>
151 <Input
152 maxlength={50}
153 placeholder={t("messages.labelsPlaceholder")}
154 class="h-8 border-none bg-transparent shadow-none focus-visible:ring-0 text-xs flex-1"
155 value={label()}
156 onInput={(e) => setLabel(e.currentTarget.value)}
157 />
158 </div>
159
160 {/* Gmail Rich Text Editor Area */}
161 <div class="flex-1 p-2.5 flex flex-col min-h-0">
162 <RichTextEditor
163 value={body()}
164 onChange={setBody}
165 placeholder={t("messages.bodyPlaceholder")}
166 class="flex-1 min-h-0"
167 />
168 </div>
169
170 <Show when={error()}>
171 <p class="px-3 text-xs font-medium text-destructive-text">{error()}</p>
172 </Show>
173
174 {/* Gmail Bottom Action Toolbar */}
175 <div class="flex items-center justify-between border-t border-border-hairline bg-surface-tint px-3 py-2.5">
176 <Button
177 type="submit"
178 size="sm"
179 class="h-9 rounded-lg px-5 font-semibold"
180 disabled={pending()}
181 >
182 <IconSend class="mr-2 h-4 w-4" />
183 {t("messages.send")}
184 </Button>
185
186 <button
187 type="button"
188 class="flex h-8 w-8 items-center justify-center rounded-lg text-muted-foreground hover:bg-destructive/10 hover:text-destructive-text transition-colors"
189 onClick={handleClose}
190 title={t("messages.deleteDraft")}
191 >
192 <IconTrash class="h-4 w-4" />
193 </button>
194 </div>
195 </form>
196 </Show>
197 </div>
198 </Show>
199 );
200}