"use client"; import { format, getDaysInMonth, startOfMonth } from "date-fns"; import { th } from "date-fns/locale"; import { ChevronLeft, ChevronRight, QrCodeIcon as PromptpayIcon, } from "lucide-react"; import Link from "next/link"; import { useMemo } from "react"; import TruemoneyIcon from "#/assets/tmn.webp"; import Image from "@/components/image"; import { SimpleTooltip } from "@/components/tooltip"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { cn } from "@/lib/utils"; type Row = { id: string; name: string; amount: number; message: string | null; created: Date; uid: string | null; method: "tmn" | "pp"; image: boolean; }; const DAYS = ["จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส.", "อา."]; export function CalendarClient({ year, month, rows, total, }: { year: number; month: number; rows: Row[]; total: number; }) { const current = new Date(year, month); const rowsByDay = useMemo(() => { const map: Record = {}; for (const row of rows) { const day = new Date(row.created).getDate(); (map[day] ??= []).push(row); } return map; }, [rows]); const daysInMonth = getDaysInMonth(current); const firstDayOfWeek = startOfMonth(current).getDay(); const monthLabel = format(current, "MMMM yyyy", { locale: th }); const prevY = month === 0 ? year - 1 : year; const prevM = month === 0 ? 11 : month - 1; const nextY = month === 11 ? year + 1 : year; const nextM = month === 11 ? 0 : month + 1; const prevHref = `/donate/admin/calendar?month=${prevY}-${String(prevM + 1).padStart(2, "0")}`; const nextHref = `/donate/admin/calendar?month=${nextY}-${String(nextM + 1).padStart(2, "0")}`; return (
{monthLabel}
รวม{" "} {total.toLocaleString()} {" "} บาท
{DAYS.map((day) => (
{day}
))} {Array.from( { length: firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1 }, (_, index) => `pad-${index + 1}`, ).map((key) => (
))} {Array.from({ length: daysInMonth }, (_, index) => index + 1).map( (day) => { const dayRows = rowsByDay[day] || []; const dayTotal = dayRows.reduce((sum, row) => sum + row.amount, 0); const hasData = dayRows.length > 0; return ( {format( new Date( current.getFullYear(), current.getMonth(), day, ), "d MMM yyyy", { locale: th }, )}
{dayRows.length === 0 && (

ไม่มีโดเนทในวันนี้

)} {dayRows.map((row) => (
{row.method === "pp" ? ( ) : ( truemoney )}
{row.name}
{format(row.created, "HH:mm", { locale: th })} {row.uid ? ` - UID ${row.uid}` : ""}
{row.amount.toLocaleString()} ฿
))}
รวม {dayTotal.toLocaleString()} บาท
); }, )}
); }