"use client"; import { MoreHorizontal } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { type WorkspaceMenuHost, type WorkspaceMenuItem, type WorkspaceMenuSection, } from "@/components/reading/workspace-menu-context"; import { MenuItem } from "./WorkspaceChrome"; import Tooltip from "@/shared/ui/Tooltip"; export type WorkspaceMenuSections = Partial< Record >; /** * The collector behind the workspace's single ⋯. * * The reader and the companion hand their ⋯ items up through the host, so * the page has one ⋯ instead of three a hand's width apart. */ export function useWorkspaceMenuHost(): { host: WorkspaceMenuHost; sections: WorkspaceMenuSections; } { const [sections, setSections] = useState({}); const host = useMemo( () => ({ register: (section, items) => setSections((current) => current[section] === (items ?? undefined) ? current : { ...current, [section]: items ?? undefined }, ), }), [], ); return { host, sections }; } export function WorkspaceMenu({ collection, sections, }: { /** What acts on the whole collection; always first. */ collection: WorkspaceMenuItem[]; sections: WorkspaceMenuSections; }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); useEffect(() => { if (!open) return; const onKeyDown = (event: KeyboardEvent) => { if (event.key !== "Escape") return; event.preventDefault(); setOpen(false); }; document.addEventListener("keydown", onKeyDown); return () => document.removeEventListener("keydown", onKeyDown); }, [open]); const groups: Array<[string, string, WorkspaceMenuItem[] | undefined]> = [ ["collection", t("Collection"), collection], ["material", t("Document"), sections.material], ["conversation", t("Conversation"), sections.conversation], ]; return (
{open && ( <>
setOpen(false)} />
{groups.map(([key, heading, items], index) => { if (!items?.length) return null; return (
{index > 0 && (
)}

{heading}

{items.map((item) => ( { // A toggle stays open so the learner sees it flip. if (item.active === undefined) setOpen(false); item.onSelect(); }} /> ))}
); })}
)}
); }