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"
/>
)}
{t("menus.create.confirm")}
);
}
/**
* "Create menu from current pages" (RFC-0003 §8, compatibility): a dry run lists the items first —
* the pages today's derived header shows, without labels so renames keep flowing in — with an
* option to add the top-level categories. Nothing is written until the person confirms.
*/
export function FromPagesDialog({
open,
onOpenChange,
siteId,
menuKey,
onCreated,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
siteId: string;
menuKey: string;
onCreated: (result: MenuWrite) => void;
}) {
const t = useT("menus");
const name = useMenuName();
const [categories, setCategories] = useState(false);
useEffect(() => {
if (open) setCategories(false);
}, [open]);
const preview = useQuery({
queryKey: ["menu-from-pages", siteId, menuKey, categories],
queryFn: () => api.menus.fromPagesPreview(siteId, { key: menuKey, categories }),
enabled: open,
staleTime: 0,
});
const create = useMutation({
mutationFn: () => api.menus.fromPages(siteId, { key: menuKey, categories }),
onSuccess: (r) => {
onCreated(r);
onOpenChange(false);
},
});
useEffect(() => {
if (open) create.reset();
}, [open]);
const p = preview.data;
const items = p?.resolved.items ?? [];
return (
{t("menus.fromPages.title", { name: name(menuKey) })}
{t("menus.fromPages.description")}
{preview.error ? (
preview.refetch()} />
) : !p ? (
) : (
{items.length === 0 ? (
{t("menus.fromPages.none")}
) : (
{items.map((i) => (
{i.label}
{i.href && (
{i.href}
)}
{i.state === "hidden" && i.reason ? (
) : (
{i.kind === "term"
? t("menus.fromPages.category")
: t("menus.fromPages.page")}
)}
))}
)}
)}
setCategories(on === true)}
/>
{t("menus.fromPages.labelsHint")}
create.mutate()}
data-testid="from-pages-confirm"
>
{t("menus.fromPages.confirm")}
);
}