import { useMemo, useState, type ReactNode } from "react"; import { InspectorSelectPolicyContext } from "./InspectorContext.js"; import { Select, type SelectProps } from "./Select.js"; import { cn } from "./cn.js"; import { Icon } from "./Icon.js"; import { IconButton } from "./Button.js"; export interface InspectorSectionSpec { id: string; label: string; description?: string; group?: string; content: ReactNode; disabled?: boolean; tone?: "default" | "accent"; icon?: ReactNode; action?: { label: string; icon: ReactNode; onAction: () => void; disabled?: boolean }; } export interface InspectorProps { sections: InspectorSectionSpec[]; /** Spaced form fields above sections and centered actions below them. */ header?: ReactNode; footer?: ReactNode; defaultOpen?: string[]; openSections?: string[]; activeId?: string; singleOpen?: boolean; variant?: "default" | "panel"; className?: string; onOpenSectionsChange?: (ids: string[]) => void; onActiveChange?: (id: string) => void; } export function Inspector({ sections, header, footer, defaultOpen = [], openSections, activeId, singleOpen = false, variant = "default", className, onOpenSectionsChange, onActiveChange, }: InspectorProps) { const [internalOpenIds, setInternalOpenIds] = useState(() => new Set(defaultOpen)); const openIds = useMemo( () => openSections === undefined ? internalOpenIds : new Set(openSections), [internalOpenIds, openSections], ); const groups = useMemo(() => { const result: Array<{ label?: string; sections: InspectorSectionSpec[] }> = []; sections.forEach((section) => { const previous = result[result.length - 1]; if (previous && previous.label === section.group) { previous.sections.push(section); } else { result.push({ label: section.group, sections: [section] }); } }); return result; }, [sections]); const toggle = (id: string) => { const next = singleOpen ? new Set() : new Set(openIds); if (openIds.has(id)) next.delete(id); else next.add(id); if (openSections === undefined) setInternalOpenIds(next); onOpenSectionsChange?.([...next]); onActiveChange?.(id); }; return (
{header ?
{header}
: null} {groups.map((group, groupIndex) => (
{group.sections.map((section) => { const isOpen = openIds.has(section.id); return (
{section.action ? {section.action.icon} : null}
{isOpen ?
{section.content}
: null}
); })}
))} {footer ?
{footer}
: null}
); } export interface ControlRowProps { label: ReactNode; children: ReactNode; layout?: "inline" | "stack"; className?: string; } export function ControlRow({ label, children, layout = "inline", className }: ControlRowProps) { return (
{label}
{children}
); } export interface InspectorSelectFieldProps extends Omit, "label" | "variant"> { label: string; fieldClassName?: string; } /** * Canonical Inspector selection field. * * Inspector labels always sit above a full-width Engine split select. The * public API intentionally exposes neither `layout` nor `variant`, so an * Inspector consumer cannot fall back to the Hub/Launcher integrated pill. */ export function InspectorSelectField({ label, fieldClassName, ...selectProps }: InspectorSelectFieldProps) { return (