"use client"; import { Loader2 } from "lucide-react"; import { useEffect, useId, useState, type FormEvent } from "react"; import { useTranslation } from "react-i18next"; import { createReadingWorkspace, updateReadingWorkspace, type ReadingWorkspace, } from "@/lib/reading-workspace-api"; import { FOLDER_COLORS, FolderGlyph, folderTone } from "./FolderGlyph"; import Tooltip from "@/shared/ui/Tooltip"; /** * Creating a collection and renaming or recolouring one are the same small * form. A new collection lives in the current workspace. */ export function FolderDialog({ collection, onClose, onSaved, }: { /** Present when editing; absent when creating. */ collection?: ReadingWorkspace; onClose: () => void; onSaved: (collection: ReadingWorkspace) => void; }) { const { t } = useTranslation(); const headingId = useId(); const [title, setTitle] = useState(collection?.title ?? ""); const [color, setColor] = useState(collection?.color ?? ""); const [working, setWorking] = useState(false); const [error, setError] = useState(""); useEffect(() => { const onKeyDown = (event: KeyboardEvent) => { if (event.key !== "Escape") return; event.preventDefault(); onClose(); }; document.addEventListener("keydown", onKeyDown); return () => document.removeEventListener("keydown", onKeyDown); }, [onClose]); const submit = (event: FormEvent) => { event.preventDefault(); if (working) return; setWorking(true); setError(""); const name = title.trim() || t("Untitled collection"); const saved = ( collection ? updateReadingWorkspace(collection.workspace_id, { title: name, color }) : createReadingWorkspace({ title: name, color }) ).then(onSaved); void saved .catch((caught) => setError( caught instanceof Error ? caught.message : t("Could not save."), ), ) .finally(() => setWorking(false)); }; return (