"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { AccessibleCalendar } from "@suphark/ui/components/shared/date/accessible-calendar"; import { ResponsivePopover, ResponsivePopoverContent, ResponsivePopoverTrigger, useIsResponsiveSheet, } from "@suphark/ui/components/shared/responsive-popover"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Badge } from "@suphark/ui/components/ui/badge"; import { Separator } from "@suphark/ui/components/ui/separator"; import { getDateTimeFormat } from "@suphark/ui/lib/intl-cache"; import { cn } from "@suphark/ui/lib/utils"; import { differenceInCalendarDays, endOfMonth, endOfQuarter, endOfYear, isSameDay, startOfDay, startOfMonth, startOfQuarter, startOfYear, subDays, subMonths, } from "date-fns"; import { CalendarIcon } from "lucide-react"; import { useState } from "react"; import type { DateRange } from "react-day-picker"; export interface DateRangePreset { label: string; /** ช่วงตอนกด (คำนวณใหม่ทุกครั้ง — "วันนี้" ขึ้นกับเวลาที่กด) */ range: () => DateRange; } /** ช่วงด่วนชุดมาตรฐาน (วันนี้, 7/30 วันล่าสุด, เดือนนี้, เดือนก่อน, ไตรมาสนี้, ปีนี้) ตามข้อความใน UiText */ export function defaultRangePresets(text?: Partial>): DateRangePreset[] { const today = () => startOfDay(new Date()); return [ { label: text?.today ?? "วันนี้", range: () => ({ from: today(), to: today() }) }, { label: text?.last7 ?? "7 วันล่าสุด", range: () => ({ from: subDays(today(), 6), to: today() }) }, { label: text?.last30 ?? "30 วันล่าสุด", range: () => ({ from: subDays(today(), 29), to: today() }), }, { label: text?.thisMonth ?? "เดือนนี้", range: () => ({ from: startOfMonth(today()), to: startOfDay(endOfMonth(today())) }), }, { label: text?.lastMonth ?? "เดือนก่อน", range: () => { const m = subMonths(today(), 1); return { from: startOfMonth(m), to: startOfDay(endOfMonth(m)) }; }, }, { label: text?.thisQuarter ?? "ไตรมาสนี้", range: () => ({ from: startOfQuarter(today()), to: startOfDay(endOfQuarter(today())) }), }, { label: text?.thisYear ?? "ปีนี้", range: () => ({ from: startOfYear(today()), to: startOfDay(endOfYear(today())) }), }, ]; } const sameRange = (a: DateRange | undefined, b: DateRange) => !!a?.from && !!a.to && !!b.from && !!b.to && isSameDay(a.from, b.from) && isSameDay(a.to, b.to); interface DateRangePickerProps { value: DateRange | undefined; onValueChange: (range: DateRange | undefined) => void; placeholder?: string; className?: string; triggerClassName?: string; align?: "start" | "end" | "center"; showClear?: boolean; title?: string; variant?: "default" | "outline" | "ghost"; /** * ช่วงด่วนเหนือปฏิทิน (กดแล้วเลือกช่วงและปิด) — ไม่ส่ง = ชุดมาตรฐานไทย (`defaultRangePresets`) · `false` = ไม่แสดง * · ส่ง array ของ `{ label, range: () => DateRange }` เป็นชุดของแอปเอง */ presets?: ReadonlyArray | false; } /** วันที่ในปุ่ม — แบบไทย พ.ศ. เหมือนบรรทัดสรุปในหน้าต่าง (เดิม date-fns "PP" = "Oct 30, 2026" ค.ศ. อังกฤษ) */ const formatDay = (date: Date) => getDateTimeFormat("th-TH", { day: "numeric", month: "short", year: "numeric" }).format(date); export function DateRangePicker({ value, onValueChange, placeholder, className, triggerClassName, align = "start", showClear = true, title, variant = "outline", presets, }: DateRangePickerProps) { const ui = useUiText(); const text = ui.datePicker; const presetText = ui.rangePresets; const [open, setOpen] = useState(false); const presetList = presets === false ? [] : (presets ?? defaultRangePresets(presetText)); const hint = placeholder ?? text?.rangePlaceholder ?? "Pick a date range"; return (
} > {title || (value?.from ? ( value.to ? ( <> {formatDay(value.from)} - {formatDay(value.to)} ) : ( formatDay(value.from) ) ) : ( {hint} ))} {title && value?.from && ( <> 1
{formatDay(value.from)} {value.to && <> - {formatDay(value.to)}}
)}
) : undefined } > {presetList.length > 0 && (
{presetList.map((preset) => { const range = preset.range(); const active = sameRange(value, range); return ( ); })}
)} {value?.from && value.to && (

{value.from.toLocaleDateString("th-TH", { day: "numeric", month: "short" })} –{" "} {value.to.toLocaleDateString("th-TH", { day: "numeric", month: "short", year: "numeric", })}{" "} ·{" "} {(presetText?.days ?? "{count} วัน").replace( "{count}", String(differenceInCalendarDays(value.to, value.from) + 1), )}

)} ); } /** ปฏิทินช่วงวัน: จอกว้างแสดง 2 เดือนคู่กัน · แผ่นล่างบนมือถือแสดงเดือนเดียว (2 เดือนซ้อนสูงเกินจอ) */ function RangeCalendar({ value, onValueChange, }: { value: DateRange | undefined; onValueChange: (range: DateRange | undefined) => void; }) { const sheet = useIsResponsiveSheet(); return ( ); }