diff --git a/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts b/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts index d5166179e1ca..4e7a75e15bb5 100644 --- a/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts +++ b/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts @@ -1,4 +1,5 @@ import { useIsLogged } from '@/auth/hooks/useIsLogged'; +import { useFreePass } from '@/billing/hooks/useFreePass'; import { useDefaultHomePagePath } from '@/navigation/hooks/useDefaultHomePagePath'; import { useOnboardingStatus } from '@/onboarding/hooks/useOnboardingStatus'; import { AppPath } from '@/types/AppPath'; @@ -38,6 +39,11 @@ const setupMockIsLogged = (isLogged: boolean) => { jest.mocked(useIsLogged).mockReturnValueOnce(isLogged); }; +jest.mock('@/billing/hooks/useFreePass'); +const setupMockFreePass = (freePass: boolean) => { + jest.mocked(useFreePass).mockReturnValueOnce(freePass); +}; + const defaultHomePagePath = '/objects/companies'; jest.mock('@/navigation/hooks/useDefaultHomePagePath'); @@ -146,6 +152,17 @@ const testCases = [ { loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam }, { loc: AppPath.PlanRequired, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: undefined }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.PastDue, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath }, + { loc: AppPath.FreePassCheckout, isLoggedIn: false, subscriptionStatus: undefined, onboardingStatus: undefined, res: AppPath.SignInUp }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.WorkspaceActivation, res: AppPath.CreateWorkspace }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.ProfileCreation, res: AppPath.CreateProfile }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.SyncEmail, res: AppPath.SyncEmails }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.InviteTeam, res: AppPath.InviteTeam }, + { loc: AppPath.FreePassCheckout, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Active, onboardingStatus: OnboardingStatus.Completed, res: defaultHomePagePath }, + { loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: undefined, onboardingStatus: OnboardingStatus.PlanRequired, res: AppPath.PlanRequired }, { loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Canceled, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' }, { loc: AppPath.PlanRequiredSuccess, isLoggedIn: true, subscriptionStatus: SubscriptionStatus.Unpaid, onboardingStatus: OnboardingStatus.Completed, res: '/settings/billing' }, @@ -275,6 +292,7 @@ describe('usePageChangeEffectNavigateLocation', () => { setupMockOnboardingStatus(testCase.onboardingStatus); setupMockSubscriptionStatus(testCase.subscriptionStatus); setupMockIsLogged(testCase.isLoggedIn); + setupMockFreePass(false); expect(usePageChangeEffectNavigateLocation()).toEqual(testCase.res); }); }); diff --git a/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts b/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts index c7c09eee875b..a7fe87586388 100644 --- a/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts +++ b/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts @@ -1,12 +1,10 @@ import { useIsLogged } from '@/auth/hooks/useIsLogged'; -import { freePassState } from '@/billing/states/freePassState'; +import { useFreePass } from '@/billing/hooks/useFreePass'; 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 { useLocation } from 'react-router-dom'; -import { useRecoilState } from 'recoil'; import { OnboardingStatus, SubscriptionStatus } from '~/generated/graphql'; import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation'; @@ -16,21 +14,7 @@ export const usePageChangeEffectNavigateLocation = () => { const onboardingStatus = useOnboardingStatus(); const subscriptionStatus = useSubscriptionStatus(); const { defaultHomePagePath } = useDefaultHomePagePath(); - - const { search } = useLocation(); - - const [freePass, setFreePass] = useRecoilState(freePassState); - - const hasFreePassParameter = - search.includes('freepass') || - search.includes('freePass') || - search.includes('free-pass') || - search.includes('Free-pass') || - search.includes('FreePass'); - - if (hasFreePassParameter) { - setFreePass(true); - } + const freePass = useFreePass(); const isMatchingOpenRoute = isMatchingLocation(AppPath.Invite) || @@ -48,7 +32,8 @@ export const usePageChangeEffectNavigateLocation = () => { isMatchingLocation(AppPath.SyncEmails) || isMatchingLocation(AppPath.InviteTeam) || isMatchingLocation(AppPath.PlanRequired) || - isMatchingLocation(AppPath.PlanRequiredSuccess); + isMatchingLocation(AppPath.PlanRequiredSuccess) || + isMatchingLocation(AppPath.FreePassCheckout); if (isMatchingOpenRoute) { return; @@ -60,7 +45,8 @@ export const usePageChangeEffectNavigateLocation = () => { if ( onboardingStatus === OnboardingStatus.PlanRequired && - !isMatchingLocation(AppPath.PlanRequired) + !isMatchingLocation(AppPath.PlanRequired) && + !isMatchingLocation(AppPath.FreePassCheckout) ) { return freePass ? AppPath.FreePassCheckout : AppPath.PlanRequired; } diff --git a/packages/twenty-front/src/modules/billing/hooks/useFreePass.ts b/packages/twenty-front/src/modules/billing/hooks/useFreePass.ts new file mode 100644 index 000000000000..0ce711f8bc33 --- /dev/null +++ b/packages/twenty-front/src/modules/billing/hooks/useFreePass.ts @@ -0,0 +1,21 @@ +import { freePassState } from '@/billing/states/freePassState'; +import { useLocation } from 'react-router-dom'; +import { useRecoilState } from 'recoil'; + +export const useFreePass = () => { + const { search } = useLocation(); + const [freePass, setFreePass] = useRecoilState(freePassState); + + const hasFreePassParameter = + search.includes('freepass') || + search.includes('freePass') || + search.includes('free-pass') || + search.includes('Free-pass') || + search.includes('FreePass'); + + if (hasFreePassParameter) { + setFreePass(true); + } + + return freePass; +};