"use client";
import dynamic from "next/dynamic";
import { useEffect, useMemo, useState, useTransition } from "react";
import { ReminderPicker } from "@/components/reminder-picker";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { createEvent } from "@/modules/calendar/server/actions";
import { richTextToPlainText } from "@/components/rich-text";
import {
getDefaultEventReminderOffsets,
listCalendars,
type CalendarDto,
} from "@/modules/calendar/server/queries";
const RichTextEditor = dynamic(
() => import("@/components/rich-text/rich-text-editor").then((mod) => mod.RichTextEditor),
{
ssr: false,
loading: () => (
Loading editor…
),
},
);
type Props = {
open: boolean;
onOpenChange: (open: boolean) => void;
};
export function CalendarEventCreateDialog({ open, onOpenChange }: Props) {
return (
);
}
function CalendarEventCreateForm({ onDone }: { onDone: () => void }) {
const [calendars, setCalendars] = useState([]);
const [calendarId, setCalendarId] = useState("");
const [title, setTitle] = useState("");
const [startAt, setStartAt] = useState(() => toInputDateTime(new Date()));
const [endAt, setEndAt] = useState(() => toInputDateTime(new Date(Date.now() + 60 * 60 * 1000)));
const [location, setLocation] = useState("");
const [notes, setNotes] = useState("");
const [reminderOffsets, setReminderOffsets] = useState([30]);
const [isPending, startTransition] = useTransition();
useEffect(() => {
listCalendars().then((rows) => {
setCalendars(rows);
setCalendarId(rows[0]?.id ?? "");
});
getDefaultEventReminderOffsets().then(setReminderOffsets);
}, []);
const calendarItems = useMemo(
() => calendars.map((c) => ({ label: c.name, value: c.id })),
[calendars],
);
function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!calendarId || !title.trim()) return;
startTransition(async () => {
await createEvent({
calendarId,
title: title.trim(),
startAt: new Date(startAt),
endAt: new Date(endAt),
allDay: false,
location: location || null,
notes: richTextToPlainText(notes) ? notes : null,
reminderOffsets,
});
onDone();
});
}
return (
<>
New event
>
);
}
function toInputDateTime(date: Date) {
const offset = date.getTimezoneOffset();
const local = new Date(date.getTime() - offset * 60 * 1000);
return local.toISOString().slice(0, 16);
}