"use client"; import { Fragment, memo, useMemo, useCallback, useRef, useEffect, useState, } from "react"; import { Download, Search, BarChart3 } 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; showChart?: boolean; onToggleChart?: () => void; className?: string; }; 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 ControlGroup({ children, showDivider = false, }: { children: React.ReactNode; showDivider?: boolean; }) { return (
{showDivider &&
} {children}
); } function FinancialsToolbarComponent({ sections, searchValue, onSearchChange, onExport, showChart = false, onToggleChart, className, }: FinancialsToolbarProps) { const [localSearch, setLocalSearch] = useState(searchValue); const debouncedSearch = useDebounce(localSearch, 300); useEffect(() => { onSearchChange(debouncedSearch); }, [debouncedSearch, onSearchChange]); useEffect(() => { setLocalSearch(searchValue); }, [searchValue]); const groupedSections = useMemo(() => { const statementKeys = ["surface"]; const periodKeys = ["cadence"]; const modeKeys = ["display"]; const scaleKeys = ["scale"]; return { statement: sections.filter((s) => statementKeys.includes(s.key)), period: sections.filter((s) => periodKeys.includes(s.key)), mode: sections.filter((s) => modeKeys.includes(s.key)), scale: sections.filter((s) => scaleKeys.includes(s.key)), }; }, [sections]); return (
{groupedSections.statement.map((section) => ( {section.options.map((option) => { const isActive = section.value === option.value; return ( ); })} ))} {groupedSections.period.map((section) => ( {section.options.map((option) => { const isActive = section.value === option.value; return ( ); })} ))} {groupedSections.mode.length > 0 && ( {groupedSections.mode.map((section) => ( {section.options.map((option) => { const isActive = section.value === option.value; return ( ); })} ))} )} {groupedSections.scale.map((section) => ( {section.options.map((option) => { const isActive = section.value === option.value; return ( ); })} ))}
setLocalSearch(e.target.value)} inputSize="compact" className="w-40 pl-7" />
{onToggleChart && ( )} {onExport && ( )}
); } export const FinancialsToolbar = memo(FinancialsToolbarComponent);