"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import SiteShell from "@social/components/SiteShell"; import { socialPath } from "@social/lib/socialPath"; import { socialTx } from "@social/lib/socialTx"; import { getGuestId } from "@social/lib/api"; import { useSocialI18n } from "@social/lib/useSocialI18n"; import { coffeeApi } from "../lib/api"; import type { CoffeeEventType, CoffeeSlot } from "../lib/types"; import { upcomingDays } from "../lib/calendar"; import { RequestError } from "~/lib/client-request"; import { canUseTrial, consumeTrial, resolveTrialLimits, trialPaywallHint, } from "@social/lib/socialTrial"; import { useSocialConfig } from "@social/components/SocialExperience"; import "./coffee.css"; export default function CoffeeBookPage({ username, slug, }: { username: string; slug: string; }) { const { locale } = useSocialI18n(); const zh = locale === "zh"; const router = useRouter(); const config = useSocialConfig(); const trialLimits = resolveTrialLimits(config); const [day, setDay] = useState(""); const [et, setEt] = useState(null); const [metadataRetry, setMetadataRetry] = useState(0); const [slots, setSlots] = useState([]); const [tz, setTz] = useState(""); const [slot, setSlot] = useState(null); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [notes, setNotes] = useState(""); const [err, setErr] = useState(null); const [busy, setBusy] = useState(false); const [loadingSlots, setLoadingSlots] = useState(false); const [slotsFor, setSlotsFor] = useState(""); const slotRequest = useRef(0); const submitting = useRef(false); const hostZone = tz || et?.schedule?.timezone || et?.host?.timezone; const days = useMemo(() => { try { return hostZone ? upcomingDays(14, hostZone) : []; } catch { return []; } }, [hostZone]); const currentSlots = slotsFor === day ? slots : []; useEffect(() => { if (days.length) setDay(current => days.some(d => d.iso === current) ? current : days[0].iso); }, [days]); useEffect(() => { let active = true; setEt(null); setTz(""); setDay(""); void coffeeApi .eventType(username, slug) .then((r) => { if (active) { setEt(r.event_type); setErr(null); } }) .catch((e) => { if (active) setErr(e instanceof Error ? e.message : String(e)); }); return () => { active = false; }; }, [username, slug, metadataRetry]); const loadSlots = useCallback(async (signal?: AbortSignal, preserveError = false) => { if (!day) return; const request = ++slotRequest.current; if (!preserveError) setErr(null); setSlot(null); setLoadingSlots(true); setSlots([]); try { const r = await coffeeApi.slots(username, slug, day, signal); if (signal?.aborted || request !== slotRequest.current) return; setSlots(r.slots); setSlotsFor(day); setTz(r.timezone); } catch (e) { if (signal?.aborted || request !== slotRequest.current) return; setErr(e instanceof Error ? e.message : String(e)); setSlots([]); } finally { if (!signal?.aborted && request === slotRequest.current) setLoadingSlots(false); } }, [username, slug, day]); useEffect(() => { const controller = new AbortController(); void loadSlots(controller.signal); return () => controller.abort(); }, [loadSlots]); async function submit() { if (!slot || !name.trim() || submitting.current || loadingSlots || slotsFor !== day) return; if (!canUseTrial("coffeeBookings", trialLimits)) { setErr(socialTx(trialPaywallHint("coffeeBookings"))); return; } submitting.current = true; setBusy(true); let booked = false; setErr(null); try { const loc = et?.locations?.[0]?.label; const res = await coffeeApi.book({ username, event_slug: slug, start: slot.start, attendee_name: name.trim(), attendee_email: email.trim() || undefined, attendee_device: getGuestId(), location: loc, notes: notes.trim(), }); consumeTrial("coffeeBookings", trialLimits); booked = true; router.push(socialPath(`/coffeechat/bookings/${res.booking.id}`)); } catch (e) { setErr(e instanceof Error ? e.message : String(e)); if (e instanceof RequestError && e.status === 409) void loadSlots(undefined, true); } finally { if (!booked) { setBusy(false); submitting.current = false; } } } return (

{et?.title || slug} @{username}

{zh ? "返回" : "Back"}
{et && (

{et.description} · {et.length_minutes} min {tz ? ` · ${tz}` : ""}

)} {err &&

{err}

} {err && !et && } {!et && !err &&

{zh ? '加载预约信息…' : 'Loading booking details…'}

}

{zh ? "选日期" : "Pick a day"}

{days.map((d) => ( ))}

{zh ? "可约时段" : "Available slots"}

{hostZone &&

{zh ? '以下日期和时段均为主持人当地时间' : 'Dates and times are shown in the host’s time zone'} · {hostZone}

} {loadingSlots &&

{zh ? '正在查找可约时段…' : 'Finding available slots…'}

} {!loadingSlots && day && !currentSlots.length && !err &&

{zh ? "这天没有空档,请换一个日期。" : "No slots this day. Please choose another date."}

} {err && day && }
{currentSlots.map((s) => ( ))}