import { useQuery } from "@tanstack/react-query" import { useNavigate } from "@tanstack/react-router" import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { dashboardsQueryOptions } from "./queries" import { INPUT_WIDGETS, WIDGET_DESCRIPTIONS, WIDGET_LABELS, type WidgetKind, } from "./widgets" const KINDS = Object.keys(WIDGET_LABELS) as WidgetKind[] /** * What a round face draws badly: a row of controls or text, a plot or a strip * that wants the width a circle only has across its middle, and a page in a * tile. Offered all the same — a soft mark, not a refusal. */ const POOR_ON_ROUND = new Set([ "input", "buttons", "chart", "forecast", "agenda", "embed", ]) const GROUPS = [ ["Show a value", KINDS.filter((kind) => !INPUT_WIDGETS.has(kind))], ["Control something", KINDS.filter((kind) => INPUT_WIDGETS.has(kind))], ] as const /** * ⌘P on a dashboard being edited: add a widget, jump to another dashboard, or * publish — the flow canvas's palette, for this editor. */ export function DashboardPalette({ open, onOpenChange, onAdd, onSettings, onPublish, round, }: { open: boolean onOpenChange: (open: boolean) => void onAdd: (kind: WidgetKind) => void onSettings: () => void /** Absent while there is nothing to publish. */ onPublish?: () => void /** The dashboard is round: the types that draw badly on it say so. */ round?: boolean }) { const navigate = useNavigate() const { data } = useQuery({ ...dashboardsQueryOptions(), enabled: open }) // Unmounted rather than faded, for the reason the flow palette gives. if (!open) return null const dashboards = data?.data ?? [] const close = (action: () => void) => { onOpenChange(false) action() } return ( Nothing matches that. {GROUPS.map(([heading, kinds]) => ( {kinds.map((kind) => ( close(() => onAdd(kind))} > {WIDGET_LABELS[kind]} {WIDGET_DESCRIPTIONS[kind]} {round && POOR_ON_ROUND.has(kind) ? " · Poor fit for round" : ""} ))} ))} {dashboards.length > 0 ? ( {dashboards.map((dashboard) => ( close(() => // Editing a neighbour is the same job, as on the rail. navigate({ to: "/dashboards/$name", params: { name: dashboard.name }, search: { edit: true }, }), ) } > {dashboard.title || dashboard.name} ))} ) : null} {onPublish ? ( close(onPublish)} > Publish this dashboard ) : null} close(onSettings)} > Dashboard settings ) }