"use client" /** * Panels and helpers behind `TablePropertiesDrawer`. Kept out of `drawer.tsx` so the * drawer reads as a router between panels and every conditional lives in a unit small * enough to reason about on its own. */ import * as React from "react" import { cn } from "../../lib/utils" import type { DataListViewType } from "../../lib/data-list-view" import { DATA_LIST_VIEW_TILES, dataListViewLabel } from "../../lib/data-list-view" import { dataListViewTilesForHub } from "../../lib/data-list-view-registry" import type { RowHeight } from "../../lib/row-height" import { ROW_HEIGHT_TILES } from "../../lib/row-height" import { Input } from "../ui/input" import { FilterByFieldDropdownContent } from "../data-table/filter-by-field-dropdown" import { SelectionTileGrid } from "../ui/selection-tile-grid" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "../ui/dropdown-menu" import { FloatingSheetPanelBody, FloatingSheetPanelHeader, } from "../ui/floating-sheet-panel" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select" import type { DataListDisplayOptions } from "../../lib/data-list-display-options" import { Tip } from "../ui/tip" import { ToggleSwitch } from "../ui/toggle-switch" import { Button } from "../ui/button" import { DrawerFilterCard } from "./filter-card" import { DrawerSortCard } from "./sort-card" import { ColumnRow } from "./column-row" import { useDraggableList } from "./draggable-list" import { type ConditionalRule, type FilterFieldDef, RULE_COLORS, } from "../../lib/table-properties-types" import { createConditionalRuleId } from "../../lib/conditional-rule-id" import { PROPERTIES_SHEET_PORTAL_Z, PROPERTIES_SHEET_TOOLTIP_Z, PROPERTIES_SHEET_TOOLTIP_Z_INDEX, } from "./sheet-portal-z" import type { TablePropertiesDrawerProps } from "./drawer" export type SheetPanel = "main" | "table-display" | "filter" | "sort" | "group" | "columns" | "conditional-rules" const EMPTY_ACTIVE_FILTERS: NonNullable = [] const EMPTY_SORT_RULES: NonNullable = [] const EMPTY_CONDITIONAL_RULES: ConditionalRule[] = [] const EMPTY_COL_ORDER: string[] = [] const EMPTY_FILTER_FIELDS: FilterFieldDef[] = [] const EMPTY_FIELD_DEFINITIONS: NonNullable = [] const EMPTY_STRING_SET = new Set() const NOOP = () => {} /** Props that always have a value inside the drawer, once defaults are applied. */ type DefaultedKeys = | "showGridlines" | "onShowGridlinesChange" | "pagination" | "onPaginationChange" | "activeFilters" | "onAddFilter" | "onUpdateFilter" | "onRemoveFilter" | "getFilterConnector" | "onToggleFilterConnector" | "filterBarVisible" | "onFilterBarVisibleChange" | "drawerExpandedFilters" | "onDrawerExpandedFiltersChange" | "totalRows" | "filteredRows" | "sortRules" | "onSortRulesChange" | "onAddSortRule" | "onRemoveSortRule" | "onToggleSortDir" | "colOrder" | "onColOrderChange" | "hiddenCols" | "onToggleColVisibility" | "onMoveCol" | "groupBy" | "onGroupByChange" | "conditionalRules" | "onAddConditionalRule" | "onRemoveConditionalRule" | "onUpdateConditionalRule" | "filterFields" | "fieldDefinitions" | "displayOptions" | "onDisplayOptionsChange" | "rowHeight" | "onRowHeightChange" export type ResolvedDrawerProps = TablePropertiesDrawerProps & Required> // Non-creation props — safe defaults when using the 2-step "Create view" surface. const DRAWER_DEFAULTS = { showGridlines: true, onShowGridlinesChange: NOOP, pagination: true, onPaginationChange: NOOP, activeFilters: EMPTY_ACTIVE_FILTERS, onAddFilter: NOOP, onUpdateFilter: NOOP, onRemoveFilter: NOOP, getFilterConnector: () => "and" as const, onToggleFilterConnector: NOOP, filterBarVisible: true, onFilterBarVisibleChange: NOOP, drawerExpandedFilters: EMPTY_STRING_SET, onDrawerExpandedFiltersChange: NOOP, totalRows: 0, filteredRows: 0, sortRules: EMPTY_SORT_RULES, onSortRulesChange: NOOP, onAddSortRule: NOOP, onRemoveSortRule: NOOP, onToggleSortDir: NOOP, colOrder: EMPTY_COL_ORDER, onColOrderChange: NOOP, hiddenCols: EMPTY_STRING_SET, onToggleColVisibility: NOOP, onMoveCol: NOOP, groupBy: null, onGroupByChange: NOOP, conditionalRules: EMPTY_CONDITIONAL_RULES, onAddConditionalRule: NOOP, onRemoveConditionalRule: NOOP, onUpdateConditionalRule: NOOP, filterFields: EMPTY_FILTER_FIELDS, fieldDefinitions: EMPTY_FIELD_DEFINITIONS, onDisplayOptionsChange: NOOP, rowHeight: "default" as RowHeight, onRowHeightChange: NOOP, } /** Apply defaults for every prop the drawer can be opened without (undefined only, like a destructuring default). */ export function resolveDrawerProps( props: TablePropertiesDrawerProps, defaultDisplayOptions: DataListDisplayOptions, ): ResolvedDrawerProps { const defined: Partial = Object.fromEntries( Object.entries(props).filter(([, v]) => v !== undefined), ) return { ...DRAWER_DEFAULTS, displayOptions: defaultDisplayOptions, ...defined, open: props.open, onOpenChange: props.onOpenChange } as ResolvedDrawerProps } export function resolveSheetPanel(open: boolean, initialPanel: string | null | undefined, navPanel: SheetPanel): SheetPanel { if (!open) return "main" return initialPanel ? (initialPanel as SheetPanel) : navPanel } /** `1 filter` / `2 filters`. */ function plural(count: number, noun: string): string { return `${count} ${noun}${count !== 1 ? "s" : ""}` } export function columnLabelFor( key: string, resolveFn: ((key: string) => string) | undefined, defs: { key: string; label: string }[], ): string { return resolveFn?.(key) ?? defs.find(f => f.key === key)?.label ?? key } /** * Non-modal Properties sheet must not scroll the hub behind it when Radix moves * focus into the panel — but keyboard users still need focus inside the sheet * (column-header filter deep-link). */ export function focusInitialControl(event: Event) { event.preventDefault() const root = event.currentTarget as HTMLElement requestAnimationFrame(() => { const focusHost = root.querySelector("[data-properties-initial-focus]") ?? root const target = focusHost.querySelector( "input:not([disabled]), textarea:not([disabled]), select:not([disabled]), button:not([disabled])", ) ?? focusHost.querySelector("button:not([disabled])") target?.focus() }) } function toggleInSet(prev: Set, id: string): Set { const next = new Set(prev) if (next.has(id)) next.delete(id) else next.add(id) return next } function withoutId(prev: Set, id: string): Set { const next = new Set(prev) next.delete(id) return next } // ───────────────────────────────────────────────────────────────────────────── // Shared building blocks // ───────────────────────────────────────────────────────────────────────────── function IconChip({ icon, className }: { icon: string; className?: string }) { return (