"use client" import * as React from "react" import { cn } from "../../lib/utils" import { Button } from "../ui/button" import { Input } from "../ui/input" import { Tip } from "../ui/tip" import { FilterDateCalendar } from "../data-table/filter-date-calendar" import { FilterDateRangeControl } from "../data-table/filter-date-range-control" import { FilterPersonOptionList } from "../data-table/filter-person-option-list" import { FilterRangeControl } from "../data-table/filter-range-control" import { FilterTextValueInput } from "../data-table/filter-text-value-input" import type { FilterFieldRuntimeContext } from "../../lib/column-filter-context" import { type ActiveFilter, type ConditionalRule, type FilterFieldDef, type FilterOperator, OPERATOR_LABELS, FILTER_TEXT_MASK_FORMAT_HINT, } from "../../lib/table-properties-types" import { PROPERTIES_SHEET_TOOLTIP_Z, PROPERTIES_SHEET_TOOLTIP_Z_INDEX } from "./sheet-portal-z" import { PropertiesCardActions } from "./properties-card-icon-action" import { ConditionalRuleColorPicker } from "./conditional-rule-color-picker" type DrawerFilterCardBaseProps = { fieldDef: FilterFieldDef fieldContext?: FilterFieldRuntimeContext expanded: boolean onToggleExpand: () => void onRemove: (id: string) => void renderOptionLabel?: (value: string) => React.ReactNode } export type DrawerFilterCardProps = | (DrawerFilterCardBaseProps & { variant?: "filter" filter: ActiveFilter onUpdate: (id: string, patch: Partial) => void }) | (DrawerFilterCardBaseProps & { variant: "conditional" filter: ConditionalRule onUpdate: (id: string, patch: Partial) => void }) type FilterCardValue = ActiveFilter | ConditionalRule type UpdateFn = (id: string, patch: any) => void // eslint-disable-line @typescript-eslint/no-explicit-any const IS_ONLY_OPERATOR_TYPES = new Set(["select", "date", "date-range"]) /** Select / date filters only offer `is` / `is_not`; anything else collapses to `is`. */ function effectiveOperatorFor(fieldDef: FilterFieldDef, operator: FilterOperator): FilterOperator { const clamp = IS_ONLY_OPERATOR_TYPES.has(fieldDef.type) && operator !== "is" && operator !== "is_not" return clamp ? "is" : operator } /** Whether the card has a value that narrows rows (drives the Clear selection footer). */ function filterHasSelection(type: FilterFieldDef["type"], values: string[]): boolean { if (type === "date-range") return Boolean(values[0] || values[1]) if (type === "range") return values.length >= 2 return values.length > 0 } function toggledValues(values: string[], val: string): string[] { return values.includes(val) ? values.filter(v => v !== val) : [...values, val] } /** Keyboard keys the option listbox handles, mapped to the target index. */ function optionKeyTarget(key: string, index: number, count: number): number | null { const targets: Record = { ArrowDown: index + 1, ArrowUp: index - 1, Home: 0, End: count - 1, } return Object.prototype.hasOwnProperty.call(targets, key) ? targets[key] : null } /** Roving tabindex over the option list (same pattern as `ViewSegmentedControl`). */ function useRovingOptions(optionCount: number, resetKey: string) { const optionRefs = React.useRef<(HTMLDivElement | null)[]>([]) const [activeOptionIndex, setActiveOptionIndex] = React.useState(0) const activeIndex = activeOptionIndex < optionCount ? activeOptionIndex : 0 React.useEffect(() => { setActiveOptionIndex(0) }, [resetKey]) const moveActiveOption = React.useCallback( (index: number) => { if (optionCount === 0) return const i = ((index % optionCount) + optionCount) % optionCount setActiveOptionIndex(i) requestAnimationFrame(() => { optionRefs.current[i]?.focus() }) }, [optionCount], ) const onOptionKeyDown = React.useCallback( (e: React.KeyboardEvent, index: number) => { const target = optionKeyTarget(e.key, index, optionCount) if (target === null) return e.preventDefault() moveActiveOption(target) }, [moveActiveOption, optionCount], ) return { optionRefs, activeIndex, onOptionKeyDown } } type OptionItem = { value: string; label: string } interface SelectOptionsEditorProps { label: string options: OptionItem[] filteredOpts: OptionItem[] showSearch: boolean optSearch: string onOptSearchChange: (q: string) => void selectedValueSet: ReadonlySet activeIndex: number optionRefs: React.MutableRefObject<(HTMLDivElement | null)[]> onOptionKeyDown: (e: React.KeyboardEvent, index: number) => void onToggle: (value: string) => void renderOptionLabel?: (value: string) => React.ReactNode } function OptionCheckbox({ checked }: { checked: boolean }) { return (