import { useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { SlidersHorizontal, Clock, 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 { TableActionButton } from '@/components/tableButton' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' // ─── Types ──────────────────────────────────────────────────────────────────── export interface ColumnMeta { id: string label: string } export interface FilterField { id: string label: string options: { value: string; label: string }[] } 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-primary" />
))}
)}
) } // ─── Date Filter Button ─────────────────────────────────────────────────────── function DateFilterButton() { return ( ) } // ─── 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 (
{/* MonthSelect ile aynı pattern: manuel span + sentinel */}
{/* Field bazlı temizle — MonthSelect'teki X butonu ile aynı */} {isFieldActive && ( )}
) })} {onPerPageChange && (
)} {/* Reset — tüm filtreler, sadece aktif filter varsa göster */} {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 && ( )}
) }