Skip to content

The open alternative to Salesforce, designed for AI.

3.8 KB
Raw Blame
import { SubTitle } from '@/auth/components/SubTitle';
import { OnboardingAnimatedReveal } from '@/onboarding/components/OnboardingAnimatedReveal';
import { OnboardingVerifyLayout } from '@/onboarding/components/OnboardingVerifyLayout';
import { useOnboardingMotionTransition } from '@/onboarding/hooks/useOnboardingMotionTransition';
import { useShowWelcomeAnimationAfterOnboardingCheckout } from '@/onboarding/hooks/useShowWelcomeAnimationAfterOnboardingCheckout';
import { useLoadCurrentUser } from '@/users/hooks/useLoadCurrentUser';
import { styled } from '@linaria/react';
import { AnimatePresence, motion } from 'framer-motion';
import { useEffect, useState } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { MainButton } from 'twenty-ui/components/input';
import { themeCssVariables } from 'twenty-ui/theme';
import { useLingui } from '@lingui/react/macro';

const SUBSCRIPTION_CONFIRMATION_POLL_INTERVAL_MS = 2000;
const SUBSCRIPTION_CONFIRMATION_MAX_ATTEMPTS = 30;

const StyledRetryButtonContainer = styled.div`
  margin-top: ${themeCssVariables.spacing[8]};
  width: 200px;
`;

export const PaymentSuccess = () => {
  const { t } = useLingui();
  const { loadCurrentUser } = useLoadCurrentUser();
  const showWelcomeAnimationAfterOnboardingCheckout =
    useShowWelcomeAnimationAfterOnboardingCheckout();
  const { enqueueToast } = useToast();
  const [hasTimedOut, setHasTimedOut] = useState(false);
  const [confirmationRunIndex, setConfirmationRunIndex] = useState(0);

  useEffect(() => {
    let attempts = 0;
    let timeoutId: ReturnType<typeof setTimeout> | undefined;
    let cancelled = false;

    const confirmSubscription = async () => {
      if (cancelled) {
        return;
      }

      const refreshedWorkspace = await loadCurrentUser()
        .then(({ workspace }) => workspace)
        .catch(() => null);

      if (cancelled) {
        return;
      }

      const refreshedSubscriptionStatus =
        refreshedWorkspace?.currentBillingSubscription?.status;

      if (isDefined(refreshedSubscriptionStatus)) {
        showWelcomeAnimationAfterOnboardingCheckout();
        return;
      }

      attempts += 1;

      if (attempts >= SUBSCRIPTION_CONFIRMATION_MAX_ATTEMPTS) {
        setHasTimedOut(true);
        enqueueToast({
          status: 'error',
          children: t`We're still waiting for a confirmation from our payment provider (Stripe). Please refresh in a few seconds.`,
        });
        return;
      }

      timeoutId = setTimeout(
        () => void confirmSubscription(),
        SUBSCRIPTION_CONFIRMATION_POLL_INTERVAL_MS,
      );
    };

    void confirmSubscription();

    return () => {
      cancelled = true;
      if (isDefined(timeoutId)) {
        clearTimeout(timeoutId);
      }
    };
    // oxlint-disable-next-line react-hooks/exhaustive-deps
  }, [confirmationRunIndex]);

  const handleRetry = () => {
    setHasTimedOut(false);
    setConfirmationRunIndex((previous) => previous + 1);
  };

  const transition = useOnboardingMotionTransition();

  return (
    <OnboardingVerifyLayout>
      <AnimatePresence mode="wait" initial={false}>
        <motion.div
          key={hasTimedOut ? 'timed-out' : 'confirming'}
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          transition={transition}
        >
          <SubTitle>
            {hasTimedOut
              ? t`We're still waiting for the payment confirmation`
              : t`Confirming your payment`}
          </SubTitle>
        </motion.div>
      </AnimatePresence>
      <OnboardingAnimatedReveal isVisible={hasTimedOut}>
        <StyledRetryButtonContainer>
          <MainButton onClick={handleRetry} fullWidth>{t`Retry`}</MainButton>
        </StyledRetryButtonContainer>
      </OnboardingAnimatedReveal>
    </OnboardingVerifyLayout>
  );
};