"use client" import * as React from "react" import type { DataListViewType } from "../../lib/data-list-view" import type { RowHeight } from "../../lib/row-height" import { FloatingSheetPanel, FloatingSheetPanelContent, FloatingSheetPanelToolbar, FloatingSheetPanelWorkflowFooter, } from "../ui/floating-sheet-panel" import { DEFAULT_DATA_LIST_DISPLAY_OPTIONS, type DataListDisplayOptions, } from "../../lib/data-list-display-options" import type { ActiveFilter, SortRule, ConditionalRule, FilterFieldDef, } from "../../lib/table-properties-types" import { PROPERTIES_SHEET_TOOLTIP_Z, PROPERTIES_SHEET_TOOLTIP_Z_INDEX } from "./sheet-portal-z" import { MainPanel, SubPanel, columnLabelFor, focusInitialControl, resolveDrawerProps, resolveSheetPanel, type SheetPanel, } from "./drawer-panels" export interface TablePropertiesDrawerProps { open: boolean onOpenChange: (open: boolean) => void /** * Optional deep-link target. When set (and `open` is true), the drawer * focuses this panel instead of "main" — e.g. the column header's * **Add Conditional Rule** menu item passes `"conditional-rules"` to jump * straight to the conditional-formatting panel. Drawer resets to "main" * automatically when it closes. */ initialPanel?: string | null // Display showGridlines?: boolean onShowGridlinesChange?: (v: boolean) => void rowHeight?: RowHeight onRowHeightChange?: (v: RowHeight) => void pagination?: boolean onPaginationChange?: (v: boolean) => void // Filters activeFilters?: ActiveFilter[] onAddFilter?: (fieldKey: string) => void onUpdateFilter?: (id: string, patch: Partial) => void onRemoveFilter?: (id: string) => void /** How the filter after `leftFilterId` combines with the one above (default "and"). */ getFilterConnector?: (leftFilterId: string) => "and" | "or" onToggleFilterConnector?: (leftFilterId: string) => void filterBarVisible?: boolean onFilterBarVisibleChange?: (v: boolean) => void drawerExpandedFilters?: Set onDrawerExpandedFiltersChange?: React.Dispatch>> totalRows?: number filteredRows?: number // Sort sortRules?: SortRule[] onSortRulesChange?: (rules: SortRule[]) => void onAddSortRule?: (fieldKey: string) => void onRemoveSortRule?: (id: string) => void onToggleSortDir?: (id: string) => void // Columns colOrder?: string[] onColOrderChange?: (order: string[]) => void hiddenCols?: Set onToggleColVisibility?: (key: string) => void onMoveCol?: (key: string, dir: "up" | "down") => void // Group groupBy?: string | null onGroupByChange?: (key: string | null) => void // Sort key for display in main panel primarySortKey?: string // Conditional formatting conditionalRules?: ConditionalRule[] onAddConditionalRule?: (rule: Omit, id?: string) => void onRemoveConditionalRule?: (id: string) => void onUpdateConditionalRule?: (id: string, patch: Partial) => void /** * Filter field defs for drawer + conditional rules. Required — every list-hub * consumer derives this from its column definitions (`columnsToFilterFields`) * and passes it through `drawerToolbarProps` from `HubTable`. There is no * product-specific default in the package. */ filterFields?: FilterFieldDef[] // View type currentView?: DataListViewType onViewChange?: (view: DataListViewType) => void /** * Subset of view types this hub actually implements; when set, the view-type tile grid is * filtered so users cannot switch to an unsupported view from Properties. Defaults to all * registered view types (`DATA_LIST_VIEW_TILES`). */ supportedViewTypes?: readonly DataListViewType[] /** Lifecycle context (e.g. tab filter) — shown in the drawer header */ lifecycleTabLabel?: string /** * Column metadata for the Sort / Group / Columns panels. Required — every * list-hub consumer derives this from its column definitions * (`columnsToFieldDefinitions`) and passes it through `drawerToolbarProps` * from `HubTable`. The drawer never falls back to a default column list. */ fieldDefinitions?: { key: string; label: string; sortable?: boolean }[] resolveColumnLabel?: (key: string) => string /** Shared display options (table + board); persisted at page level. */ displayOptions?: DataListDisplayOptions onDisplayOptionsChange?: (patch: Partial) => void /** * When the active view is Board and more than one entry is provided, shows a control to pick * which field defines swimlane columns (`displayOptions.boardGroupByColumnKey`). */ boardGroupByColumnOptions?: { key: string; label: string }[] /** Optional custom option renderer for filter values (e.g. status chips). */ renderFilterOptionValue?: (fieldKey: string, value: string) => React.ReactNode /** Runtime filter bounds/options derived from hub rows (person lists, numeric ranges). */ filterFieldContext?: Record /** Creation mode for the new 2-step "Add view" flow */ isCreatingNewView?: boolean newViewName?: string onNewViewNameChange?: (name: string) => void onCreateNewView?: () => void /** * Fired when the user dismisses the creation drawer (Cancel button, Esc). * In creation mode this is the cancellation channel — the drawer's * `onOpenChange(false)` also routes here so callers get a single hook for * "creation aborted". When omitted, dismissal falls back to * `onOpenChange(false)`. */ onCancelNewView?: () => void creatingViewTypeLabel?: string /** * Edit-mode rename support — when `onViewNameChange` is set and the drawer is * NOT in creation mode (`!isCreatingNewView`), the main panel renders the same * "Name" input shape as creation mode, bound to `(viewName, onViewNameChange)`. * The drawer just emits; the consumer decides whether to commit on every * keystroke or onBlur. Omit both props to keep the existing read-only header. */ viewName?: string onViewNameChange?: (name: string) => void /** Hub-level counts toggle (global) */ showViewCounts?: boolean onShowViewCountsChange?: (show: boolean) => void } /** Footer shown only while creating a view: Cancel + Create view. */ function CreateViewFooter({ isCreatingNewView, newViewName, onCancel, onCreate, }: { isCreatingNewView: boolean | undefined newViewName: string | undefined onCancel: () => void onCreate: (() => void) | undefined }) { if (!isCreatingNewView) return null return ( onCreate?.()} primaryDisabled={!newViewName?.trim()} primaryIconClassName="fa-light fa-plus text-xs" /> ) } export function TablePropertiesDrawer(props: TablePropertiesDrawerProps) { const p = resolveDrawerProps(props, DEFAULT_DATA_LIST_DISPLAY_OPTIONS) const { open, onOpenChange, isCreatingNewView, onCancelNewView, fieldDefinitions, resolveColumnLabel: resolveColumnLabelProp } = p const [navPanel, setNavPanel] = React.useState("main") const sheetPanel = resolveSheetPanel(open, p.initialPanel, navPanel) const resolveColumnLabel = React.useCallback( (key: string) => columnLabelFor(key, resolveColumnLabelProp, fieldDefinitions), [resolveColumnLabelProp, fieldDefinitions], ) // In creation mode, dismissal (Esc, Cancel) must route through // `onCancelNewView` so the parent resets the ephemeral creation state. // Falls back to `onOpenChange(false)` when no explicit cancel hook is wired. const handleSheetOpenChange = React.useCallback( (next: boolean) => { if (!next) { setNavPanel("main") if (isCreatingNewView && onCancelNewView) { onCancelNewView() return } } onOpenChange(next) }, [isCreatingNewView, onCancelNewView, onOpenChange], ) return ( setNavPanel("main")} backLabel="Back to Properties" tipClassName={PROPERTIES_SHEET_TOOLTIP_Z} tipZIndex={PROPERTIES_SHEET_TOOLTIP_Z_INDEX} /> {sheetPanel === "main" ? ( ) : ( )} handleSheetOpenChange(false)} onCreate={p.onCreateNewView} /> ) }