import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { SlidersHorizontal, Filter, ChevronDown, X, GripVertical } from "lucide-react"; import type { VisibilityState } from "@tanstack/react-table"; import { useOutsideClick } from "@/lib/hooks/useOutsideClick"; import { cn } from "@/lib/utils"; import { TableSearchInput } from "@/components/tableSearch"; import { CreateButton } from "@/components/createButton"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { InfiniteSelect } from "@/components/infiniteSelect"; // ─── Types ──────────────────────────────────────────────────────────────────── export interface ColumnMeta { id: string; label: string; } export interface FilterField { id: string; label: string; type?: "select" | "text" | "infinite-select"; options?: { value: string; label: string }[]; // Infinite Select props loading?: boolean; isLoadingMore?: boolean; hasNextPage?: boolean; onLoadMore?: () => void; searchValue?: string; onSearchChange?: (value: string) => void; searchPlaceholder?: string; disabled?: boolean; } export interface ActiveFilter { fieldId: string; value: string; } interface DataTableToolbarProps { searchValue: string; onSearchChange: (value: string) => void; searchPlaceholder?: string; columns: ColumnMeta[]; columnVisibility: VisibilityState; onColumnVisibilityChange: (visibility: VisibilityState) => void; columnOrder: string[]; onColumnOrderChange: (order: string[]) => void; filterFields?: FilterField[]; activeFilters?: ActiveFilter[]; onFilterChange?: (fieldId: string, value: string) => void; onFilterReset?: () => void; perPageOptions?: number[]; perPage?: number; onPerPageChange?: (value: number) => void; actionLabel?: string; onAction?: () => void; hideSearch?: boolean; hideAction?: boolean; extraActions?: React.ReactNode; } // ─── Toggle Columns Dropdown ────────────────────────────────────────────────── interface ToggleColumnsDropdownProps { columns: ColumnMeta[]; columnVisibility: VisibilityState; onColumnVisibilityChange: (visibility: VisibilityState) => void; columnOrder: string[]; onColumnOrderChange: (order: string[]) => void; } function ToggleColumnsDropdown({ columns, columnVisibility, onColumnVisibilityChange, columnOrder, onColumnOrderChange, }: ToggleColumnsDropdownProps) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const ref = useRef(null); const dragIdRef = useRef(null); const [dragOverId, setDragOverId] = useState(null); useOutsideClick(ref, () => setOpen(false)); const isVisible = (id: string) => (columnVisibility[id] === undefined ? true : columnVisibility[id]); const toggle = (id: string) => { onColumnVisibilityChange({ ...columnVisibility, [id]: !isVisible(id) }); }; const orderedColumns = [...columns].sort((a, b) => { const ai = columnOrder.indexOf(a.id); const bi = columnOrder.indexOf(b.id); if (ai === -1 && bi === -1) return 0; if (ai === -1) return 1; if (bi === -1) return -1; return ai - bi; }); const handleDragStart = (id: string) => { dragIdRef.current = id; }; const handleDragOver = (e: React.DragEvent, id: string) => { e.preventDefault(); if (dragIdRef.current !== id) setDragOverId(id); }; const handleDrop = (targetId: string) => { const sourceId = dragIdRef.current; if (!sourceId || sourceId === targetId) return; const next = [...columnOrder]; const from = next.indexOf(sourceId); const to = next.indexOf(targetId); const safeFrom = from === -1 ? next.length : from; const safeTo = to === -1 ? next.length : to; next.splice(safeFrom, 1); next.splice(safeTo, 0, sourceId); onColumnOrderChange(next); dragIdRef.current = null; setDragOverId(null); }; const handleDragEnd = () => { dragIdRef.current = null; setDragOverId(null); }; return (
{open && (

{t("table.toggleColumns", "Toggle Columns")}

{orderedColumns.map((col) => (
handleDragStart(col.id)} onDragOver={(e) => handleDragOver(e, col.id)} onDrop={() => handleDrop(col.id)} onDragEnd={handleDragEnd} className={cn( "flex items-center gap-2.5 px-3 py-1.5 transition-colors cursor-default", "hover:bg-muted/40", dragOverId === col.id && "bg-primary/10 border-l-2 border-primary", )} > toggle(col.id)} className="w-4 h-4 rounded cursor-pointer accent-[#18b69b] dark:accent-primary" />
))}
)}
); } // ─── Filter Dropdown ────────────────────────────────────────────────────────── interface FilterDropdownProps { filterFields: FilterField[]; activeFilters: ActiveFilter[]; onFilterChange: (fieldId: string, value: string) => void; onFilterReset: () => void; perPageOptions?: number[]; perPage?: number; onPerPageChange?: (value: number) => void; } function FilterDropdown({ filterFields, activeFilters, onFilterChange, onFilterReset, perPageOptions = [10, 25, 50, 100], perPage = 25, onPerPageChange, }: FilterDropdownProps) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const activeCount = activeFilters.filter((f) => f.value !== "").length; const hasActive = activeCount > 0; const getActiveValue = (fieldId: string) => activeFilters.find((f) => f.fieldId === fieldId)?.value ?? ""; return ( {/* Header */}

{t("table.filters", "Filters")}

{/* Fields */}
{filterFields.map((field) => { const current = getActiveValue(field.id); const isFieldActive = current !== ""; return (
{field.type === "text" ? ( <> onFilterChange(field.id, e.target.value)} placeholder="—" className={cn( "h-8 flex-1 rounded-md border border-input bg-background px-2.5 text-sm text-foreground", "placeholder:text-muted-foreground/50 focus:outline-none focus:ring-1 focus:ring-ring", "transition-colors", isFieldActive && "border-primary", )} /> {isFieldActive && ( )} ) : field.type === "infinite-select" ? (
onFilterChange(field.id, val)} options={field.options} placeholder="—" searchPlaceholder={field.searchPlaceholder || t("common.search", "Gözle...")} disabled={field.disabled} loading={field.loading} isLoadingMore={field.isLoadingMore} hasNextPage={field.hasNextPage} onLoadMore={field.onLoadMore} searchValue={field.searchValue} onSearchChange={field.onSearchChange} />
) : ( <> {isFieldActive && ( )} )}
); })} {onPerPageChange && (
)} {hasActive && ( )}
); } // ─── DataTableToolbar ───────────────────────────────────────────────────────── export function DataTableToolbar({ searchValue, onSearchChange, searchPlaceholder, columns, columnVisibility, onColumnVisibilityChange, columnOrder, onColumnOrderChange, filterFields = [], activeFilters = [], onFilterChange, onFilterReset, perPageOptions, perPage, onPerPageChange, actionLabel, onAction, hideSearch = false, hideAction = false, extraActions, }: DataTableToolbarProps) { return (
{!hideSearch && }
{filterFields.length > 0 && onFilterChange && onFilterReset && ( )} {extraActions} {!hideAction && actionLabel && onAction && }
); }