import { Badge, Checkbox, Input, InsetPanel, LayerDialog, Radio, Select, SkeletonLine, Text, } from "@cloudhal/ui"; import { Camera } from "@phosphor-icons/react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { useEffect, useState } from "react"; import { useT } from "../i18n/index.tsx"; import { api } from "../lib/api.ts"; import { MENU_LIMITS, type MenuWrite, validateMenuKey } from "../lib/menus.ts"; import { ErrorBanner } from "./ErrorBanner.tsx"; import { HiddenBadge, useMenuName } from "./Menus.tsx"; function OneSnapshot() { const t = useT("menus"); return (
{t("governance.oneSnapshot")}
); } const CUSTOM = "__custom__"; /** * "New menu" (RFC-0003 §8): a well-known place the site has no menu for yet, or a custom key * (a slug, unique per site, immutable once created), and how deep it may go. The menu starts empty. */ export function CreateMenuDialog({ open, onOpenChange, siteId, taken, missing, defaultDepth, onCreated, }: { open: boolean; onOpenChange: (open: boolean) => void; siteId: string; /** Keys the site already has a menu for. */ taken: string[]; /** Well-known keys without a menu (offered as cards). */ missing: string[]; defaultDepth: (key: string) => number; onCreated: (result: MenuWrite) => void; }) { const t = useT("menus"); const name = useMenuName(); const [place, setPlace] = useState(CUSTOM); const [key, setKey] = useState(""); const [depth, setDepth] = useState(3); const [attempted, setAttempted] = useState(false); useEffect(() => { if (!open) return; const first = missing[0] ?? CUSTOM; setPlace(first); setKey(""); setDepth(defaultDepth(first === CUSTOM ? "" : first)); setAttempted(false); }, [open]); const finalKey = place === CUSTOM ? key.trim() : place; const keyError = place === CUSTOM ? validateMenuKey(finalKey, taken) : undefined; const create = useMutation({ mutationFn: () => api.menus.create(siteId, { key: finalKey, maxDepth: depth, items: [] }), onSuccess: (r) => { onCreated(r); onOpenChange(false); }, }); useEffect(() => { if (open) create.reset(); }, [open]); const submit = () => { setAttempted(true); if (keyError) return; create.mutate(); }; return ( {t("menus.create.title")} {t("menus.create.description")}
{missing.length > 0 && ( { setPlace(String(v)); setDepth(defaultDepth(String(v) === CUSTOM ? key : String(v))); }} > {missing.map((k) => ( ))} )} {place === CUSTOM && ( setKey(v.toLowerCase())} onKeyDown={(e) => { if (e.key === "Enter") submit(); }} error={attempted && keyError ? t(`menus.create.errors.${keyError}`) : undefined} data-testid="menu-key" /> )}