Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence. Co-authored-by: Cursor <cursoragent@cursor.com>
245 lines
9.5 KiB
TypeScript
245 lines
9.5 KiB
TypeScript
"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<CoffeeEventType | null>(null);
|
||
const [metadataRetry, setMetadataRetry] = useState(0);
|
||
const [slots, setSlots] = useState<CoffeeSlot[]>([]);
|
||
const [tz, setTz] = useState("");
|
||
const [slot, setSlot] = useState<CoffeeSlot | null>(null);
|
||
const [name, setName] = useState("");
|
||
const [email, setEmail] = useState("");
|
||
const [notes, setNotes] = useState("");
|
||
const [err, setErr] = useState<string | null>(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 (
|
||
<SiteShell title={socialTx("选择时段")}>
|
||
<div className="cf-root">
|
||
<header className="cf-head">
|
||
<h1>
|
||
{et?.title || slug} <span>@{username}</span>
|
||
</h1>
|
||
<Link className="cf-btn cf-btn-ghost" href={socialPath(`/coffeechat/${username}`)}>
|
||
{zh ? "返回" : "Back"}
|
||
</Link>
|
||
</header>
|
||
{et && (
|
||
<p className="cf-lead" style={{ marginTop: 0 }}>
|
||
{et.description} · {et.length_minutes} min
|
||
{tz ? ` · ${tz}` : ""}
|
||
</p>
|
||
)}
|
||
{err && <p className="cf-error" role="alert">{err}</p>}
|
||
{err && !et && <button type="button" className="cf-btn cf-btn-ghost" onClick={() => setMetadataRetry(value => value + 1)}>{zh ? '重新加载' : 'Retry'}</button>}
|
||
{!et && !err && <p className="cf-hint" role="status">{zh ? '加载预约信息…' : 'Loading booking details…'}</p>}
|
||
|
||
<div className="cf-split">
|
||
<section className="cf-panel">
|
||
<h2 style={{ margin: "0 0 0.75rem", fontFamily: "Syne, sans-serif", fontSize: "1.05rem" }}>
|
||
{zh ? "选日期" : "Pick a day"}
|
||
</h2>
|
||
<div className="cf-days">
|
||
{days.map((d) => (
|
||
<button
|
||
key={d.iso}
|
||
type="button"
|
||
aria-pressed={day === d.iso}
|
||
aria-label={d.iso}
|
||
disabled={busy}
|
||
className={`cf-day${day === d.iso ? " is-active" : ""}`}
|
||
onClick={() => { setSlot(null); setDay(d.iso); }}
|
||
>
|
||
{d.label}
|
||
</button>
|
||
))}
|
||
</div>
|
||
<h2 style={{ margin: "0.5rem 0 0.75rem", fontFamily: "Syne, sans-serif", fontSize: "1.05rem" }}>
|
||
{zh ? "可约时段" : "Available slots"}
|
||
</h2>
|
||
{hostZone && <p className="cf-hint">{zh ? '以下日期和时段均为主持人当地时间' : 'Dates and times are shown in the host’s time zone'} · {hostZone}</p>}
|
||
{loadingSlots && <p className="cf-empty" role="status">{zh ? '正在查找可约时段…' : 'Finding available slots…'}</p>}
|
||
{!loadingSlots && day && !currentSlots.length && !err && <p className="cf-empty">{zh ? "这天没有空档,请换一个日期。" : "No slots this day. Please choose another date."}</p>}
|
||
{err && day && <button type="button" className="cf-btn cf-btn-ghost" disabled={loadingSlots || busy} onClick={() => void loadSlots()}>{zh ? '重新加载时段' : 'Retry slots'}</button>}
|
||
<div className="cf-slots" aria-busy={loadingSlots}>
|
||
{currentSlots.map((s) => (
|
||
<button
|
||
key={s.start}
|
||
type="button"
|
||
aria-pressed={slot?.start === s.start}
|
||
disabled={busy || loadingSlots}
|
||
className={`cf-slot${slot?.start === s.start ? " is-active" : ""}`}
|
||
onClick={() => setSlot(s)}
|
||
>
|
||
{s.local}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</section>
|
||
|
||
<aside className="cf-panel">
|
||
<h2 style={{ margin: "0 0 0.75rem", fontFamily: "Syne, sans-serif", fontSize: "1.05rem" }}>
|
||
{zh ? "确认预约" : "Confirm"}
|
||
</h2>
|
||
<p className="cf-hint">
|
||
{slot
|
||
? `${day} ${slot.local} · ${et?.length_minutes || "—"} min`
|
||
: zh
|
||
? "先选一个时段"
|
||
: "Pick a slot first"}
|
||
</p>
|
||
<form className="cf-form" onSubmit={event => { event.preventDefault(); void submit(); }}>
|
||
<label>
|
||
{zh ? "你的称呼" : "Your name"}
|
||
<input value={name} onChange={(e) => setName(e.target.value)} required maxLength={120} autoComplete="name" disabled={busy} />
|
||
</label>
|
||
<label>
|
||
Email
|
||
<input
|
||
type="email"
|
||
autoComplete="email"
|
||
maxLength={254}
|
||
disabled={busy}
|
||
value={email}
|
||
onChange={(e) => setEmail(e.target.value)}
|
||
placeholder={zh ? '可选' : 'optional'}
|
||
/>
|
||
</label>
|
||
<label>
|
||
{zh ? "备注" : "Notes"}
|
||
<textarea value={notes} maxLength={2000} disabled={busy} onChange={(e) => setNotes(e.target.value)} />
|
||
</label>
|
||
<button
|
||
type="submit"
|
||
className="cf-btn cf-btn-primary"
|
||
disabled={busy || loadingSlots || slotsFor !== day || !slot || !name.trim()}
|
||
>
|
||
{busy ? (zh ? '正在确认…' : 'Confirming…') : (zh ? "确认预约" : "Confirm booking")}
|
||
</button>
|
||
</form>
|
||
</aside>
|
||
</div>
|
||
</div>
|
||
</SiteShell>
|
||
);
|
||
}
|