'use client'; //=========================================== // THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template //=========================================== import { KnownErrors } from "@hexclave/shared"; import { runAsynchronouslyWithAlert } from "@hexclave/shared/dist/utils/promises"; import { use } from "@hexclave/shared/dist/utils/react"; import { Typography } from "@hexclave/ui"; import React from "react"; import { MessageCard, StackClientApp, useStackApp, useUser } from ".."; import { PredefinedMessageCard } from "../components/message-cards/predefined-message-card"; import { useTranslation } from "../lib/translations"; import { cacheTeamInvitationOperation } from "./team-invitation-cache"; // Both endpoints inspect the current account, so the session must participate in // the key even though the SDK app object and invitation code are otherwise stable. const cachedVerifyInvitation = cacheTeamInvitationOperation(async (hexclaveApp: StackClientApp, code: string) => { return await hexclaveApp.verifyTeamInvitationCode(code); }); const cachedGetInvitationDetails = cacheTeamInvitationOperation(async (hexclaveApp: StackClientApp, code: string) => { return await hexclaveApp.getTeamInvitationDetails(code); }); function TeamInvitationInner(props: { fullPage?: boolean, searchParams: Record, session: object }) { const { t } = useTranslation(); const hexclaveApp = useStackApp(); const [success, setSuccess] = React.useState(false); const [errorMessage, setErrorMessage] = React.useState(null); const details = use(cachedGetInvitationDetails(hexclaveApp, props.session, props.searchParams.code || '')); if (errorMessage || details.status === 'error') { return ( ); } if (success) { return ( hexclaveApp.redirectToHome()} > You have successfully joined {details.data.teamDisplayName} ); } return ( runAsynchronouslyWithAlert(async () => { const result = await hexclaveApp.acceptTeamInvitation(props.searchParams.code || ''); if (result.status === 'error') { setErrorMessage(result.error.message); } else { setSuccess(true); } })} secondaryButtonText={t('Ignore')} secondaryAction={() => hexclaveApp.redirectToHome()} > You are invited to join {details.data.teamDisplayName} ); } export function TeamInvitation({ fullPage=false, searchParams }: { fullPage?: boolean, searchParams: Record }) { const { t } = useTranslation(); // Include restricted users to detect if user needs to complete onboarding const user = useUser({ includeRestricted: true }); const hexclaveApp = useStackApp(); const invalidJsx = ( {t('Please double check if you have the correct team invitation link.')} ); const expiredJsx = ( {t('Your team invitation link has expired. Please request a new team invitation link ')} ); const usedJsx = ( {t('This team invitation link has already been used.')} ); const code = searchParams.code; if (!code) { return invalidJsx; } if (!user) { return ( hexclaveApp.redirectToSignIn()} secondaryButtonText={t('Cancel')} secondaryAction={() => hexclaveApp.redirectToHome()} > {t('Sign in or create an account to join the team.')} ); } // User is restricted (needs to complete onboarding) - redirect to onboarding if (user.isRestricted) { return ( hexclaveApp.redirectToOnboarding()} secondaryButtonText={t('Cancel')} secondaryAction={() => hexclaveApp.redirectToHome()} > {t('Please complete your account setup before joining teams.')} ); } const verificationResult = use(cachedVerifyInvitation(hexclaveApp, user._internalSession, searchParams.code || '')); if (verificationResult.status === 'error') { const error = verificationResult.error; if (KnownErrors.VerificationCodeNotFound.isInstance(error)) { return invalidJsx; } else if (KnownErrors.VerificationCodeExpired.isInstance(error)) { return expiredJsx; } else if (KnownErrors.VerificationCodeAlreadyUsed.isInstance(error)) { return usedJsx; } else { throw error; } } return ( ); };