"use client" import { memo, useMemo } from "react" import type { HTMLAttributes, ReactNode } from "react" import { getColumnHeaderLabel, useDataGrid, } from "@/components/reui/data-grid/data-grid" import type { DataGridFeatures } from "@/components/reui/data-grid/data-grid" import { Subscribe } from "@tanstack/react-table" import type { Column } from "@tanstack/react-table" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { ArrowDownIcon, ArrowUpIcon, ChevronsUpDownIcon, CheckIcon, ArrowLeftToLineIcon, ArrowRightToLineIcon, ArrowLeftIcon, ArrowRightIcon, Settings2Icon, PinOffIcon } from "lucide-react" interface DataGridColumnHeaderProps< TData extends object, TValue, > extends HTMLAttributes { column: Column /** When omitted, uses `column.columnDef.meta.headerTitle`, then a string `columnDef.header`, then `column.id`. */ title?: string icon?: ReactNode /** Reserved; pin controls are gated by tableLayout.columnsPinnable + column.getCanPin(). */ pinnable?: boolean filter?: ReactNode visibility?: boolean } function DataGridColumnHeaderInner({ column, title, icon, className, filter, visibility = false, }: DataGridColumnHeaderProps) { const { i18n, isLoading, table, props } = useDataGrid() const resolvedTitle = title ?? getColumnHeaderLabel(column) // TanStack's columnOrder defaults to [] until a consumer seeds it; fall // back to the definition order so Move Left/Right work out of the box. const columnOrderState = table.state.columnOrder const columnOrder = columnOrderState.length > 0 ? columnOrderState : table.getAllLeafColumns().map((leafColumn) => leafColumn.id) const columnVisibilityKey = props.tableLayout?.columnsVisibility && visibility ? JSON.stringify(table.state.columnVisibility) : "" const isSorted = column.getIsSorted() const isPinned = column.getIsPinned() const canSort = column.getCanSort() const canPin = column.getCanPin() const canResize = column.getCanResize() const columnIndex = columnOrder.indexOf(column.id) const canMoveLeft = columnIndex > 0 const canMoveRight = columnIndex < columnOrder.length - 1 const handleSort = () => { if (isSorted === "asc") { column.toggleSorting(true) } else if (isSorted === "desc") { column.clearSorting() } else { column.toggleSorting(false) } } const headerLabelClassName = cn( "text-secondary-foreground/80 inline-flex h-full items-center gap-1.5 font-normal [&_svg]:opacity-60 text-[0.8125rem] leading-[calc(1.125/0.8125)] [&_svg]:size-3.5", className ) const headerButtonClassName = cn( "text-secondary-foreground/80 hover:bg-secondary data-[state=open]:bg-secondary hover:text-foreground data-[state=open]:text-foreground px-2 font-normal h-6 rounded-lg", className ) const sortIcon = canSort && (isSorted === "desc" ? (