"use client"; import React from "react"; import { observer } from "mobx-react"; import { useRouter, useSearchParams } from "next/navigation"; import useSWR from "swr"; import { Boxes, Check, Share2, Star, User2, X } from "lucide-react"; // components import { LogoSpinner } from "@/components/common"; import { EmptySpace, EmptySpaceItem } from "@/components/ui/empty-space"; // constants import { WORKSPACE_INVITATION } from "@/constants/fetch-keys"; // helpers import { EPageTypes } from "@/helpers/authentication.helper"; // hooks import { useUser } from "@/hooks/store"; // wrappers import { AuthenticationWrapper } from "@/lib/wrappers"; // services import { WorkspaceService } from "@/services/workspace.service"; // service initialization const workspaceService = new WorkspaceService(); const WorkspaceInvitationPage = observer(() => { // router const router = useRouter(); // query params const searchParams = useSearchParams(); const invitation_id = searchParams.get("invitation_id"); const email = searchParams.get("email"); const slug = searchParams.get("slug"); // store hooks const { data: currentUser } = useUser(); const { data: invitationDetail, error } = useSWR( invitation_id && slug && WORKSPACE_INVITATION(invitation_id.toString()), invitation_id && slug ? () => workspaceService.getWorkspaceInvitation(slug.toString(), invitation_id.toString()) : null ); const handleAccept = () => { if (!invitationDetail) return; workspaceService .joinWorkspace(invitationDetail.workspace.slug, invitationDetail.id, { accepted: true, email: invitationDetail.email, }) .then(() => { if (email === currentUser?.email) { router.push("/invitations"); } else { router.push(`/?${searchParams.toString()}`); } }) .catch((err) => console.error(err)); }; const handleReject = () => { if (!invitationDetail) return; workspaceService .joinWorkspace(invitationDetail.workspace.slug, invitationDetail.id, { accepted: false, email: invitationDetail.email, }) .then(() => { router.push("/"); }) .catch((err) => console.error(err)); }; return (
{invitationDetail && !invitationDetail.responded_at ? ( error ? (

INVITATION NOT FOUND

) : ( ) ) : error || invitationDetail?.responded_at ? ( invitationDetail?.accepted ? ( ) : ( {!currentUser ? ( ) : ( )} ) ) : (
)}
); }); export default WorkspaceInvitationPage;