import { useEffect, useState } from 'react' import { useNavigate, useParams } from 'react-router-dom' import { toast } from 'react-toastify' import { useAuth } from '#components/Auth/useAuth' import { useMyProfile } from '#components/Map/hooks/useMyProfile' import { MapOverlayPage } from '#components/Templates/MapOverlayPage' import type { FullItemsApi } from '#types/FullItemsApi' import type { InviteApi } from '#types/InviteApi' import type { Item } from '#types/Item' interface Props { inviteApi: InviteApi itemsApi: FullItemsApi } /** * @category Onboarding */ export function InvitePage({ inviteApi, itemsApi }: Props) { const { isAuthenticated, isInitialized: isAuthenticationInitialized } = useAuth() const { id } = useParams<{ id: string }>() const navigate = useNavigate() const { myProfile, isMyProfileLoaded } = useMyProfile() if (!id) throw new Error('Invite ID is required') const [invitingProfile, setInvitingProfile] = useState(null) useEffect(() => { async function redeemInvite() { if (!id) throw new Error('Invite ID is required') if (!isMyProfileLoaded) return if (!myProfile) { toast.error('Could not find your profile to redeem the invite.') return } const invitingProfileId = await inviteApi.redeemInvite(id, myProfile.id) if (invitingProfileId) { toast.success('Invite redeemed successfully!') navigate(`/item/${invitingProfileId}`) } else { toast.error('Failed to redeem invite') navigate('/') } } async function validateInvite() { if (!id) throw new Error('Invite ID is required') const invitingProfileId = await inviteApi.validateInvite(id) if (!invitingProfileId) { toast.error('Invalid invite code') navigate('/') return } const invitingProfile = await itemsApi.getItem(invitingProfileId) if (!invitingProfile) { toast.error('Inviting profile not found') navigate('/') return } setInvitingProfile(invitingProfile) } if (!isAuthenticationInitialized) return if (isAuthenticated) { void redeemInvite() } else { // Save invite code in local storage localStorage.setItem('inviteCode', id) void validateInvite() } }, [ id, isAuthenticated, inviteApi, navigate, isAuthenticationInitialized, myProfile, isMyProfileLoaded, itemsApi, ]) const goToSignup = () => { navigate('/signup') } const goToLogin = () => { navigate('/login') } return (

Invitation

{invitingProfile ? (

Welcome to Utopia!

You have been invited by: {invitingProfile.name} to join the Utopia community.

{!isAuthenticated && ( )}
) : (

Validating invite...

)}
) }