Files
PAGIVA/src/components/ThaiDateTimePicker.tsx
T
Loma Bot Ops 7f1a9face1 Add image art direction, Thai date picker on week board, sturdier image download
- Share IMAGE_ART_DIRECTION between editable image prompts and queued post drafts
- Use ThaiDateTimePicker for week board planning; a11y, clear action, mobile layout
- Show planned day in Thai format in the generation queue
- Wait longer for ChatGPT image download and handle the download menu item

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 23:14:13 +07:00

87 lines
6.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useEffect, useId, useMemo, useRef, useState } from "react";
import { Icon } from "./Icon";
const THAI_MONTHS = ["มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"];
const THAI_DAYS = ["อา.", "จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส."];
const pad = (value: number) => String(value).padStart(2, "0");
function parseValue(value: string) {
const match = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}))?$/.exec(value);
return match ? { year: +match[1], month: +match[2] - 1, day: +match[3], hour: +(match[4] ?? 9), minute: +(match[5] ?? 0) } : null;
}
function bangkokToday() {
const parts = new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Bangkok", year: "numeric", month: "2-digit", day: "2-digit" }).formatToParts(new Date());
const get = (type: Intl.DateTimeFormatPartTypes) => Number(parts.find((part) => part.type === type)?.value);
return { year: get("year"), month: get("month") - 1, day: get("day") };
}
export function ThaiDateTimePicker({ value, onChange, dateOnly = false, disabled = false, label = "เลือกวันและเวลา" }: { value: string; onChange: (value: string) => void; dateOnly?: boolean; disabled?: boolean; label?: string }) {
const id = useId();
const headingId = `${id}-heading`;
const rootRef = useRef<HTMLDivElement>(null);
const selected = parseValue(value);
const [today] = useState(() => bangkokToday());
const [open, setOpen] = useState(false);
const [view, setView] = useState(() => ({ year: selected?.year ?? today.year, month: selected?.month ?? today.month }));
useEffect(() => {
const close = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) setOpen(false); };
const escape = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); };
document.addEventListener("pointerdown", close);
document.addEventListener("keydown", escape);
return () => { document.removeEventListener("pointerdown", close); document.removeEventListener("keydown", escape); };
}, []);
const days = useMemo(() => {
const first = new Date(Date.UTC(view.year, view.month, 1)).getUTCDay();
const count = new Date(Date.UTC(view.year, view.month + 1, 0)).getUTCDate();
return Array.from({ length: 42 }, (_, index) => { const day = index - first + 1; return day >= 1 && day <= count ? day : null; });
}, [view]);
const display = selected ? `${selected.day} ${THAI_MONTHS[selected.month]} ${selected.year + 543}${dateOnly ? "" : ` เวลา ${pad(selected.hour)}:${pad(selected.minute)} น.`}` : dateOnly ? "เลือกวันที่" : "เลือกวันและเวลา";
function pickDay(day: number) {
const time = selected ?? { hour: 9, minute: 0 };
onChange(`${view.year}-${pad(view.month + 1)}-${pad(day)}${dateOnly ? "" : `T${pad(time.hour)}:${pad(time.minute)}`}`);
if (dateOnly) setOpen(false);
}
function pickToday() {
const time = selected ?? { hour: 9, minute: 0 };
setView({ year: today.year, month: today.month });
onChange(`${today.year}-${pad(today.month + 1)}-${pad(today.day)}${dateOnly ? "" : `T${pad(time.hour)}:${pad(time.minute)}`}`);
if (dateOnly) setOpen(false);
}
function changeTime(kind: "hour" | "minute", next: number) {
const date = selected ?? { ...today, hour: 9, minute: 0 };
const result = { ...date, [kind]: next };
onChange(`${result.year}-${pad(result.month + 1)}-${pad(result.day)}T${pad(result.hour)}:${pad(result.minute)}`);
}
function moveMonth(offset: number) {
const next = new Date(Date.UTC(view.year, view.month + offset, 1));
setView({ year: next.getUTCFullYear(), month: next.getUTCMonth() });
}
function togglePicker() {
if (!open && selected) setView({ year: selected.year, month: selected.month });
setOpen((current) => !current);
}
return <div className="thai-picker" ref={rootRef} lang="th">
<button type="button" className="thai-picker-trigger num" disabled={disabled} aria-expanded={open} aria-controls={id} aria-haspopup="dialog" aria-label={label} onClick={togglePicker}>
<span className={selected ? "" : "text-ink-4"}>{display}</span><Icon name="calendar" size={19} />
</button>
{open && <div className="thai-picker-popover" id={id} role="dialog" aria-modal="false" aria-labelledby={headingId}>
<div className="thai-picker-head"><button type="button" aria-label="เดือนก่อนหน้า" onClick={() => moveMonth(-1)}>‹</button><strong id={headingId}>{THAI_MONTHS[view.month]} {view.year + 543}</strong><button type="button" aria-label="เดือนถัดไป" onClick={() => moveMonth(1)}>›</button></div>
<div className="thai-picker-week" aria-hidden="true">{THAI_DAYS.map((day) => <span key={day}>{day}</span>)}</div>
<div className="thai-picker-grid">{days.map((day, index) => day ? <button type="button" key={index} aria-label={`${day} ${THAI_MONTHS[view.month]} ${view.year + 543}`} aria-pressed={selected?.year === view.year && selected.month === view.month && selected.day === day} data-today={today.year === view.year && today.month === view.month && today.day === day || undefined} onClick={() => pickDay(day)}>{day}</button> : <span key={index} />)}</div>
{!dateOnly && <div className="thai-picker-time"><Icon name="clock" size={17} /><span>เวลาไทย</span><select aria-label="ชั่วโมง" value={selected?.hour ?? 9} onChange={(event) => changeTime("hour", +event.target.value)}>{Array.from({ length: 24 }, (_, hour) => <option key={hour} value={hour}>{pad(hour)}</option>)}</select><span>:</span><select aria-label="นาที" value={selected?.minute ?? 0} onChange={(event) => changeTime("minute", +event.target.value)}>{Array.from({ length: 60 }, (_, minute) => <option key={minute} value={minute}>{pad(minute)}</option>)}</select><span>น.</span></div>}
<div className="thai-picker-foot">
<button type="button" className="thai-picker-text-action" onClick={pickToday}>วันนี้</button>
{selected && <button type="button" className="thai-picker-text-action" onClick={() => { onChange(""); setOpen(false); }}>ล้างวันที่</button>}
{!dateOnly && <button type="button" className="btn btn-primary btn-sm" onClick={() => setOpen(false)}>เรียบร้อย</button>}
</div>
</div>}
</div>;
}