'use client' import Select from '@/components/ui/Select' import { CalendarDays, Filter } from 'lucide-react' import { FormEvent, useEffect, useState } from 'react' import { usePathname, useRouter } from 'next/navigation' import type { MonthlyLaunchMonthOption } from '@/lib/ding-workflow-sync' import { toShanghaiDateInput } from './monthly-launch-period' type PeriodSummary = { monthStart: string monthEnd: string } function formatDateLabel(value: string) { const [year, month, day] = value.split('-') return year && month && day ? `${year}/${month}/${day}` : value } export default function MonthlyLaunchPeriodFilter({ summary, options, metricLabel = '本月上新', }: { summary: PeriodSummary options: MonthlyLaunchMonthOption[] metricLabel?: string }) { const router = useRouter() const pathname = usePathname() const initialStartDate = toShanghaiDateInput(summary.monthStart) const initialEndDate = toShanghaiDateInput(summary.monthEnd, true) const initialPreset = options.find((option) => option.startDate === initialStartDate && option.endDate === initialEndDate)?.value || 'custom' const [startDate, setStartDate] = useState(initialStartDate) const [endDate, setEndDate] = useState(initialEndDate) const [preset, setPreset] = useState(initialPreset) useEffect(() => { setStartDate(initialStartDate) setEndDate(initialEndDate) setPreset(initialPreset) }, [initialEndDate, initialPreset, initialStartDate]) const navigate = (params: URLSearchParams) => { const query = params.toString() router.push(`${pathname}${query ? `?${query}` : ''}`) } const handlePresetChange = (value: string) => { if (value === 'custom') { setPreset('custom') return } const option = options.find((candidate) => candidate.value === value) if (!option) return setStartDate(option.startDate) setEndDate(option.endDate) setPreset(option.value) navigate(new URLSearchParams({ month: option.value })) } const handleSubmit = (event: FormEvent) => { event.preventDefault() if (!startDate || !endDate || startDate > endDate) return navigate(new URLSearchParams({ startDate, endDate })) } return (
) }