"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useSearchParams } from "next/navigation"; import { useUser } from "@clerk/nextjs"; import { useInviteLink } from "@/lib/supabase/invite-actions"; import { Button } from "@/components/ui/button"; import { Check, X, Loader2 } from "lucide-react"; export default function InviteJoinPage() { const searchParams = useSearchParams(); const router = useRouter(); const { isLoaded, isSignedIn } = useUser(); const [token, setToken] = useState(null); const [status, setStatus] = useState<"loading" | "joining" | "success" | "error">("loading"); const [error, setError] = useState(""); useEffect(() => { const t = searchParams.get("token"); if (t) setToken(t); else { setStatus("error"); setError("No invite token provided."); } }, [searchParams]); useEffect(() => { if (isLoaded && isSignedIn && token && status === "loading") { handleJoin(); } }, [isLoaded, isSignedIn, token, status]); const handleJoin = async () => { if (!token) return; setStatus("joining"); try { const result = await useInviteLink(token); if (result.success) { setStatus("success"); setTimeout(() => router.push("/home"), 2000); } else { setStatus("error"); setError(result.error || "Failed to join"); } } catch (e: any) { setStatus("error"); setError(e.message || "An unexpected error occurred"); } }; return (
{status === "loading" && ( <>

Checking invite...

Please sign in to accept the invite.

)} {status === "joining" && ( <>

Joining organisation...

)} {status === "success" && ( <>

Welcome aboard!

You've joined the organisation. Redirecting...

)} {status === "error" && ( <>

Invite Error

{error}

)}
); }