Skip to content

The open alternative to Salesforce, designed for AI.

Hold a suspended workspace on its billing pages (#27362)

Charles Bochetec4154d9ecb4
diff --git a/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts b/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts
index 27c0c9820a6..e153b7e3a8d 100644
--- a/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts
+++ b/packages/twenty-front/src/hooks/__tests__/usePageChangeEffectNavigateLocation.test.ts
@@ -416,7 +416,7 @@ const testCases: {
{ loc: AppPath.PageLayoutPage, isLogged: true, isWorkspaceSuspended: false, onboardingStatus: OnboardingStatus.COMPLETED, res: undefined, pageLayoutId: 'valid-standalone-id', useQueryResult: { data: { getPageLayout: { type: PageLayoutType.STANDALONE_PAGE } }, loading: false } },
{ loc: AppPath.SettingsCatchAll, isLogged: true, isWorkspaceSuspended: false, onboardingStatus: OnboardingStatus.PLAN_REQUIRED, res: AppPath.PlanRequired },
- { loc: AppPath.SettingsCatchAll, isLogged: true, isWorkspaceSuspended: true, onboardingStatus: OnboardingStatus.COMPLETED, res: undefined },
+ { loc: AppPath.SettingsCatchAll, isLogged: true, isWorkspaceSuspended: true, onboardingStatus: OnboardingStatus.COMPLETED, res: getSettingsPath(SettingsPath.Billing) },
{ loc: AppPath.SettingsCatchAll, isLogged: false, isWorkspaceSuspended: false, onboardingStatus: undefined, res: AppPath.SignInUp },
{ loc: AppPath.SettingsCatchAll, isLogged: true, isWorkspaceSuspended: false, onboardingStatus: OnboardingStatus.WORKSPACE_ACTIVATION, res: AppPath.WorkspaceActivation },
{ loc: AppPath.SettingsCatchAll, isLogged: true, isWorkspaceSuspended: false, onboardingStatus: OnboardingStatus.PROFILE_CREATION, res: AppPath.CreateProfile },
@@ -580,3 +580,26 @@ describe('usePageChangeEffectNavigateLocation — authenticated with no current
},
);
});
+
+describe('usePageChangeEffectNavigateLocation — suspended workspace settings pages', () => {
+ it.each([
+ { loc: getSettingsPath(SettingsPath.Billing), res: undefined },
+ { loc: getSettingsPath(SettingsPath.BillingPlans), res: undefined },
+ { loc: getSettingsPath(SettingsPath.ProfilePage), res: undefined },
+ {
+ loc: getSettingsPath(SettingsPath.Usage),
+ res: getSettingsPath(SettingsPath.Billing),
+ },
+ ])('returns $res from $loc', ({ loc, res }) => {
+ setupMockIsMatchingLocation(loc);
+ setupMockOnboardingStatus(OnboardingStatus.COMPLETED);
+ setupMockIsWorkspaceActivationStatusEqualsTo(true);
+ setupMockIsLogged(true);
+ setupMockIsOnAWorkspace(true);
+ setupMockUseQuery();
+ setupMockUseLocation(loc as AppPath);
+ setupMockState();
+
+ expect(usePageChangeEffectNavigateLocation()).toEqual(res);
+ });
+});
diff --git a/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts b/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts
index 39841aac1df..bc46097d52c 100644
--- a/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts
+++ b/packages/twenty-front/src/hooks/usePageChangeEffectNavigateLocation.ts
@@ -18,7 +18,7 @@ import { useQuery } from '@apollo/client/react';
import { isNonEmptyString } from '@sniptt/guards';
import { matchPath, useLocation } from 'react-router-dom';
import { AppPath, SettingsPath } from 'twenty-shared/types';
-import { isDefined } from 'twenty-shared/utils';
+import { getSettingsPath, isDefined } from 'twenty-shared/utils';
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import {
FindOnePageLayoutTypeDocument,
@@ -113,10 +113,16 @@ export const usePageChangeEffectNavigateLocation = () => {
}
if (isWorkspaceSuspended) {
- if (!isMatchingLocation(location, AppPath.SettingsCatchAll)) {
- return `${AppPath.SettingsCatchAll.replace('/*', '')}/${
- SettingsPath.Billing
- }`;
+ const isOnSuspendedWorkspaceAllowedPage = [
+ SettingsPath.Billing,
+ SettingsPath.BillingPlans,
+ SettingsPath.ProfilePage,
+ ].some((settingsPath) =>
+ isMatchingLocation(location, getSettingsPath(settingsPath)),
+ );
+
+ if (!isOnSuspendedWorkspaceAllowedPage) {
+ return getSettingsPath(SettingsPath.Billing);
}
return;
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
index 98d041d97f1..8883139403f 100644
--- a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
@@ -5,11 +5,16 @@ import { AgentChatStreamKeepAliveEffect } from '@/ai/components/AgentChatStreamK
import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect';
import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
+import { useIsWorkspaceActivationStatusEqualsTo } from '@/workspace/hooks/useIsWorkspaceActivationStatusEqualsTo';
+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
export const AgentChatRuntimeEffects = () => {
const hasAgentChatBeenOpened = useAtomStateValue(hasAgentChatBeenOpenedState);
+ const isWorkspaceSuspended = useIsWorkspaceActivationStatusEqualsTo(
+ WorkspaceActivationStatus.SUSPENDED,
+ );
- if (!hasAgentChatBeenOpened) {
+ if (!hasAgentChatBeenOpened || isWorkspaceSuspended) {
return null;
}
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx
new file mode 100644
index 00000000000..66abd8b3693
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx
@@ -0,0 +1,59 @@
+import { render, screen } from '@testing-library/react';
+import { Provider as JotaiProvider } from 'jotai';
+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
+
+import { AgentChatRuntimeEffects } from '@/ai/components/AgentChatRuntimeEffects';
+import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState';
+import {
+ currentWorkspaceState,
+ type CurrentWorkspace,
+} from '@/auth/states/currentWorkspaceState';
+import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
+
+jest.mock('@/ai/components/AgentChatMessagesFetchEffect', () => ({
+ AgentChatMessagesFetchEffect: () => null,
+}));
+jest.mock('@/ai/components/AgentChatStreamSubscriptionEffect', () => ({
+ AgentChatStreamSubscriptionEffect: () => (
+ <div data-testid="stream-subscription" />
+ ),
+}));
+jest.mock('@/ai/components/AgentChatPrepromptEffect', () => ({
+ AgentChatPrepromptEffect: () => null,
+}));
+jest.mock('@/ai/components/AgentChatStreamKeepAliveEffect', () => ({
+ AgentChatStreamKeepAliveEffect: () => null,
+}));
+jest.mock('@/ai/components/AgentChatSessionStartTimeEffect', () => ({
+ AgentChatSessionStartTimeEffect: () => null,
+}));
+
+const renderRuntimeEffects = () =>
+ render(
+ <JotaiProvider store={jotaiStore}>
+ <AgentChatRuntimeEffects />
+ </JotaiProvider>,
+ );
+
+describe('AgentChatRuntimeEffects', () => {
+ beforeEach(() => {
+ jotaiStore.set(hasAgentChatBeenOpenedState.atom, true);
+ jotaiStore.set(currentWorkspaceState.atom, null);
+ });
+
+ it('should run the chat runtime once the chat has been opened', () => {
+ renderRuntimeEffects();
+
+ expect(screen.queryByTestId('stream-subscription')).not.toBeNull();
+ });
+
+ it('should not run the chat runtime for a suspended workspace', () => {
+ jotaiStore.set(currentWorkspaceState.atom, {
+ activationStatus: WorkspaceActivationStatus.SUSPENDED,
+ } as CurrentWorkspace);
+
+ renderRuntimeEffects();
+
+ expect(screen.queryByTestId('stream-subscription')).toBeNull();
+ });
+});
diff --git a/packages/twenty-front/src/modules/side-panel/hooks/__tests__/useOpenAskAiPageInSidePanel.test.tsx b/packages/twenty-front/src/modules/side-panel/hooks/__tests__/useOpenAskAiPageInSidePanel.test.tsx
index 07458f1f0d9..699f5a88375 100644
--- a/packages/twenty-front/src/modules/side-panel/hooks/__tests__/useOpenAskAiPageInSidePanel.test.tsx
+++ b/packages/twenty-front/src/modules/side-panel/hooks/__tests__/useOpenAskAiPageInSidePanel.test.tsx
@@ -2,11 +2,17 @@ import { act, renderHook } from '@testing-library/react';
import { Provider as JotaiProvider } from 'jotai';
import { type ReactNode } from 'react';
+import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState';
+import {
+ currentWorkspaceState,
+ type CurrentWorkspace,
+} from '@/auth/states/currentWorkspaceState';
import { useOpenAskAiPageInSidePanel } from '@/side-panel/hooks/useOpenAskAiPageInSidePanel';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
import { SidePanelPages } from 'twenty-shared/types';
import { IconSparkles } from 'twenty-ui/icon';
+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
const navigateSidePanelMenuMock = jest.fn();
@@ -27,6 +33,8 @@ describe('useOpenAskAiPageInSidePanel', () => {
beforeEach(() => {
jest.clearAllMocks();
jotaiStore.set(isSidePanelOpenedState.atom, false);
+ jotaiStore.set(hasAgentChatBeenOpenedState.atom, false);
+ jotaiStore.set(currentWorkspaceState.atom, null);
window.history.pushState({}, '', '/objects/companies');
});
@@ -97,4 +105,21 @@ describe('useOpenAskAiPageInSidePanel', () => {
expect(navigateSidePanelMenuMock).not.toHaveBeenCalled();
});
+
+ it('should not open the AskAI page for a suspended workspace', () => {
+ jotaiStore.set(currentWorkspaceState.atom, {
+ activationStatus: WorkspaceActivationStatus.SUSPENDED,
+ } as CurrentWorkspace);
+
+ const { result } = renderHook(() => useOpenAskAiPageInSidePanel(), {
+ wrapper: Wrapper,
+ });
+
+ act(() => {
+ result.current.openAskAiPage();
+ });
+
+ expect(navigateSidePanelMenuMock).not.toHaveBeenCalled();
+ expect(jotaiStore.get(hasAgentChatBeenOpenedState.atom)).toBe(false);
+ });
});
diff --git a/packages/twenty-front/src/modules/side-panel/hooks/useOpenAskAiPageInSidePanel.ts b/packages/twenty-front/src/modules/side-panel/hooks/useOpenAskAiPageInSidePanel.ts
index 2780df3eccd..addb5661fa8 100644
--- a/packages/twenty-front/src/modules/side-panel/hooks/useOpenAskAiPageInSidePanel.ts
+++ b/packages/twenty-front/src/modules/side-panel/hooks/useOpenAskAiPageInSidePanel.ts
@@ -1,4 +1,5 @@
import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState';
+import { useIsWorkspaceActivationStatusEqualsTo } from '@/workspace/hooks/useIsWorkspaceActivationStatusEqualsTo';
import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
@@ -6,6 +7,7 @@ import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomStat
import { t } from '@lingui/core/macro';
import { useCallback } from 'react';
import { SidePanelPages } from 'twenty-shared/types';
+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import { IconSparkles } from 'twenty-ui/icon';
import { v4 } from 'uuid';
import { isCurrentPathAiChatPage } from '~/utils/isCurrentPathAiChatPage';
@@ -16,6 +18,9 @@ export const useOpenAskAiPageInSidePanel = () => {
const setHasAgentChatBeenOpened = useSetAtomState(
hasAgentChatBeenOpenedState,
);
+ const isWorkspaceSuspended = useIsWorkspaceActivationStatusEqualsTo(
+ WorkspaceActivationStatus.SUSPENDED,
+ );
const openAskAiPage = useCallback(
({
@@ -23,7 +28,7 @@ export const useOpenAskAiPageInSidePanel = () => {
}: {
resetNavigationStack?: boolean;
} = {}) => {
- if (isCurrentPathAiChatPage()) {
+ if (isWorkspaceSuspended || isCurrentPathAiChatPage()) {
return;
}
@@ -42,7 +47,12 @@ export const useOpenAskAiPageInSidePanel = () => {
resetNavigationStack: shouldReset,
});
},
- [navigateSidePanelMenu, isSidePanelOpened, setHasAgentChatBeenOpened],
+ [
+ navigateSidePanelMenu,
+ isSidePanelOpened,
+ setHasAgentChatBeenOpened,
+ isWorkspaceSuspended,
+ ],
);
return {