'use client'; import { memo, useMemo, useCallback, useRef, useEffect, useState } from 'react'; import { Download, Search } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { cn } from '@/lib/utils'; export type FinancialControlOption = { value: string; label: string; }; export type FinancialControlSection = { key: string; label: string; options: FinancialControlOption[]; value: string; onChange: (value: string) => void; }; export type FinancialsToolbarProps = { sections: FinancialControlSection[]; searchValue: string; onSearchChange: (value: string) => void; onExport?: () => void; className?: string; }; // Debounce hook function useDebounce(value: T, delay: number): T { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { const handler = setTimeout(() => { setDebouncedValue(value); }, delay); return () => { clearTimeout(handler); }; }, [value, delay]); return debouncedValue; } function FinancialsToolbarComponent({ sections, searchValue, onSearchChange, onExport, className }: FinancialsToolbarProps) { const [localSearch, setLocalSearch] = useState(searchValue); const debouncedSearch = useDebounce(localSearch, 300); // Sync debounced search to parent useEffect(() => { onSearchChange(debouncedSearch); }, [debouncedSearch, onSearchChange]); // Sync parent search to local (if changed externally) useEffect(() => { setLocalSearch(searchValue); }, [searchValue]); return (
{/* Control sections */}
{sections.map((section) => (
{section.options.map((option) => { const isActive = section.value === option.value; return ( ); })}
))}
{/* Spacer */}
{/* Search and actions */}
setLocalSearch(e.target.value)} inputSize="compact" className="w-48 pl-7" />
{onExport && ( )}
); } export const FinancialsToolbar = memo(FinancialsToolbarComponent);