gongxiangp2/components/department-dashboard/MonthlyLaunchPeriodFilter.tsx

134 lines
5.4 KiB
TypeScript

'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<HTMLFormElement>) => {
event.preventDefault()
if (!startDate || !endDate || startDate > endDate) return
navigate(new URLSearchParams({ startDate, endDate }))
}
return (
<form
onSubmit={handleSubmit}
aria-label={`${metricLabel}日期筛选`}
className="mt-6 flex flex-col gap-3 rounded-xl border border-slate-200 bg-white p-4 sm:flex-row sm:flex-wrap sm:items-end"
>
<label className="flex min-w-0 flex-1 flex-col gap-1.5 sm:min-w-[180px] sm:flex-none">
<span className="text-xs font-bold text-slate-500">快捷月份</span>
<Select
value={preset}
onChange={(event) => handlePresetChange(event.target.value)}
aria-label="选择统计月份"
className="h-11 w-full rounded-lg border border-slate-300 bg-white px-3 text-sm font-semibold text-slate-800 outline-none transition focus:border-[#1677ff] focus:ring-2 focus:ring-[#1677ff]/20"
>
{options.map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}
<option value="custom">自定义日期</option>
</Select>
</label>
<label className="flex min-w-0 flex-1 flex-col gap-1.5 sm:min-w-[180px] sm:flex-none">
<span className="text-xs font-bold text-slate-500">开始日期</span>
<span className="relative">
<CalendarDays className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" aria-hidden="true" />
<input
type="date"
value={startDate}
max={endDate || undefined}
onChange={(event) => {
setStartDate(event.target.value)
setPreset('custom')
}}
aria-label={`统计开始日期 ${formatDateLabel(startDate)}`}
className="h-11 w-full rounded-lg border border-slate-300 bg-white pl-10 pr-3 text-sm font-semibold text-slate-800 outline-none transition focus:border-[#1677ff] focus:ring-2 focus:ring-[#1677ff]/20"
/>
</span>
</label>
<label className="flex min-w-0 flex-1 flex-col gap-1.5 sm:min-w-[180px] sm:flex-none">
<span className="text-xs font-bold text-slate-500">结束日期</span>
<span className="relative">
<CalendarDays className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" aria-hidden="true" />
<input
type="date"
value={endDate}
min={startDate || undefined}
onChange={(event) => {
setEndDate(event.target.value)
setPreset('custom')
}}
aria-label={`统计结束日期 ${formatDateLabel(endDate)}`}
className="h-11 w-full rounded-lg border border-slate-300 bg-white pl-10 pr-3 text-sm font-semibold text-slate-800 outline-none transition focus:border-[#1677ff] focus:ring-2 focus:ring-[#1677ff]/20"
/>
</span>
</label>
<button
type="submit"
disabled={!startDate || !endDate || startDate > endDate}
className="inline-flex h-11 shrink-0 items-center justify-center gap-2 rounded-lg bg-[#1677ff] px-4 text-sm font-bold text-white transition hover:bg-[#0d63d6] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1677ff] focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:bg-slate-300"
>
<Filter className="h-4 w-4" aria-hidden="true" />应用筛选
</button>
</form>
)
}