import { CheckSquare } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' import toast from 'react-hot-toast' import { useParams } from 'common' import { useOrganizationJoinDeclineMutation } from 'data/organizations/organization-join-decline-mutation' import { useOrganizationJoinMutation } from 'data/organizations/organization-join-mutation' import { TokenInfo, validateTokenInformation, } from 'data/organizations/organization-join-token-validation-query' import { useSignOut } from 'lib/auth' import { useProfile } from 'lib/profile' import { Button, Loading } from 'ui' export const OrganizationInviteOld = () => { const router = useRouter() const signOut = useSignOut() const { profile } = useProfile() const { slug, token, name } = useParams() const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState() const [tokenValidationInfo, setTokenValidationInfo] = useState(undefined) const [tokenInfoLoaded, setTokenInfoLoaded] = useState(false) const { token_does_not_exist, sso_mismatch, email_match, expired_token, organization_name, invite_id, } = tokenValidationInfo || {} const loginRedirectLink = `/?returnTo=${encodeURIComponent(`/join?token=${token}&slug=${slug}`)}` const { mutate: joinOrganization } = useOrganizationJoinMutation({ onSuccess: () => { setIsSubmitting(false) router.push('/') }, onError: (error) => { toast.error(`Failed to join organization: ${error.message}`) setIsSubmitting(false) }, }) const { mutate: declineOrganization } = useOrganizationJoinDeclineMutation({ onSuccess: () => { setIsSubmitting(false) router.push('/') }, onError: (error) => { toast.error(`Failed to decline invitation: ${error.message}`) setIsSubmitting(false) }, }) const isError = error || !!(tokenInfoLoaded && sso_mismatch) || !!(tokenInfoLoaded && token_does_not_exist) || (tokenInfoLoaded && !email_match) || (tokenInfoLoaded && expired_token) const ErrorMessage = () => { const Container = ({ children }: { children: React.ReactNode }) => (
{children}
) const message = error ? (

There was an error requesting details for this invitation. ({error.message})

) : sso_mismatch ? ( <>

SSO providers do not match.

This invitation comes from a different identity provider than the one you are currently logged in with. Please use the same SSO provider instead.

) : token_does_not_exist ? ( <>

The invite token is invalid.

Try copying and pasting the link from the invite email, or ask the organization owner to invite you again.

) : !email_match ? ( <>

Your email address {profile?.primary_email} does not match the email address this invitation was sent to.

To accept this invitation, you will need to{' '} { await signOut() router.reload() }} > sign out {' '} and then sign in or create a new account using the same email address used in the invitation.

) : expired_token ? ( <>

The invite token has expired.

Please request a new one from the organization owner.

) : ( '' ) return isError ? {message} : null } const Content = () => ( <>
<>

You have been invited to join

{organization_name ? ( <>

{name ? name : organization_name ? `${organization_name}` : 'an organization'}

{!token_does_not_exist && (

an organization on Supabase

)} ) : ( <>

{'an organization'}

)} {slug && (

{`organization slug: ${slug}`}

)}
{profile && !isError && (
)} {tokenInfoLoaded && } {!profile && (

You will need to sign in to accept this invitation

)}
) async function handleJoinOrganization() { if (!slug) return console.error('Slug is required') if (!token) return console.error('Token is required') setIsSubmitting(true) joinOrganization({ slug, token }) } async function handleDeclineJoinOrganization() { if (!slug) return console.error('Slug is required') if (!invite_id) return console.error('Invite ID is required') setIsSubmitting(true) declineOrganization({ slug, invited_id: invite_id }) } useEffect(() => { const fetchTokenInfo = async () => { if (!slug) return console.error('Slug is required') if (!token) return console.error('Token is required') try { const response = await validateTokenInformation({ slug, token }) setTokenInfoLoaded(true) setTokenValidationInfo(response) } catch (error) { setError(error) setTokenInfoLoaded(true) } } if (!tokenInfoLoaded && token) { fetchTokenInfo() } /** * if params are empty then redirect * user to the homepage of app */ // if (!slug && !token) router.push('/') }, [token, router.asPath]) return (
Supabase
) }