"use client"; import { useState } from "react"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Checkbox } from "@/components/ui/checkbox"; type Recurrence = { freq: "NONE" | "DAILY" | "WEEKLY" | "MONTHLY"; interval: number; byDay?: string[]; count?: number; until?: string; }; interface Props { value?: string; onChange: (rrule: string | undefined) => void; } export function RecurrencePicker({ value, onChange }: Props) { const [rec, setRec] = useState(() => { // If existing rrule, parse minimally (for simplicity we only rehydrate FREQ and INTERVAL) if (value) { const parts = Object.fromEntries( value.split(";").map((p) => p.split("=")), ); return { freq: parts.FREQ || "NONE", interval: parts.INTERVAL ? Number.parseInt(parts.INTERVAL, 10) : 1, byDay: parts.BYDAY ? parts.BYDAY.split(",") : [], count: parts.COUNT ? Number.parseInt(parts.COUNT, 10) : undefined, until: parts.UNTIL, }; } return { freq: "NONE", interval: 1 }; }); const update = (updates: Partial) => { const newRec = { ...rec, ...updates }; setRec(newRec); if (newRec.freq === "NONE") { onChange(undefined); return; } // Build RRULE string let rrule = `FREQ=${newRec.freq};INTERVAL=${newRec.interval}`; if (newRec.freq === "WEEKLY" && newRec.byDay?.length) { rrule += `;BYDAY=${newRec.byDay.join(",")}`; } if (newRec.count) rrule += `;COUNT=${newRec.count}`; if (newRec.until) rrule += `;UNTIL=${newRec.until.replace(/-/g, "")}T000000Z`; onChange(rrule); }; const toggleDay = (day: string) => { const byDay = rec.byDay || []; const newByDay = byDay.includes(day) ? byDay.filter((d) => d !== day) : [...byDay, day]; update({ byDay: newByDay }); }; const dayLabels = { MO: "Mon", TU: "Tue", WE: "Wed", TH: "Thu", FR: "Fri", SA: "Sat", SU: "Sun", }; return (
{rec.freq !== "NONE" && ( <>
update({ interval: Number.parseInt(e.target.value, 10) || 1 }) } className="w-24" />
{rec.freq === "WEEKLY" && (
{["MO", "TU", "WE", "TH", "FR", "SA", "SU"].map((day) => (
toggleDay(day)} />
))}
)}
update({ count: e.target.value ? Number.parseInt(e.target.value, 10) : undefined, }) } />
update({ until: e.target.value || undefined })} />
)}
); }