import { createSignal, createEffect, Show } from "solid-js"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { RichTextEditor } from "@/components/ui/rich-text-editor"; import { UserSearchSelect } from "@/components/users/user-search-select"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { IconAlert, IconSend, IconTrash, IconX } from "@/components/ui/icons"; import { postMessage } from "@/api/messages"; import { formatApiError } from "@/api/client"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; import { suppressQuickActions } from "@/stores/quick-actions"; interface GmailComposeBoxProps { open: boolean; onClose: () => void; onSuccess: () => void; } export function GmailComposeBox(props: GmailComposeBoxProps) { const t = useT(); const auth = useAuth(); const [recipient, setRecipient] = createSignal(""); const [subject, setSubject] = createSignal(""); const [body, setBody] = createSignal(""); const [label, setLabel] = createSignal(""); const [isMinimized, setIsMinimized] = createSignal(false); const [pending, setPending] = createSignal(false); const [error, setError] = createSignal(""); // The sheet docks at the bottom of a phone; the quick-action button would // float over its editor. createEffect(() => { if (props.open) suppressQuickActions(); }); createEffect(() => { if (props.open) { setRecipient(""); setSubject(""); setBody(""); setLabel(""); setIsMinimized(false); setError(""); } }); const reset = () => { setRecipient(""); setSubject(""); setBody(""); setLabel(""); setError(""); }; const handleClose = () => { reset(); props.onClose(); }; // Closing throws the draft away, so anything typed is confirmed first. const [confirmDiscard, setConfirmDiscard] = createSignal(false); const hasDraft = () => !!recipient() || subject().trim() !== "" || label().trim() !== "" || body().replace(/<[^>]*>| /g, "").trim() !== ""; const requestClose = () => { if (hasDraft()) setConfirmDiscard(true); else handleClose(); }; // The backend needs a recipient and a subject (the body is optional), so // "Gönder" stays off until both are there and says which one is missing. const sendBlocker = () => { if (!recipient()) return t("messages.sendHintRecipient"); if (!subject().trim()) return t("messages.sendHintSubject"); return ""; }; const handleSend = async (e: SubmitEvent) => { e.preventDefault(); if (sendBlocker()) { setError(sendBlocker()); return; } setError(""); setPending(true); try { await postMessage({ recipient_id: recipient(), subject: subject().trim(), body: body().trim() || undefined, label: label().trim() || undefined, }); props.onSuccess(); handleClose(); } catch (err) { setError(formatApiError(err)); } finally { setPending(false); } }; return ( } onConfirm={handleClose} /> ); }