import { useState, useRef, useEffect, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { ScanLine, Calendar, ArrowRight } from 'lucide-react'; import { format } from 'date-fns'; import { fr } from 'date-fns/locale/fr'; import { enUS } from 'date-fns/locale/en-US'; import { Button } from '@/components/ui/button'; type Period = 'today' | '7days' | 'custom'; interface HeaderProps { title: string; onScanClick?: () => void; activePeriod?: Period; onPeriodChange?: (period: Period) => void; customStartDate?: string; customEndDate?: string; onCustomDateChange?: (startDate: string, endDate: string) => void; } function computeDateRange(period: Period, customStart?: string, customEnd?: string): { start: Date; end: Date } { const now = new Date(); switch (period) { case 'today': { const midnight = new Date(now); midnight.setHours(0, 0, 0, 0); return { start: midnight, end: now }; } case '7days': { const sevenDaysAgo = new Date(now); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); sevenDaysAgo.setHours(0, 0, 0, 0); return { start: sevenDaysAgo, end: now }; } case 'custom': { if (customStart && customEnd) { // Parse as local date (YYYY-MM-DD) to avoid UTC timezone shift const [sy, sm, sd] = customStart.split('-').map(Number); const [ey, em, ed] = customEnd.split('-').map(Number); return { start: new Date(sy, sm - 1, sd), end: new Date(ey, em - 1, ed) }; } return { start: now, end: now }; } } } function formatDateLabel(date: Date, locale: string): string { const loc = locale === 'fr' ? fr : enUS; return format(date, 'd MMM', { locale: loc }); } export default function Header({ title, onScanClick, activePeriod: controlledPeriod, onPeriodChange, customStartDate, customEndDate, onCustomDateChange, }: HeaderProps) { const { t, i18n } = useTranslation(); const [showDatePicker, setShowDatePicker] = useState(false); const [internalPeriod, setInternalPeriod] = useState('today'); const [internalStart, setInternalStart] = useState(''); const [internalEnd, setInternalEnd] = useState(''); const activePeriod = controlledPeriod ?? internalPeriod; const startDate = customStartDate ?? internalStart; const endDate = customEndDate ?? internalEnd; const handlePeriodChange = (p: Period) => { setInternalPeriod(p); onPeriodChange?.(p); if (p === 'custom') { setShowDatePicker(true); } else { setShowDatePicker(false); } }; const handleCustomDateChange = (start: string, end: string) => { setInternalStart(start); setInternalEnd(end); onCustomDateChange?.(start, end); }; const datePickerRef = useRef(null); useEffect(() => { if (!showDatePicker) return; function handleClick(e: MouseEvent) { if (datePickerRef.current && !datePickerRef.current.contains(e.target as Node)) { setShowDatePicker(false); } } document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); }, [showDatePicker]); const dateRange = useMemo( () => computeDateRange(activePeriod, startDate, endDate), [activePeriod, startDate, endDate] ); const today = format(new Date(), 'yyyy-MM-dd'); return (

{title}

{/* Period filter */}
{/* Custom date picker dropdown */} {showDatePicker && (
handleCustomDateChange(e.target.value, endDate)} className="w-full rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-sm text-slate-900 focus:border-primary focus:ring-1 focus:ring-primary/30 outline-none transition-colors" />
handleCustomDateChange(startDate, e.target.value)} className="w-full rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-sm text-slate-900 focus:border-primary focus:ring-1 focus:ring-primary/30 outline-none transition-colors" />
{startDate && endDate && ( )}
)}
{/* Date range display */}
{formatDateLabel(dateRange.start, i18n.language)} {formatDateLabel(dateRange.end, i18n.language)}
{/* Scan Button */}
); }