/** * Widgets page component * * Manage widget areas and widgets with drag-and-drop support. * Available widgets can be dragged from the palette into widget areas. * Widgets within an area can be reordered via drag-and-drop. */ import { Button, Input, Label, LayerDialog, Select, Switch, useKumoToastManager, } from "@cloudflare/kumo"; import { DndContext, DragOverlay, type CollisionDetection, type DragEndEvent, type DragStartEvent, KeyboardSensor, closestCenter, rectIntersection, useSensor, useSensors, useDraggable, useDroppable, PointerSensor, } from "@dnd-kit/core"; import { SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy, useSortable, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import type { MessageDescriptor } from "@lingui/core"; import { msg } from "@lingui/core/macro"; import { useLingui } from "@lingui/react/macro"; import { Plus, DotsSixVertical, Trash, CaretDown } from "@phosphor-icons/react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import * as React from "react"; import { fetchManifest, fetchWidgetAreas, fetchWidgetComponents, fetchMenus, createWidgetArea, createWidget, updateWidget, deleteWidget, deleteWidgetArea, reorderWidgets, type WidgetArea, type Widget, type WidgetComponent, type CreateWidgetAreaInput, type CreateWidgetInput, type UpdateWidgetInput, } from "../lib/api"; import { ApiResponseError } from "../lib/api/client.js"; import { guardUnsavedInput, revealErrors, useSingleFlight, useTakenValue, } from "../lib/dialog-form.js"; import { getPluginBlocks } from "../lib/pluginBlocks"; import { CaretNext } from "./ArrowIcons.js"; import { ConfirmDialog } from "./ConfirmDialog.js"; import { DialogError, getMutationError } from "./DialogError.js"; import { GalleryDetailPanel } from "./editor/GalleryDetailPanel"; import type { GalleryAttributes } from "./editor/GalleryNode"; import { ImageDetailPanel, type ImageAttributes } from "./editor/ImageDetailPanel"; import { PortableTextEditor, type BlockSidebarPanel, type PluginBlockDef, } from "./PortableTextEditor"; /** Palette item types that can be dragged into areas */ interface PaletteItemData { source: "palette"; widgetInput: CreateWidgetInput; label: string; } /** Identifies an existing widget being reordered */ interface ExistingWidgetData { source: "area"; areaName: string; } type DragItemData = PaletteItemData | ExistingWidgetData; function isPaletteItem(data: DragItemData): data is PaletteItemData { return data.source === "palette"; } /** Built-in widget types available in the palette */ const BUILTIN_WIDGETS: Array<{ id: string; label: MessageDescriptor; description: MessageDescriptor; input: CreateWidgetInput; }> = [ { id: "palette-content", label: msg`Content Block`, description: msg`Rich text content`, input: { type: "content" }, }, { id: "palette-menu", label: msg`Menu`, description: msg`Display a navigation menu`, input: { type: "menu" }, }, ]; /** * Localized labels/descriptions for built-in core widget components. The server * (packages/core/src/widgets/components.ts) ships these in English as stable * data; the admin maps them to translated strings client-side, the same way * error codes are localized. Plugin-provided components fall back to their * server-provided strings. */ interface CoreWidgetMeta { label: MessageDescriptor; description: MessageDescriptor; /** Prop-field labels keyed by prop key; `options` localizes select choices by option value. */ props?: Record }>; } const CORE_WIDGET_META: Record = { "core:recent-posts": { label: msg`Recent Posts`, description: msg`Display a list of recent posts`, props: { count: { label: msg`Number of posts` }, showThumbnails: { label: msg`Show thumbnails` }, showDate: { label: msg`Show date` }, urlTemplate: { label: msg`URL template (e.g. /blog/:slug)` }, }, }, "core:categories": { label: msg`Categories`, description: msg`Display category list`, props: { showCount: { label: msg`Show post count` }, hierarchical: { label: msg`Show hierarchy` }, }, }, "core:tags": { label: msg`Tags`, description: msg`Display tag cloud`, props: { showCount: { label: msg`Show count` }, limit: { label: msg`Maximum tags` }, }, }, "core:search": { label: msg`Search`, description: msg`Search form`, props: { placeholder: { label: msg`Placeholder text` }, }, }, "core:archives": { label: msg`Archives`, description: msg`Monthly/yearly archives`, props: { type: { label: msg`Group by`, options: { monthly: msg`Monthly`, yearly: msg`Yearly` }, }, limit: { label: msg`Limit` }, }, }, }; const WIDGET_AREA_NAME_PATTERN = /^[a-z0-9-]+$/; type CreateAreaField = "name" | "label"; function createAreaProblems( name: string, label: string, ): Partial> { const problems: Partial> = {}; const trimmedName = name.trim(); if (!trimmedName) problems.name = msg`Enter a name.`; else if (!WIDGET_AREA_NAME_PATTERN.test(trimmedName)) { problems.name = msg`Use lowercase letters, numbers, and hyphens.`; } if (!label.trim()) problems.label = msg`Enter a label.`; return problems; } function isConflict(error: unknown): boolean { return error instanceof ApiResponseError && error.code === "CONFLICT"; } export function Widgets() { const { t } = useLingui(); const queryClient = useQueryClient(); const toastManager = useKumoToastManager(); const [isCreateAreaOpen, setIsCreateAreaOpen] = React.useState(false); const createAreaFormId = React.useId(); const createAreaFormRef = React.useRef(null); const [newAreaName, setNewAreaName] = React.useState(""); const [newAreaLabel, setNewAreaLabel] = React.useState(""); const [newAreaDescription, setNewAreaDescription] = React.useState(""); const [createAreaSubmitted, setCreateAreaSubmitted] = React.useState(false); const takenAreaName = useTakenValue(newAreaName.trim()); const [activeId, setActiveId] = React.useState(null); const [activeDragData, setActiveDragData] = React.useState(null); const [expandedWidgets, setExpandedWidgets] = React.useState>(new Set()); const [blockSidebarPanel, setBlockSidebarPanel] = React.useState(null); // Track palette drag source across the full drag lifecycle (including drop animation) const draggingFromPaletteRef = React.useRef(false); const handleBlockSidebarOpen = React.useCallback((panel: BlockSidebarPanel) => { setBlockSidebarPanel((prev) => { // Close any existing panel before opening a new one so only one is ever active prev?.onClose(); return panel; }); }, []); const handleBlockSidebarClose = React.useCallback(() => { setBlockSidebarPanel((prev) => { prev?.onClose(); return null; }); }, []); const { data: areas = [], isLoading } = useQuery({ queryKey: ["widget-areas"], queryFn: fetchWidgetAreas, }); const { data: components = [] } = useQuery({ queryKey: ["widget-components"], queryFn: fetchWidgetComponents, }); const { data: manifest } = useQuery({ queryKey: ["manifest"], queryFn: fetchManifest, }); const pluginBlocks = React.useMemo(() => (manifest ? getPluginBlocks(manifest) : []), [manifest]); const createAreaMutation = useMutation({ mutationFn: createWidgetArea, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); setIsCreateAreaOpen(false); toastManager.add({ title: t`Widget area created` }); }, onError: (error, input) => { if (!isConflict(error) || !takenAreaName.stillHolds(input.name)) return; revealErrors(createAreaFormRef.current, () => { setCreateAreaSubmitted(true); takenAreaName.take(input.name, error); }); }, }); const submitCreateArea = useSingleFlight((input: CreateWidgetAreaInput) => createAreaMutation.mutateAsync(input), ); const createWidgetMutation = useMutation({ mutationFn: ({ areaName, input }: { areaName: string; input: CreateWidgetInput }) => createWidget(areaName, input), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); toastManager.add({ title: t`Widget added` }); }, onError: (error: Error) => { toastManager.add({ title: t`Error adding widget`, description: error.message, variant: "error", }); }, }); const handleCreateAreaOpenChange = (open: boolean) => { setIsCreateAreaOpen(open); if (!open) return; setNewAreaName(""); setNewAreaLabel(""); setNewAreaDescription(""); setCreateAreaSubmitted(false); takenAreaName.reset(); createAreaMutation.reset(); }; const areaProblems = createAreaProblems(newAreaName, newAreaLabel); if (takenAreaName.isTaken) { areaProblems.name = msg`Another widget area already uses this name. Enter a different one.`; } const areaErrors = createAreaSubmitted ? areaProblems : {}; const handleCreateArea = (e: React.FormEvent) => { e.preventDefault(); if (createAreaMutation.isPending) return; if (Object.keys(areaProblems).length > 0) { revealErrors(e.currentTarget, () => setCreateAreaSubmitted(true)); return; } submitCreateArea({ name: newAreaName.trim(), label: newAreaLabel.trim(), description: newAreaDescription, }); }; const sensors = useSensors( useSensor(PointerSensor, { activationConstraint: { distance: 8 }, }), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }), ); // Custom collision detection: palette items use rectIntersection (anywhere // over the area counts) and only match area:* droppables. Existing widgets // use closestCenter for precise reorder positioning. const collisionDetection: CollisionDetection = React.useCallback((args) => { const dragData = args.active.data.current as DragItemData | undefined; if (dragData && isPaletteItem(dragData)) { // Only consider area droppables, use generous rect intersection const areaContainers = args.droppableContainers.filter((c) => String(c.id).startsWith("area:"), ); return rectIntersection({ ...args, droppableContainers: areaContainers }); } return closestCenter(args); }, []); const handleDragStart = (event: DragStartEvent) => { const id = String(event.active.id); const data = (event.active.data.current as DragItemData) ?? null; setActiveId(id); setActiveDragData(data); draggingFromPaletteRef.current = data !== null && isPaletteItem(data); }; const reorderMutation = useMutation({ mutationFn: ({ areaName, widgetIds }: { areaName: string; widgetIds: string[] }) => reorderWidgets(areaName, widgetIds), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); }, onError: (error: Error) => { toastManager.add({ title: t`Error reordering widgets`, description: error.message, variant: "error", }); }, }); const handleDragEnd = (event: DragEndEvent) => { const { active, over } = event; const dragData = active.data.current as DragItemData | undefined; setActiveId(null); setActiveDragData(null); if (!over || !dragData) return; // Case 1: Dragging from palette into an area if (isPaletteItem(dragData)) { const overId = String(over.id); // The drop target is a widget area (droppable id = "area:{name}") if (overId.startsWith("area:")) { const areaName = overId.slice(5); createWidgetMutation.mutate({ areaName, input: dragData.widgetInput, }); } return; } // Case 2: Reordering within an area if (active.id === over.id) return; const sourceArea = areas.find((area) => area.widgets?.some((w) => w.id === active.id)); if (!sourceArea?.widgets) return; const oldIndex = sourceArea.widgets.findIndex((w) => w.id === active.id); const newIndex = sourceArea.widgets.findIndex((w) => w.id === over.id); if (oldIndex === -1 || newIndex === -1) return; const newWidgets = [...sourceArea.widgets]; const [movedWidget] = newWidgets.splice(oldIndex, 1); if (!movedWidget) return; newWidgets.splice(newIndex, 0, movedWidget); reorderMutation.mutate({ areaName: sourceArea.name, widgetIds: newWidgets.map((w) => w.id), }); }; const toggleWidget = (widgetId: string) => { setExpandedWidgets((prev) => { const next = new Set(prev); if (next.has(widgetId)) { next.delete(widgetId); } else { next.add(widgetId); } return next; }); }; // Build the palette label for the drag overlay const activePaletteLabel = activeDragData && isPaletteItem(activeDragData) ? activeDragData.label : null; // Find the existing widget being dragged for overlay const activeWidget = activeId && activeDragData && !isPaletteItem(activeDragData) ? areas.flatMap((a) => a.widgets ?? []).find((w) => w.id === activeId) : null; if (isLoading) { return (
{t`Loading widgets...`}
); } return ( <>

{t`Widgets`}

{t`Manage content widgets in your widget areas`}

( )} /> {t`Create Widget Area`} {t`A widget area is a place in your theme, such as a sidebar, that holds widgets.`}
setNewAreaName(e.target.value)} error={areaErrors.name && t(areaErrors.name)} aria-invalid={areaErrors.name ? true : undefined} /> setNewAreaLabel(e.target.value)} error={areaErrors.label && t(areaErrors.label)} aria-invalid={areaErrors.label ? true : undefined} /> setNewAreaDescription(e.target.value)} />
{createAreaMutation.isPending ? t`Creating...` : t`Create`}
{/* Available Widgets (draggable palette) */}

{t`Available Widgets`}

{t`Drag widgets into an area to add them`}

{BUILTIN_WIDGETS.map((item) => ( ))} {components.map((comp) => { const meta = CORE_WIDGET_META[comp.id]; const label = meta ? t(meta.label) : comp.label; const description = meta ? t(meta.description) : comp.description; return ( ); })}
{/* Widget Areas (droppable + sortable) */}
{areas.length === 0 ? (

{t`No widget areas yet. Create one to get started.`}

) : ( areas.map((area) => ( )) )}
{/* Drag overlay — no drop animation for palette items (source stays in place). Use ref because state is cleared in handleDragEnd before animation runs. */} {activePaletteLabel ? (
{activePaletteLabel}
) : activeWidget ? (
{activeWidget.title || t`Untitled Widget`} ({activeWidget.type})
) : null}
{/* A single block-sidebar panel for the whole page — ensures only one is ever open at a time, preventing stacked fixed overlays and duplicated window listeners. */} {blockSidebarPanel?.type === "image" && ( blockSidebarPanel.onUpdate(attrs)} onReplace={(attrs) => blockSidebarPanel.onReplace(attrs as unknown as Record) } onDelete={() => { blockSidebarPanel.onDelete(); setBlockSidebarPanel(null); }} onClose={handleBlockSidebarClose} /> )}
{blockSidebarPanel?.type === "gallery" && ( blockSidebarPanel.onUpdate(attrs)} onDelete={() => { blockSidebarPanel.onDelete(); setBlockSidebarPanel(null); }} onClose={handleBlockSidebarClose} /> )} ); } /** A draggable item in the available widgets palette */ function DraggablePaletteItem({ id, label, description, widgetInput, }: { id: string; label: string; description?: string; widgetInput: CreateWidgetInput; }) { const { attributes, listeners, setNodeRef, isDragging } = useDraggable({ id, data: { source: "palette", widgetInput, label, } satisfies PaletteItemData, }); return (
{label}
{description &&
{description}
}
); } function WidgetAreaPanel({ area, expandedWidgets, onToggleWidget, isDraggingPalette, components, pluginBlocks, onBlockSidebarOpen, onBlockSidebarClose, }: { area: WidgetArea; expandedWidgets: Set; onToggleWidget: (id: string) => void; isDraggingPalette: boolean; components: WidgetComponent[]; pluginBlocks: PluginBlockDef[]; onBlockSidebarOpen: (panel: BlockSidebarPanel) => void; onBlockSidebarClose: () => void; }) { const { t } = useLingui(); const queryClient = useQueryClient(); const toastManager = useKumoToastManager(); const [deleteAreaName, setDeleteAreaName] = React.useState(null); // Make the area a droppable target for palette items const { setNodeRef: setDropRef, isOver } = useDroppable({ id: `area:${area.name}`, }); const deleteAreaMutation = useMutation({ mutationFn: deleteWidgetArea, onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); setDeleteAreaName(null); toastManager.add({ title: t`Widget area deleted` }); }, }); const hasWidgets = area.widgets && area.widgets.length > 0; return (

{area.label}

{area.description &&

{area.description}

}
{hasWidgets ? ( w.id)} strategy={verticalListSortingStrategy} > {area.widgets!.map((widget) => ( onToggleWidget(widget.id)} components={components} pluginBlocks={pluginBlocks} onBlockSidebarOpen={onBlockSidebarOpen} onBlockSidebarClose={onBlockSidebarClose} /> ))} ) : null} {/* Drop zone hint — shown when dragging a palette item */} {isDraggingPalette && (
{isOver ? t`Drop to add widget` : t`Drag here to add`}
)} {!hasWidgets && !isDraggingPalette && (
{t`Drag widgets here to add them`}
)}
{ setDeleteAreaName(null); deleteAreaMutation.reset(); }} title={t`Delete Widget Area?`} description={t`This will delete the widget area and all its widgets. This action cannot be undone.`} confirmLabel={t`Delete`} pendingLabel={t`Deleting...`} isPending={deleteAreaMutation.isPending} error={deleteAreaMutation.error} onConfirm={() => deleteAreaMutation.mutate(area.name)} />
); } function WidgetItem({ widget, areaName, isExpanded, onToggle, components, pluginBlocks, onBlockSidebarOpen, onBlockSidebarClose, }: { widget: Widget; areaName: string; isExpanded: boolean; onToggle: () => void; components: WidgetComponent[]; pluginBlocks: PluginBlockDef[]; onBlockSidebarOpen: (panel: BlockSidebarPanel) => void; onBlockSidebarClose: () => void; }) { const { t } = useLingui(); const queryClient = useQueryClient(); const toastManager = useKumoToastManager(); const [isDeleteOpen, setIsDeleteOpen] = React.useState(false); const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: widget.id, data: { source: "area", areaName, } satisfies ExistingWidgetData, }); const style = { transform: CSS.Transform.toString(transform), transition, }; const deleteMutation = useMutation({ mutationFn: () => deleteWidget(areaName, widget.id), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); setIsDeleteOpen(false); toastManager.add({ title: t`Widget deleted` }); }, }); const updateMutation = useMutation({ mutationFn: (input: UpdateWidgetInput) => updateWidget(areaName, widget.id, input), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["widget-areas"] }); toastManager.add({ title: t`Widget updated` }); }, onError: (error: Error) => { toastManager.add({ title: t`Error updating widget`, description: error.message, variant: "error", }); }, }); return (
{isExpanded && ( updateMutation.mutate(input)} isSaving={updateMutation.isPending} onBlockSidebarOpen={onBlockSidebarOpen} onBlockSidebarClose={onBlockSidebarClose} /> )} { setIsDeleteOpen(false); deleteMutation.reset(); }} title={t`Delete widget?`} description={t`"${widget.title || t`Untitled Widget`}" will be removed from this widget area. This action cannot be undone.`} confirmLabel={t`Delete`} pendingLabel={t`Deleting...`} isPending={deleteMutation.isPending} error={deleteMutation.error} onConfirm={() => deleteMutation.mutate()} />
); } /** Inline editor form for a widget, rendered when the widget is expanded */ function WidgetEditor({ widget, components, pluginBlocks, onSave, isSaving, onBlockSidebarOpen, onBlockSidebarClose, }: { widget: Widget; components: WidgetComponent[]; pluginBlocks: PluginBlockDef[]; onSave: (input: UpdateWidgetInput) => void; isSaving: boolean; onBlockSidebarOpen: (panel: BlockSidebarPanel) => void; onBlockSidebarClose: () => void; }) { const { t } = useLingui(); const [title, setTitle] = React.useState(widget.title ?? ""); const [content, setContent] = React.useState( Array.isArray(widget.content) ? widget.content : [], ); const [menuName, setMenuName] = React.useState(widget.menuName ?? ""); const [componentId, setComponentId] = React.useState(widget.componentId ?? ""); const [componentProps, setComponentProps] = React.useState>( widget.componentProps ?? {}, ); const { data: menus = [] } = useQuery({ queryKey: ["menus"], queryFn: () => fetchMenus(), enabled: widget.type === "menu", }); const selectedComponent = components.find((c) => c.id === componentId); const handleSave = () => { const input: UpdateWidgetInput = { title }; if (widget.type === "content") { input.content = content; } else if (widget.type === "menu") { input.menuName = menuName; } else if (widget.type === "component") { input.componentId = componentId; input.componentProps = componentProps; } onSave(input); }; return (
setTitle(e.target.value)} placeholder={t`Widget title`} /> {widget.type === "content" && (
[0]["value"]} onChange={(value) => setContent(value)} minimal placeholder={t`Write widget content...`} pluginBlocks={pluginBlocks} onBlockSidebarOpen={onBlockSidebarOpen} onBlockSidebarClose={onBlockSidebarClose} />
)} {widget.type === "menu" && ( )} {widget.type === "component" && ( <> {selectedComponent && Object.entries(selectedComponent.props).map(([key, def]) => ( setComponentProps((prev) => ({ ...prev, [key]: v }))} /> ))} )}
); } /** Renders a single prop field for a component widget based on PropDef type */ function ComponentPropField({ componentId, propKey, def, value, onChange, }: { componentId: string; propKey: string; def: WidgetComponent["props"][string]; value: unknown; onChange: (value: unknown) => void; }) { const { t } = useLingui(); // Localize built-in core widget prop labels/options; fall back to the // server-provided string for plugin-registered components. const propMeta = CORE_WIDGET_META[componentId]?.props?.[propKey]; const label = propMeta ? t(propMeta.label) : def.label; const optionLabel = (optValue: string, fallback: string) => { const descriptor = propMeta?.options?.[optValue]; return descriptor ? t(descriptor) : fallback; }; switch (def.type) { case "string": return ( onChange(e.target.value)} /> ); case "number": return ( onChange(Number(e.target.value))} /> ); case "boolean": return ; case "select": { const items: Record = {}; for (const opt of def.options ?? []) { items[opt.value] = optionLabel(opt.value, opt.label); } return ( ); } default: return ( onChange(e.target.value)} /> ); } }