'use client'; import React, { useEffect, useMemo, useState } from 'react'; import { AD_TRACKER_PRESETS, RangePreset, isoYesterday, resolveAdRange, } from '@/lib/ad-tracker-range'; type Props = { preset: RangePreset; customSince: string; customUntil: string; onPreset: (preset: RangePreset) => void; onCustomChange: (since: string, until: string) => void; /** Compact = fewer labels (for sticky headers). */ compact?: boolean; }; /** * Periodo predefinido + rango libre. * El hasta personalizado no puede pasar de ayer (Hoy solo vía botón «Hoy»). */ export default function DateRangePicker({ preset, customSince, customUntil, onPreset, onCustomChange, compact = false, }: Props) { const yesterday = useMemo(() => isoYesterday(), []); const range = useMemo( () => resolveAdRange(preset, { since: customSince, until: customUntil, }), [preset, customSince, customUntil] ); const [draftSince, setDraftSince] = useState(customSince || range.since); const [draftUntil, setDraftUntil] = useState(customUntil || range.until); useEffect(() => { if (preset === 'custom') { setDraftSince(customSince || range.since); setDraftUntil(customUntil || range.until); } else { setDraftSince(range.since); setDraftUntil(range.until); } }, [preset, customSince, customUntil, range.since, range.until]); function applyCustom() { let s = draftSince || yesterday; let u = draftUntil || yesterday; if (u > yesterday) u = yesterday; if (s > u) s = u; onCustomChange(s, u); onPreset('custom'); } return (
{!compact ? (

Periodo

) : null}
{AD_TRACKER_PRESETS.map((p) => ( ))} {range.label}
{preset === 'custom' ? (

El rango personalizado llega como máximo a ayer. Usa «Hoy» para incluir el día en curso.

) : preset !== 'today' ? (

Datos hasta ayer · «Hoy» incluye el día en curso

) : null}
); }