import { useCallback, useEffect, useRef, useState } from "react"; import { Code2, Link as LinkIcon, MoreHorizontal, Trash2, Wand2, } from "lucide-react"; import { useDismissOn } from "../lib/useDismissOn"; /** * Mobile-only overflow menu for the canvas toolbar. * * Bundles Auto-connect / Share / Clear into a single "⋮" dropdown so * narrow viewports (< md, i.e. <768px) keep the toolbar to three visible * controls (Load preset, More, Run pipeline) instead of five. Desktop * renders each action as its own toolbar button and hides this * component entirely via `md:hidden` on the wrapper `className`. * * The component closes itself on: * - outside click * - Escape keypress * - viewport growing past `md` (i.e. the menu's wrapper flips to * `display:none`; without this guard the React `open` state would * silently stay `true` and the menu would pop back open if the * user shrinks the viewport again) */ export function MoreMenu({ className = "", canAutoConnect, hasEdgesToReplace, canClear, canExport, onAutoConnect, onShare, onExport, onClear, }: { className?: string; canAutoConnect: boolean; hasEdgesToReplace: boolean; canClear: boolean; canExport: boolean; onAutoConnect: () => void; onShare: () => void; onExport: () => void; onClear: () => void; }) { const [open, setOpen] = useState(false); const rootRef = useRef(null); useDismissOn(open, rootRef, useCallback(() => setOpen(false), [])); // Responsive guard: if the viewport grows past the `md` breakpoint // while the menu is open, the wrapper becomes `display:none` but the // React `open` state is unchanged. Resizing back to mobile would then // re-reveal the menu in an "open" state the user never asked for. // `matchMedia` fires the moment the breakpoint is crossed so we can // close the menu eagerly. useEffect(() => { if (typeof window === "undefined") return; const mq = window.matchMedia("(min-width: 768px)"); const onChange = () => { if (mq.matches) setOpen(false); }; mq.addEventListener("change", onChange); return () => mq.removeEventListener("change", onChange); }, []); const items: { key: string; icon: React.ReactNode; label: string; sub?: string; disabled?: boolean; tone?: "default" | "danger"; onClick: () => void; }[] = [ { key: "auto", icon: , label: "Auto-connect", sub: hasEdgesToReplace ? "Re-wire all blocks (replaces existing links)" : "Wire all blocks into a sensible chain", disabled: !canAutoConnect, onClick: onAutoConnect, }, { key: "share", icon: , label: "Copy share link", sub: "Copies a URL that restores this pipeline", onClick: onShare, }, { key: "export", icon: , label: "Export .py", sub: "Download pipeline as a Python script", disabled: !canExport, onClick: onExport, }, { key: "clear", icon: , label: "Clear canvas", sub: "Remove every block and link", disabled: !canClear, tone: "danger", onClick: onClear, }, ]; return (
{open && ( // Mobile ( {items.map((it) => ( ))}
)} ); }