Dashboard add widget: searchable command palette with ⌘P, as on flows
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
CommandDialog,
|
||||
CommandEmpty,
|
||||
CommandGroup,
|
||||
CommandInput,
|
||||
CommandItem,
|
||||
CommandList,
|
||||
} from "@/components/ui/command"
|
||||
import {
|
||||
INPUT_WIDGETS,
|
||||
WIDGET_DESCRIPTIONS,
|
||||
WIDGET_LABELS,
|
||||
type WidgetKind,
|
||||
} from "./widgets"
|
||||
|
||||
const KINDS = Object.keys(WIDGET_LABELS) as WidgetKind[]
|
||||
|
||||
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: the flow canvas's add-a-node palette, for widgets. */
|
||||
export function WidgetPalette({
|
||||
open,
|
||||
onOpenChange,
|
||||
onAdd,
|
||||
}: {
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
onAdd: (kind: WidgetKind) => void
|
||||
}) {
|
||||
// Unmounted rather than faded, for the reason the flow palette gives.
|
||||
if (!open) return null
|
||||
|
||||
return (
|
||||
<CommandDialog
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title="Add a widget"
|
||||
description="Search the widget types"
|
||||
>
|
||||
<CommandInput placeholder="Add a widget…" />
|
||||
<CommandList>
|
||||
<CommandEmpty>Nothing matches that.</CommandEmpty>
|
||||
{GROUPS.map(([heading, kinds]) => (
|
||||
<CommandGroup key={heading} heading={heading}>
|
||||
{kinds.map((kind) => (
|
||||
<CommandItem
|
||||
key={kind}
|
||||
value={`${WIDGET_LABELS[kind]} ${WIDGET_DESCRIPTIONS[kind]}`}
|
||||
data-testid={`add-widget-${kind}`}
|
||||
onSelect={() => {
|
||||
onOpenChange(false)
|
||||
onAdd(kind)
|
||||
}}
|
||||
>
|
||||
<span className="flex flex-col">
|
||||
<span>{WIDGET_LABELS[kind]}</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{WIDGET_DESCRIPTIONS[kind]}
|
||||
</span>
|
||||
</span>
|
||||
</CommandItem>
|
||||
))}
|
||||
</CommandGroup>
|
||||
))}
|
||||
</CommandList>
|
||||
</CommandDialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user