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 ( +
+
+
+ ); + } + + if (status === "not_found") { + return ( +
+
+ +
+

房间不存在

+

+ 这个房间已过期或不存在,请让朋友重新分享链接 +

+ +
+ ); + } + + return ( +
+ +
+ +
+ +

+ NoWhatever +

+

+ 别说随便 +

+
+ + +

+ 有人邀请你一起选餐厅 +

+
+ + {roomId} + +
+ {userCount > 0 && ( +
+ + + 已有 {userCount} 人在房间 + +
+ )} +
+ + +
+
+ +
+ 加入房间 + + 和朋友一起 + +
+ + + +
+
+ +
+ 各自滑卡 + + 右滑喜欢的店 + +
+ + + +
+
+ +
+ 匹配结果 + + 滑中同一家就去 + +
+
+ + + + +
+ ); +} diff --git a/src/components/TopNav.tsx b/src/components/TopNav.tsx index 0c4db48..a2f2f19 100644 --- a/src/components/TopNav.tsx +++ b/src/components/TopNav.tsx @@ -20,7 +20,7 @@ export default function TopNav({ roomId, userCount }: TopNavProps) { }, []); const handleInvite = useCallback(async () => { - const url = window.location.href; + const url = `${window.location.origin}/invite/${roomId}`; const shareData = { title: "别说随便啦,来滑卡片决定吃什么!", text: "我建好房间了,快点开链接一起选餐厅,滑中同一家就去吃!", @@ -42,7 +42,7 @@ export default function TopNav({ roomId, userCount }: TopNavProps) { } catch { showToast("复制失败,请手动复制链接"); } - }, [showToast]); + }, [roomId, showToast]); return ( <>