"use client" import * as React from "react" import type { ColumnDef } from "../data-table/types" import { useTableState } from "../data-table/use-table-state" import { type DataListViewType } from "../../lib/data-list-view" import { DEFAULT_DATA_LIST_DISPLAY_OPTIONS, type DataListDisplayOptions } from "../../lib/data-list-display-options" import type { ConditionalRule } from "../../lib/table-properties-types" /** * The "Add view" creation drawer's ephemeral table state: a second `useTableState` * over no rows, display options and conditional rules, reset whenever creation * starts. Never persisted, so closing and reopening starts from defaults. */ export function useHubCreationBuffer>({ columns, creatingViewType, defaultSort, }: { columns: ColumnDef[] creatingViewType: DataListViewType | null defaultSort: { key: string; dir: "asc" | "desc"; } }) { // ─── 2-step "Add view" creation flow — ephemeral state buffer ───────────── // A SECOND `useTableState` instance dedicated to the creation drawer. It // never sees the real `rows` (the user is configuring a *new* view, not // editing one) and is intentionally NOT persisted — close-and-reopen always // starts from defaults. Always instantiated so we don't violate the rules // of hooks; cheap because `rows: []`. const creationEmptyRows = React.useMemo(() => [], []) const creationState = useTableState(creationEmptyRows, columns, defaultSort) const [creationDisplayOptions, setCreationDisplayOptions] = React.useState( DEFAULT_DATA_LIST_DISPLAY_OPTIONS, ) const patchCreationDisplay = React.useCallback( (patch: Partial) => { setCreationDisplayOptions(prev => ({ ...prev, ...patch })) }, [], ) const [creationConditionalRules, setCreationConditionalRules] = React.useState([]) const addCreationCR = React.useCallback((rule: Omit, id?: string) => { setCreationConditionalRules(prev => [ ...prev, { ...rule, id: id ?? `cr-${Date.now()}` }, ]) }, []) const removeCreationCR = React.useCallback((id: string) => { setCreationConditionalRules(prev => prev.filter(r => r.id !== id)) }, []) const updateCreationCR = React.useCallback( (id: string, patch: Partial) => { setCreationConditionalRules(prev => prev.map(r => (r.id === id ? { ...r, ...patch } : r))) }, [], ) // Default colOrder for reset — mirror `useTableState`'s initializer so the // creation drawer's "Columns" panel returns to the same baseline order as a // fresh hook mount. const columnKeysList = React.useMemo(() => columns.map(c => c.key), [columns]) // Keep refs to the reset targets so the effect below doesn't need to depend // on every individual setter (which is stable anyway, but listing them all // would balloon the deps array and obscure intent). const creationStateRef = React.useRef(creationState) React.useLayoutEffect(() => { creationStateRef.current = creationState }) const creationColKeysRef = React.useRef(columnKeysList) React.useLayoutEffect(() => { creationColKeysRef.current = columnKeysList }) const resetCreationBuffer = React.useCallback(() => { const s = creationStateRef.current s.setSheetOpen(false) s.setSortRules([]) for (const f of s.activeFilters) { s.removeFilter(f.id) } s.setColOrder(creationColKeysRef.current) s.setHiddenCols(new Set()) s.setGroupBy(null) s.setSearch("") s.setFilterBarVisible(true) s.setSearchOpen(false) s.setShowGridlines(true) setCreationDisplayOptions(DEFAULT_DATA_LIST_DISPLAY_OPTIONS) setCreationConditionalRules([]) }, []) React.useEffect(() => { if (!creatingViewType) return creationStateRef.current.setSheetOpen(true) return () => { resetCreationBuffer() } }, [creatingViewType, resetCreationBuffer]) return { addCreationCR, creationConditionalRules, creationDisplayOptions, creationState, patchCreationDisplay, removeCreationCR, updateCreationCR, } }