Skip to content

The open alternative to Salesforce, designed for AI.

Host the main surface's route context store once, above the page transition (#25233)

Félix Malfait85b3026f5d8f
diff --git a/packages/twenty-front/src/modules/app/routing/utils/__tests__/getWorkspaceRouteObjectsForSurface.test.tsx b/packages/twenty-front/src/modules/app/routing/utils/__tests__/getWorkspaceRouteObjectsForSurface.test.tsx
new file mode 100644
index 00000000000..b02765faf45
--- /dev/null
+++ b/packages/twenty-front/src/modules/app/routing/utils/__tests__/getWorkspaceRouteObjectsForSurface.test.tsx
@@ -0,0 +1,58 @@
+import { Children, isValidElement, type ReactNode } from 'react';
+
+import { type WorkspaceRouteObject } from '@/app/routing/types/WorkspaceRouteObject';
+import { getWorkspaceRouteObjectsForSurface } from '@/app/routing/utils/getWorkspaceRouteObjectsForSurface';
+import { RouteContextStoreProvider } from '@/context-store/components/RouteContextStoreProvider';
+
+const containsRouteContextStoreProvider = (node: ReactNode): boolean =>
+ Children.toArray(node).some(
+ (child) =>
+ isValidElement(child) &&
+ (child.type === RouteContextStoreProvider ||
+ containsRouteContextStoreProvider(
+ (child.props as { children?: ReactNode }).children,
+ )),
+ );
+
+const routeObjects: WorkspaceRouteObject[] = [
+ {
+ path: '/objects/:objectNamePlural',
+ element: <div>index</div>,
+ handle: { workspaceSurfaces: ['main', 'side-panel'] },
+ },
+ {
+ path: '/settings/*',
+ element: <div>settings</div>,
+ handle: { workspaceSurfaces: ['main', 'side-panel'] },
+ },
+];
+
+describe('getWorkspaceRouteObjectsForSurface', () => {
+ it('leaves main-surface route elements without a route context store provider', () => {
+ const mainRouteObjects = getWorkspaceRouteObjectsForSurface(
+ routeObjects,
+ 'main',
+ );
+
+ expect(mainRouteObjects).toHaveLength(2);
+ expect(
+ mainRouteObjects.some((routeObject) =>
+ containsRouteContextStoreProvider(routeObject.element),
+ ),
+ ).toBe(false);
+ });
+
+ it('wraps side-panel route elements with a route context store provider', () => {
+ const sidePanelRouteObjects = getWorkspaceRouteObjectsForSurface(
+ routeObjects,
+ 'side-panel',
+ );
+
+ expect(sidePanelRouteObjects).toHaveLength(2);
+ expect(
+ sidePanelRouteObjects.every((routeObject) =>
+ containsRouteContextStoreProvider(routeObject.element),
+ ),
+ ).toBe(true);
+ });
+});
diff --git a/packages/twenty-front/src/modules/app/routing/utils/getWorkspaceRouteObjectsForSurface.tsx b/packages/twenty-front/src/modules/app/routing/utils/getWorkspaceRouteObjectsForSurface.tsx
index 0dddd7f21b2..265eca0e681 100644
--- a/packages/twenty-front/src/modules/app/routing/utils/getWorkspaceRouteObjectsForSurface.tsx
+++ b/packages/twenty-front/src/modules/app/routing/utils/getWorkspaceRouteObjectsForSurface.tsx
@@ -43,7 +43,14 @@ const getWorkspaceRouteObjects = (
];
});
+// The main surface hosts its route context store once, above the page
+// transition, in MainAppLayoutWithSidePanel: the transition keeps the page
+// being left mounted while the next one enters, and a provider inside each
+// page would leave two of them writing the same store from different routes.
+// The side panel renders its routes against its own location, which only the
+// routed tree can read, and it remounts per location, so its provider lives
+// inside the tree.
export const getWorkspaceRouteObjectsForSurface = (
routeObjects: WorkspaceRouteObject[],
surface: WorkspaceSurfaceType,
-) => getWorkspaceRouteObjects(routeObjects, surface, true);
+) => getWorkspaceRouteObjects(routeObjects, surface, surface === 'side-panel');
diff --git a/packages/twenty-front/src/modules/ui/layout/page/components/MainAppLayoutWithSidePanel.tsx b/packages/twenty-front/src/modules/ui/layout/page/components/MainAppLayoutWithSidePanel.tsx
index 98661b84ba0..3b7ccd62832 100644
--- a/packages/twenty-front/src/modules/ui/layout/page/components/MainAppLayoutWithSidePanel.tsx
+++ b/packages/twenty-front/src/modules/ui/layout/page/components/MainAppLayoutWithSidePanel.tsx
@@ -1,5 +1,6 @@
import { CommandMenuForMobile } from '@/command-menu/components/CommandMenuForMobile';
import { useCommandMenuHotKeys } from '@/command-menu/hooks/useCommandMenuHotKeys';
+import { RouteContextStoreProvider } from '@/context-store/components/RouteContextStoreProvider';
import { SidePanelForDesktop } from '@/side-panel/components/SidePanelForDesktop';
import { SidePanelPathUrlSyncEffect } from '@/side-panel/routing/components/SidePanelPathUrlSyncEffect';
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
@@ -129,6 +130,7 @@ export const MainAppLayoutWithSidePanel = () => {
return (
<StyledRow>
+ <RouteContextStoreProvider />
<SidePanelPathUrlSyncEffect />
<StyledContent>
<MainAppLayoutOutlet />
diff --git a/packages/twenty-front/src/modules/ui/layout/page/components/__tests__/MainAppLayoutWithSidePanel.test.tsx b/packages/twenty-front/src/modules/ui/layout/page/components/__tests__/MainAppLayoutWithSidePanel.test.tsx
new file mode 100644
index 00000000000..58136fba436
--- /dev/null
+++ b/packages/twenty-front/src/modules/ui/layout/page/components/__tests__/MainAppLayoutWithSidePanel.test.tsx
@@ -0,0 +1,115 @@
+import { act, render, screen, waitFor } from '@testing-library/react';
+import { useEffect } from 'react';
+import {
+ MemoryRouter,
+ type NavigateFunction,
+ useNavigate,
+ useRoutes,
+} from 'react-router-dom';
+
+import { type WorkspaceRouteObject } from '@/app/routing/types/WorkspaceRouteObject';
+import { getWorkspaceRouteObjectsForSurface } from '@/app/routing/utils/getWorkspaceRouteObjectsForSurface';
+import { MainAppLayoutWithSidePanel } from '@/ui/layout/page/components/MainAppLayoutWithSidePanel';
+
+const mockProviderStats = { current: 0, max: 0 };
+const mockSeenParams: Record<string, string | undefined>[] = [];
+
+jest.mock('@/context-store/components/RouteContextStoreProvider', () => {
+ const { useParams } = jest.requireActual('react-router-dom');
+
+ return {
+ RouteContextStoreProvider: () => {
+ mockSeenParams.push(useParams());
+
+ useEffect(() => {
+ mockProviderStats.current += 1;
+ mockProviderStats.max = Math.max(
+ mockProviderStats.max,
+ mockProviderStats.current,
+ );
+
+ return () => {
+ mockProviderStats.current -= 1;
+ };
+ }, []);
+
+ return null;
+ },
+ };
+});
+
+jest.mock('@/side-panel/components/SidePanelForDesktop', () => ({
+ SidePanelForDesktop: () => null,
+}));
+
+jest.mock('@/command-menu/components/CommandMenuForMobile', () => ({
+ CommandMenuForMobile: () => null,
+}));
+
+jest.mock('@/side-panel/routing/components/SidePanelPathUrlSyncEffect', () => ({
+ SidePanelPathUrlSyncEffect: () => null,
+}));
+
+jest.mock('@/command-menu/hooks/useCommandMenuHotKeys', () => ({
+ useCommandMenuHotKeys: () => {},
+}));
+
+const routeObjects: WorkspaceRouteObject[] = [
+ {
+ path: '/objects/:objectNamePlural',
+ element: <div>record index page</div>,
+ handle: { workspaceSurfaces: ['main'] },
+ },
+ {
+ path: '/settings/*',
+ element: <div>settings page</div>,
+ handle: { workspaceSurfaces: ['main'] },
+ },
+];
+
+const navigateRef: { current: NavigateFunction | null } = { current: null };
+
+const NavigateProbeEffect = () => {
+ navigateRef.current = useNavigate();
+
+ return null;
+};
+
+const MainSurfaceRoutes = () =>
+ useRoutes([
+ {
+ element: <MainAppLayoutWithSidePanel />,
+ children: getWorkspaceRouteObjectsForSurface(routeObjects, 'main'),
+ },
+ ]);
+
+describe('MainAppLayoutWithSidePanel', () => {
+ // Switching between the app and settings sections keeps the page being left
+ // mounted while the next one animates in. If each page carried its own route
+ // context store provider, the stale one would keep writing the main store
+ // from a route that no longer matches the location, fighting the new one.
+ it('keeps a single route context store provider on the main surface across a section switch', async () => {
+ render(
+ <MemoryRouter initialEntries={['/objects/companies']}>
+ <NavigateProbeEffect />
+ <MainSurfaceRoutes />
+ </MemoryRouter>,
+ );
+
+ expect(await screen.findByText('record index page')).toBeInTheDocument();
+ // Hosted above the routes, the provider still reads the matched leaf's
+ // params: react-router shares one params object across a matched branch.
+ expect(mockSeenParams.at(-1)?.objectNamePlural).toBe('companies');
+
+ act(() => {
+ navigateRef.current?.('/settings/profile');
+ });
+
+ await waitFor(() => {
+ expect(screen.getByText('settings page')).toBeInTheDocument();
+ });
+
+ expect(mockSeenParams.at(-1)?.objectNamePlural).toBeUndefined();
+ expect(mockProviderStats.max).toBe(1);
+ });
+});