Fixes https://github.com/twentyhq/core-team-issues/issues/191 - remove automatic redirection on payment success page when subscription status is undefined - add an effect component to refresh the subscription status on payment success page Observation: Locally, I had to delay the stripe webhook subscription created endpoint by 7s to see race condition issue https://github.com/user-attachments/assets/463e1816-34fd-4c4f-b590-3994a3a3e91a
123 lines
3.4 KiB
TypeScript
123 lines
3.4 KiB
TypeScript
import { useIsLogged } from '@/auth/hooks/useIsLogged';
|
|
import { useDefaultHomePagePath } from '@/navigation/hooks/useDefaultHomePagePath';
|
|
import { useOnboardingStatus } from '@/onboarding/hooks/useOnboardingStatus';
|
|
import { AppPath } from '@/types/AppPath';
|
|
import { SettingsPath } from '@/types/SettingsPath';
|
|
import { useSubscriptionStatus } from '@/workspace/hooks/useSubscriptionStatus';
|
|
import { OnboardingStatus, SubscriptionStatus } from '~/generated/graphql';
|
|
import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';
|
|
|
|
export const usePageChangeEffectNavigateLocation = () => {
|
|
const isMatchingLocation = useIsMatchingLocation();
|
|
const isLoggedIn = useIsLogged();
|
|
const onboardingStatus = useOnboardingStatus();
|
|
const subscriptionStatus = useSubscriptionStatus();
|
|
const { defaultHomePagePath } = useDefaultHomePagePath();
|
|
|
|
const isMatchingOpenRoute =
|
|
isMatchingLocation(AppPath.Invite) ||
|
|
isMatchingLocation(AppPath.ResetPassword);
|
|
|
|
const isMatchingOngoingUserCreationRoute =
|
|
isMatchingOpenRoute ||
|
|
isMatchingLocation(AppPath.SignInUp) ||
|
|
isMatchingLocation(AppPath.Verify);
|
|
|
|
const isMatchingOnboardingRoute =
|
|
isMatchingOngoingUserCreationRoute ||
|
|
isMatchingLocation(AppPath.CreateWorkspace) ||
|
|
isMatchingLocation(AppPath.CreateProfile) ||
|
|
isMatchingLocation(AppPath.SyncEmails) ||
|
|
isMatchingLocation(AppPath.InviteTeam) ||
|
|
isMatchingLocation(AppPath.PlanRequired) ||
|
|
isMatchingLocation(AppPath.PlanRequiredSuccess);
|
|
|
|
if (isMatchingOpenRoute) {
|
|
return;
|
|
}
|
|
|
|
if (!isLoggedIn && !isMatchingOngoingUserCreationRoute) {
|
|
return AppPath.SignInUp;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.PlanRequired &&
|
|
!isMatchingLocation(AppPath.PlanRequired) &&
|
|
!isMatchingLocation(AppPath.PlanRequiredSuccess)
|
|
) {
|
|
return AppPath.PlanRequired;
|
|
}
|
|
|
|
if (
|
|
subscriptionStatus === SubscriptionStatus.Unpaid &&
|
|
!isMatchingLocation(AppPath.SettingsCatchAll)
|
|
) {
|
|
return `${AppPath.SettingsCatchAll.replace('/*', '')}/${
|
|
SettingsPath.Billing
|
|
}`;
|
|
}
|
|
|
|
if (
|
|
subscriptionStatus === SubscriptionStatus.Canceled &&
|
|
!(
|
|
isMatchingLocation(AppPath.SettingsCatchAll) ||
|
|
isMatchingLocation(AppPath.PlanRequired)
|
|
)
|
|
) {
|
|
return `${AppPath.SettingsCatchAll.replace('/*', '')}/${
|
|
SettingsPath.Billing
|
|
}`;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.WorkspaceActivation &&
|
|
!isMatchingLocation(AppPath.CreateWorkspace) &&
|
|
!isMatchingLocation(AppPath.PlanRequiredSuccess)
|
|
) {
|
|
return AppPath.CreateWorkspace;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.ProfileCreation &&
|
|
!isMatchingLocation(AppPath.CreateProfile)
|
|
) {
|
|
return AppPath.CreateProfile;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.SyncEmail &&
|
|
!isMatchingLocation(AppPath.SyncEmails)
|
|
) {
|
|
return AppPath.SyncEmails;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.InviteTeam &&
|
|
!isMatchingLocation(AppPath.InviteTeam)
|
|
) {
|
|
return AppPath.InviteTeam;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.Completed &&
|
|
subscriptionStatus === SubscriptionStatus.Canceled &&
|
|
isMatchingLocation(AppPath.PlanRequired)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.Completed &&
|
|
isMatchingOnboardingRoute &&
|
|
isLoggedIn
|
|
) {
|
|
return defaultHomePagePath;
|
|
}
|
|
|
|
if (isMatchingLocation(AppPath.Index) && isLoggedIn) {
|
|
return defaultHomePagePath;
|
|
}
|
|
|
|
return;
|
|
};
|