import { useT } from "@agent-native/core/client/i18n"; import { IconSend, IconChevronDown, IconCalendar } from "@tabler/icons-react"; import { useState, useRef } from "react"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; interface SendLaterButtonProps { onSend: () => void; onSendLater: (runAt: number) => void; disabled?: boolean; isSending?: boolean; } function getPresets(): Array<{ labelKey: string; date: Date }> { const now = new Date(); const presets: Array<{ labelKey: string; date: Date }> = []; const laterToday = new Date(now); laterToday.setHours(Math.max(now.getHours() + 4, 18), 0, 0, 0); if (laterToday.getDate() === now.getDate()) { presets.push({ labelKey: "mail.sendLater.laterToday", date: laterToday }); } const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); tomorrow.setHours(8, 0, 0, 0); presets.push({ labelKey: "mail.sendLater.tomorrowMorning", date: tomorrow, }); const nextWeek = new Date(now); const daysUntilMon = (1 - now.getDay() + 7) % 7 || 7; nextWeek.setDate(now.getDate() + daysUntilMon); nextWeek.setHours(8, 0, 0, 0); presets.push({ labelKey: "mail.sendLater.nextWeek", date: nextWeek }); return presets; } function formatDate(date: Date): string { return date.toLocaleString("en-US", { weekday: "short", month: "short", day: "numeric", hour: "numeric", minute: "2-digit", }); } export function SendLaterButton({ onSend, onSendLater, disabled, isSending, }: SendLaterButtonProps) { const t = useT(); const [open, setOpen] = useState(false); const presets = getPresets(); const dateInputRef = useRef(null); const handleSendLater = (date: Date) => { onSendLater(date.getTime()); setOpen(false); }; return (
{t("mail.sendLater.scheduleSend")}
{presets.map((preset) => ( ))}
{ const ms = new Date(e.target.value).getTime(); if (!isNaN(ms) && ms > Date.now()) { handleSendLater(new Date(ms)); } }} />
); }