diff --git a/src/app/invite/[id]/page.tsx b/src/app/invite/[id]/page.tsx new file mode 100644 index 0000000..c7a65e1 --- /dev/null +++ b/src/app/invite/[id]/page.tsx @@ -0,0 +1,191 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { useParams, useRouter } from "next/navigation"; +import { motion } from "framer-motion"; +import { + Utensils, + Users, + Heart, + Sparkles, + ChevronRight, + Loader2, +} from "lucide-react"; +import { getUserId } from "@/lib/userId"; + +export default function InvitePage() { + const params = useParams<{ id: string }>(); + const router = useRouter(); + const roomId = params.id; + + const [status, setStatus] = useState<"loading" | "ready" | "not_found">( + "loading", + ); + const [userCount, setUserCount] = useState(0); + const [joining, setJoining] = useState(false); + + useEffect(() => { + fetch(`/api/room/${roomId}`) + .then((res) => { + if (!res.ok) throw new Error(); + return res.json(); + }) + .then((data) => { + setUserCount(data.userCount ?? 0); + setStatus("ready"); + }) + .catch(() => setStatus("not_found")); + }, [roomId]); + + const handleJoin = async () => { + setJoining(true); + try { + const userId = getUserId(); + await fetch(`/api/room/${roomId}/join`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ userId }), + }); + router.push(`/room/${roomId}`); + } catch { + setJoining(false); + } + }; + + if (status === "loading") { + return ( +
+ 这个房间已过期或不存在,请让朋友重新分享链接 +
+ ++ 别说随便 +
++ 有人邀请你一起选餐厅 +
+