"use client"; import { useState, useRef, useEffect } from "react"; import Image from "next/image"; import { HiX } from "react-icons/hi"; import { HiOutlineCalendarDays } from "react-icons/hi2"; import { toast } from "sonner"; import { cn, satsToBtc } from "@/lib/utils"; import { randomId } from "@/lib/nostr"; import { unixTimeNowInSeconds } from "@/lib/nostr/dates"; import { addMinutesToDate, toUnix, convertToTimezone } from "@/lib/utils/dates"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { DatePicker } from "@/components/ui/date-picker"; import { TimePicker } from "@/components/ui/time-picker"; import { TimezoneSelector } from "@/components/ui/timezone"; import { Label } from "@/components/ui/label"; import Picker from "@/components/FormComponents/Picker"; import { Switch } from "@/components/ui/switch"; import SmallCalendarIcon from "@/components/EventIcons/DateIcon"; import LocationIcon from "@/components/EventIcons/LocationIcon"; import LocationSearchInput from "@/components/LocationSearch"; import Spinner from "../spinner"; import useAutosizeTextArea from "@/lib/hooks/useAutoSizeTextArea"; import { useModal } from "@/app/_providers/modal/provider"; import { useRouter } from "next/navigation"; import { createEvent, updateList } from "@/lib/actions/create"; import { useNDK } from "@/app/_providers/ndk"; import useCurrentUser from "@/lib/hooks/useCurrentUser"; import useImageUpload from "@/lib/hooks/useImageUpload"; import { NDKEvent } from "@nostr-dev-kit/ndk"; import { getTagValues } from "@/lib/nostr/utils"; type CreateCalendarEventModalProps = { calendar?: string; }; export default function CreateCalendarEventModal({ calendar: _calendar, }: CreateCalendarEventModalProps) { const modal = useModal(); const now = new Date(new Date().setHours(12, 0, 0, 0)); const [isLoading, setIsLoading] = useState(false); const { ImageUploadButton, clear, imagePreview, imageUrl, status: imageStatus, } = useImageUpload("event"); const [error, setError] = useState>({}); const [title, setTitle] = useState(""); const [description, setDescription] = useState(""); const [tickets, setTickets] = useState(false); const [price, setPrice] = useState(1000); const [startDate, setStartDate] = useState(now); const startTime = `${ startDate?.getHours().toLocaleString().length === 1 ? "0" + startDate?.getHours().toLocaleString() : startDate?.getHours() }:${ startDate?.getMinutes().toLocaleString().length === 1 ? "0" + startDate?.getMinutes().toLocaleString() : startDate?.getMinutes() }`; const [endDate, setEndDate] = useState(addMinutesToDate(now, 60)); const endTime = `${ endDate?.getHours().toLocaleString().length === 1 ? "0" + endDate?.getHours().toLocaleString() : endDate?.getHours() }:${ endDate?.getMinutes().toLocaleString().length === 1 ? "0" + endDate?.getMinutes().toLocaleString() : endDate?.getMinutes() }`; const [timezone, setTimezone] = useState( Intl.DateTimeFormat().resolvedOptions().timeZone, ); const [calendar, setCalendar] = useState(_calendar ?? ""); const [location, setLocation] = useState<{ address: string; name: string; coordinates: { lat: number; lng: number }; geohash: string; }>(); const { ndk } = useNDK(); const { currentUser, calendars } = useCurrentUser(); const router = useRouter(); useEffect(() => { if (error["title"]) { if (title) { setError((prev) => ({ ...prev, title: undefined, })); } } }, [title, error]); async function handleSubmit() { if (!ndk || !currentUser) { alert("MISSING"); return; } if (!title) { setError({ title: "Please add an event name", }); return; } setIsLoading(true); try { const random = randomId(); const tags: string[][] = [ ["d", random], ["title", title], ["description", description], ["start", toUnix(convertToTimezone(startDate, timezone)).toString()], ["end", toUnix(convertToTimezone(endDate, timezone)).toString()], ["start_tzid", timezone], ["p", currentUser.pubkey, "", "host"], ]; if (location) { tags.push([ "location", `${location.name}, ${location.address}`, location.name, location.address, ]); tags.push([ "address", `${location.name}, ${location.address}`, location.name, location.address, ]); tags.push(["g", location.geohash]); } if (imageUrl) { tags.push(["image", imageUrl]); } if (tickets) { tags.push(["tickets", "true"]); if (price) { tags.push(["price", satsToBtc(price).toString(), "btc"]); } } const preEvent = { content: description, pubkey: currentUser.pubkey, created_at: unixTimeNowInSeconds(), tags: tags, kind: 31923, }; const event = await createEvent(ndk, preEvent); if (event) { const encodedEvent = event.tagId(); if (calendar) { const selectedCalendar = Array.from(calendars) .find((option) => option.tagId() === calendar) ?.rawEvent(); if (selectedCalendar) { console.log("selectedCalendar", selectedCalendar); await updateList(ndk, selectedCalendar, [["a", encodedEvent]]); } } toast.success("Event Created!"); modal?.hide(); router.push(`/event/${event.encode()}`); } else { toast.error("An error occured"); } } catch (err) { console.log("error creating event", err); } finally { setIsLoading(false); } } useEffect(() => { if (startDate && endDate) { if (startDate.getTime() > endDate.getTime()) { setEndDate(addMinutesToDate(startDate, 60)); } } }, [startDate]); const titleRef = useRef(null); useAutosizeTextArea(titleRef.current, title); return (
{/* Event Name */}