Take openRecordIn from the view in scope instead of a global atom (#23422)

Stacked on #23424 (mobile chip navigation). Review that one first; the
diff shown here is only the delta.

## Problem

`recordIndexOpenRecordInState` was a global atom mirroring
`view.openRecordIn`. It was written whenever any index view loaded and
never reset, so a record chip behaved according to whichever view had
been browsed last:

- Companies view set to "record page". Open a Company, tap a related
Opportunity chip. The Opportunities view says "side panel", but the chip
reads the leftover Companies setting and opens a full page.
- Visit the Opportunities index first, then the same Company page, and
that same chip now opens a side panel.

The setting is per view in the database, but the frontend kept it in one
slot as though it were a user preference.

## Approach

The value already lives on the view, so the mirror is deleted rather
than scoped:

- `useResolveOpenRecordIn` reads the current view of the surrounding
context store. On a record index that is the view being displayed. On a
record show page `MainContextStoreProvider` resolves a view for the
object in the URL — the last visited view for that object, falling back
to its index view — so chips there follow a view belonging to the object
they sit on, rather than whatever was loaded last.
- Where no context store is mounted at all (a mention inside a note, for
instance) there is no view to take a setting from, so the hook falls
back to `DEFAULT_VIEW_OPEN_RECORD_IN`. The instance lookup is
non-throwing on purpose: `RecordChip` renders in a lot of places, and an
existing test caught this crashing when the read was strict.
- The options dropdown now reads and writes `currentView.openRecordIn`
directly, the same way `isCompact` beside it already works, so
`setAndPersistOpenRecordIn` only has to persist.
- `useGetOpenRecordIn` is gone; every call site had the object name
available at render, so the reactive hook covers all of them.

## Behaviour change

A chip whose behaviour previously came from an unrelated view now
follows the view in scope. That is the point of the change, but it does
mean some chips will open somewhere different from before, always in the
direction of "what this list is configured to do" rather than "what the
last list was configured to do".

## Testing

- New `useResolveOpenRecordIn` tests: falls back to the default with no
context store, follows the context store's view when there is one.
- Full frontend suite: 951 suites, 5598 tests passing. Typecheck and
lint clean.
- Not exercised in a running app: no database in this environment. The
dropdown's optimistic behaviour in particular relies on the same view
store refresh that `isCompact` already depends on, so it is worth a
click-through before merge.
This commit is contained in:
Félix Malfait
2026-07-28 22:53:05 +02:00
committed by GitHub
parent 057468343f
commit 840c6d0129
12 changed files with 134 additions and 82 deletions
@@ -4,7 +4,6 @@ import { useSetViewTypeFromLayoutOptionsMenu } from '@/object-record/object-opti
import { getSupportedRecordCalendarLayout } from '@/object-record/record-calendar/utils/getSupportedRecordCalendarLayout';
import { recordIndexCalendarLayoutComponentState } from '@/object-record/record-index/states/recordIndexCalendarLayoutComponentState';
import { recordIndexGroupFieldMetadataItemComponentState } from '@/object-record/record-index/states/recordIndexGroupFieldMetadataComponentState';
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader/DropdownMenuHeader';
import { DropdownMenuHeaderLeftComponent } from '@/ui/layout/dropdown/components/DropdownMenuHeader/internal/DropdownMenuHeaderLeftComponent';
@@ -26,7 +25,6 @@ import {
import { useGetAvailableFieldsForCalendar } from '@/views/view-picker/hooks/useGetAvailableFieldsForCalendar';
import { useGetAvailableFieldsToGroupRecordsBy } from '@/views/view-picker/hooks/useGetAvailableFieldsToGroupRecordsBy';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useLingui } from '@lingui/react/macro';
import { useCallback } from 'react';
import { isDefined } from 'twenty-shared/utils';
@@ -69,9 +67,6 @@ export const ObjectOptionsDropdownLayoutContent = () => {
[updateCurrentView],
);
const recordIndexOpenRecordIn = useAtomStateValue(
recordIndexOpenRecordInState,
);
const recordIndexCalendarLayout = useAtomComponentStateValue(
recordIndexCalendarLayoutComponentState,
);
@@ -305,7 +300,7 @@ export const ObjectOptionsDropdownLayoutContent = () => {
<MenuItem
focused={selectedItemId === ViewOpenRecordIn.SIDE_PANEL}
LeftIcon={
recordIndexOpenRecordIn === ViewOpenRecordIn.SIDE_PANEL
currentView?.openRecordIn === ViewOpenRecordIn.SIDE_PANEL
? IconLayoutSidebarRight
: IconLayoutNavbar
}
@@ -314,7 +309,7 @@ export const ObjectOptionsDropdownLayoutContent = () => {
onContentChange('layoutOpenIn');
}}
contextualText={
recordIndexOpenRecordIn === ViewOpenRecordIn.SIDE_PANEL
currentView?.openRecordIn === ViewOpenRecordIn.SIDE_PANEL
? t`Side Panel`
: t`Record Page`
}
@@ -2,7 +2,6 @@ import { OBJECT_OPTIONS_DROPDOWN_ID } from '@/object-record/object-options-dropd
import { useObjectOptionsDropdown } from '@/object-record/object-options-dropdown/hooks/useObjectOptionsDropdown';
import { useUpdateObjectViewOptions } from '@/object-record/object-options-dropdown/hooks/useUpdateObjectViewOptions';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { canOpenObjectInSidePanel } from '@/object-record/utils/canOpenObjectInSidePanel';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader/DropdownMenuHeader';
@@ -12,7 +11,6 @@ import { SelectableList } from '@/ui/layout/selectable-list/components/Selectabl
import { SelectableListItem } from '@/ui/layout/selectable-list/components/SelectableListItem';
import { selectedItemIdComponentState } from '@/ui/layout/selectable-list/states/selectedItemIdComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useGetCurrentViewOnly } from '@/views/hooks/useGetCurrentViewOnly';
import { ViewOpenRecordIn } from '~/generated-metadata/graphql';
import { t } from '@lingui/core/macro';
@@ -25,9 +23,6 @@ import { MenuItemSelect } from 'twenty-ui/navigation';
export const ObjectOptionsDropdownLayoutOpenInContent = () => {
const { onContentChange } = useObjectOptionsDropdown();
const recordIndexOpenRecordIn = useAtomStateValue(
recordIndexOpenRecordInState,
);
const { currentView } = useGetCurrentViewOnly();
const { setAndPersistOpenRecordIn } = useUpdateObjectViewOptions();
const { objectMetadataItem } = useRecordIndexContextOrThrow();
@@ -78,7 +73,9 @@ export const ObjectOptionsDropdownLayoutOpenInContent = () => {
<MenuItemSelect
LeftIcon={IconLayoutSidebarRight}
text={t`Side Panel`}
selected={recordIndexOpenRecordIn === ViewOpenRecordIn.SIDE_PANEL}
selected={
currentView?.openRecordIn === ViewOpenRecordIn.SIDE_PANEL
}
focused={selectedItemId === ViewOpenRecordIn.SIDE_PANEL}
onClick={() => {
if (!canOpenInSidePanel) {
@@ -106,7 +103,7 @@ export const ObjectOptionsDropdownLayoutOpenInContent = () => {
LeftIcon={IconLayoutNavbar}
text={t`Record Page`}
selected={
recordIndexOpenRecordIn === ViewOpenRecordIn.RECORD_PAGE
currentView?.openRecordIn === ViewOpenRecordIn.RECORD_PAGE
}
onClick={() =>
setAndPersistOpenRecordIn(
@@ -1,7 +1,4 @@
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { useStore } from 'jotai';
import { useUpdateCurrentView } from '@/views/hooks/useUpdateCurrentView';
import { type GraphQLView } from '@/views/types/GraphQLView';
import { type ViewOpenRecordIn } from '~/generated-metadata/graphql';
@@ -10,12 +7,6 @@ import { viewPickerSelectedIconComponentState } from '@/views/view-picker/states
import { useCallback } from 'react';
export const useUpdateObjectViewOptions = () => {
const store = useStore();
const setRecordIndexOpenRecordIn = useSetAtomState(
recordIndexOpenRecordInState,
);
const setViewPickerInputName = useSetAtomComponentState(
viewPickerInputNameComponentState,
);
@@ -29,13 +20,11 @@ export const useUpdateObjectViewOptions = () => {
const setAndPersistOpenRecordIn = useCallback(
(openRecordIn: ViewOpenRecordIn, view: GraphQLView | undefined) => {
if (!view) return;
setRecordIndexOpenRecordIn(openRecordIn);
store.set(recordIndexOpenRecordInState.atom, openRecordIn);
updateCurrentView({
openRecordIn,
});
},
[setRecordIndexOpenRecordIn, updateCurrentView, store],
[updateCurrentView],
);
const setAndPersistViewName = useCallback(
@@ -0,0 +1,5 @@
import { ViewOpenRecordIn } from '~/generated-metadata/graphql';
// Used where no view is in scope, so there is no setting to honour: a record
// chip in the command menu or in a mention has no list behind it.
export const DEFAULT_VIEW_OPEN_RECORD_IN = ViewOpenRecordIn.SIDE_PANEL;
@@ -0,0 +1,86 @@
import { renderHook } from '@testing-library/react';
import { Provider as JotaiProvider } from 'jotai';
import { ContextStoreComponentInstanceContext } from '@/context-store/states/contexts/ContextStoreComponentInstanceContext';
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
import { useResolveOpenRecordIn } from '@/object-record/record-index/hooks/useResolveOpenRecordIn';
import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';
import { ViewOpenRecordIn } from '~/generated-metadata/graphql';
jest.mock('react-responsive', () => ({
useMediaQuery: jest.fn().mockReturnValue(false),
}));
jest.mock(
'@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue',
() => ({
useAtomFamilySelectorValue: jest.fn(),
}),
);
const mockUseAtomFamilySelectorValue = jest.requireMock(
'@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue',
).useAtomFamilySelectorValue as jest.Mock;
// Stands in for the views store: only the view the hook actually asks for
// comes back, so a hook reading the wrong view id resolves to nothing.
mockUseAtomFamilySelectorValue.mockImplementation(
(_selector: unknown, { viewId }: { viewId: string }) =>
viewId === 'test-view-id'
? { id: viewId, openRecordIn: ViewOpenRecordIn.RECORD_PAGE }
: undefined,
);
const WrapperWithoutContextStore = ({
children,
}: {
children: React.ReactNode;
}) => <JotaiProvider store={jotaiStore}>{children}</JotaiProvider>;
const WrapperWithContextStore = ({
children,
}: {
children: React.ReactNode;
}) => (
<JotaiProvider store={jotaiStore}>
<ContextStoreComponentInstanceContext.Provider
value={{ instanceId: 'test-context-store' }}
>
{children}
</ContextStoreComponentInstanceContext.Provider>
</JotaiProvider>
);
describe('useResolveOpenRecordIn', () => {
afterEach(() => {
jotaiStore.set(
contextStoreCurrentViewIdComponentState.atomFamily({
instanceId: 'test-context-store',
}),
undefined,
);
});
it('falls back to the default where no context store is mounted', () => {
const { result } = renderHook(() => useResolveOpenRecordIn('company'), {
wrapper: WrapperWithoutContextStore,
});
expect(result.current).toBe(ViewOpenRecordIn.SIDE_PANEL);
});
it('follows the current view of the surrounding context store', () => {
jotaiStore.set(
contextStoreCurrentViewIdComponentState.atomFamily({
instanceId: 'test-context-store',
}),
'test-view-id',
);
const { result } = renderHook(() => useResolveOpenRecordIn('company'), {
wrapper: WrapperWithContextStore,
});
expect(result.current).toBe(ViewOpenRecordIn.RECORD_PAGE);
});
});
@@ -1,23 +0,0 @@
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { resolveOpenRecordIn } from '@/object-record/record-index/utils/resolveOpenRecordIn';
import { useStore } from 'jotai';
import { useCallback } from 'react';
import { useIsMobile } from 'twenty-ui/utilities';
export const useGetOpenRecordIn = () => {
const store = useStore();
const isMobile = useIsMobile();
const getOpenRecordIn = useCallback(
(objectNameSingular: string) =>
resolveOpenRecordIn({
openRecordInViewSetting: store.get(recordIndexOpenRecordInState.atom),
objectNameSingular,
canDisplaySidePanel: !isMobile,
}),
[isMobile, store],
);
return { getOpenRecordIn };
};
@@ -24,7 +24,6 @@ import { clampRecordBoardColumnWidth } from '@/object-record/record-board/utils/
import { recordIndexFieldDefinitionsState } from '@/object-record/record-index/states/recordIndexFieldDefinitionsState';
import { recordIndexGroupAggregateFieldMetadataItemComponentState } from '@/object-record/record-index/states/recordIndexGroupAggregateFieldMetadataItemComponentState';
import { recordIndexGroupAggregateOperationComponentState } from '@/object-record/record-index/states/recordIndexGroupAggregateOperationComponentState';
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { recordIndexKanbanColumnWidthComponentState } from '@/object-record/record-index/states/recordIndexKanbanColumnWidthComponentState';
import { recordIndexShouldHideEmptyRecordGroupsComponentState } from '@/object-record/record-index/states/recordIndexShouldHideEmptyRecordGroupsComponentState';
import { recordIndexViewTypeState } from '@/object-record/record-index/states/recordIndexViewTypeState';
@@ -332,7 +331,6 @@ export const useLoadRecordIndexStates = () => {
if (!skipGlobalIndexStates) {
batchSet(recordIndexViewTypeState.atom, view.type);
batchSet(recordIndexOpenRecordInState.atom, view.openRecordIn);
}
const recordCalendarInstanceId =
@@ -6,7 +6,7 @@ import { contextStoreRecordShowParentViewComponentState } from '@/context-store/
import { currentRecordFilterGroupsComponentState } from '@/object-record/record-filter-group/states/currentRecordFilterGroupsComponentState';
import { currentRecordFiltersComponentState } from '@/object-record/record-filter/states/currentRecordFiltersComponentState';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
import { useGetOpenRecordIn } from '@/object-record/record-index/hooks/useGetOpenRecordIn';
import { useResolveOpenRecordIn } from '@/object-record/record-index/hooks/useResolveOpenRecordIn';
import { currentRecordSortsComponentState } from '@/object-record/record-sort/states/currentRecordSortsComponentState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { ViewOpenRecordIn } from '~/generated-metadata/graphql';
@@ -23,7 +23,7 @@ export const useOpenRecordFromIndexView = () => {
const navigate = useNavigateApp();
const { openRecordInSidePanel } = useOpenRecordInSidePanel();
const { getOpenRecordIn } = useGetOpenRecordIn();
const openRecordIn = useResolveOpenRecordIn(objectNameSingular);
const currentRecordFilters = useAtomComponentStateCallbackState(
currentRecordFiltersComponentState,
@@ -65,7 +65,7 @@ export const useOpenRecordFromIndexView = () => {
},
);
if (getOpenRecordIn(objectNameSingular) === ViewOpenRecordIn.SIDE_PANEL) {
if (openRecordIn === ViewOpenRecordIn.SIDE_PANEL) {
openRecordInSidePanel({
recordId,
objectNameSingular,
@@ -93,7 +93,7 @@ export const useOpenRecordFromIndexView = () => {
objectNameSingular,
navigate,
openRecordInSidePanel,
getOpenRecordIn,
openRecordIn,
closeSidePanelMenu,
store,
],
@@ -1,17 +1,35 @@
import { recordIndexOpenRecordInState } from '@/object-record/record-index/states/recordIndexOpenRecordInState';
import { ContextStoreComponentInstanceContext } from '@/context-store/states/contexts/ContextStoreComponentInstanceContext';
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
import { DEFAULT_VIEW_OPEN_RECORD_IN } from '@/object-record/record-index/constants/DefaultViewOpenRecordIn';
import { resolveOpenRecordIn } from '@/object-record/record-index/utils/resolveOpenRecordIn';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useAvailableComponentInstanceId } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceId';
import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';
import { viewFromViewIdFamilySelector } from '@/views/states/selectors/viewFromViewIdFamilySelector';
import { useAtomValue } from 'jotai';
import { useIsMobile } from 'twenty-ui/utilities';
export const useResolveOpenRecordIn = (objectNameSingular: string) => {
const recordIndexOpenRecordIn = useAtomStateValue(
recordIndexOpenRecordInState,
// Record chips also render where no context store is mounted at all, such as
// a mention inside a note, and those have no view to take a setting from.
const contextStoreInstanceId = useAvailableComponentInstanceId(
ContextStoreComponentInstanceContext,
);
const contextStoreCurrentViewId = useAtomValue(
contextStoreCurrentViewIdComponentState.atomFamily({
instanceId: contextStoreInstanceId ?? '',
}),
);
const currentView = useAtomFamilySelectorValue(viewFromViewIdFamilySelector, {
viewId: contextStoreCurrentViewId ?? '',
});
const isMobile = useIsMobile();
return resolveOpenRecordIn({
openRecordInViewSetting: recordIndexOpenRecordIn,
openRecordInViewSetting:
currentView?.openRecordIn ?? DEFAULT_VIEW_OPEN_RECORD_IN,
objectNameSingular,
canDisplaySidePanel: !isMobile,
});
@@ -1,7 +0,0 @@
import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';
import { ViewOpenRecordIn } from '~/generated-metadata/graphql';
export const recordIndexOpenRecordInState = createAtomState<ViewOpenRecordIn>({
key: 'recordIndexOpenRecordInState',
defaultValue: ViewOpenRecordIn.SIDE_PANEL,
});
@@ -5,7 +5,7 @@ import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord';
import { recordGroupDefinitionsComponentSelector } from '@/object-record/record-group/states/selectors/recordGroupDefinitionsComponentSelector';
import { getFieldMetadataItemGqlFieldName } from '@/object-metadata/utils/getFieldMetadataItemGqlFieldName';
import { recordIndexGroupFieldMetadataItemComponentState } from '@/object-record/record-index/states/recordIndexGroupFieldMetadataComponentState';
import { useGetOpenRecordIn } from '@/object-record/record-index/hooks/useGetOpenRecordIn';
import { useResolveOpenRecordIn } from '@/object-record/record-index/hooks/useResolveOpenRecordIn';
import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { useBuildRecordInputFromFilters } from '@/object-record/record-table/hooks/useBuildRecordInputFromFilters';
@@ -51,7 +51,7 @@ export const useCreateNewIndexRecord = ({
const { openRecordInSidePanel } = useOpenRecordInSidePanel();
const { getOpenRecordIn } = useGetOpenRecordIn();
const openRecordIn = useResolveOpenRecordIn(objectMetadataItem.nameSingular);
const { closeSidePanelMenu } = useSidePanelMenu();
@@ -91,10 +91,7 @@ export const useCreateNewIndexRecord = ({
...mergedRecordInput,
});
if (
getOpenRecordIn(objectMetadataItem.nameSingular) ===
ViewOpenRecordIn.SIDE_PANEL
) {
if (openRecordIn === ViewOpenRecordIn.SIDE_PANEL) {
openRecordInSidePanel({
recordId,
objectNameSingular: objectMetadataItem.nameSingular,
@@ -169,7 +166,7 @@ export const useCreateNewIndexRecord = ({
navigate,
objectMetadataItem,
openRecordInSidePanel,
getOpenRecordIn,
openRecordIn,
recordGroupDefinitions,
recordIndexGroupFieldMetadataItem,
recordIndexRecordIdsByGroupCallbackState,
@@ -18,7 +18,7 @@ import { useSetActiveDropdownFocusIdAndMemorizePrevious } from '@/ui/layout/drop
import { useRecordFieldsScopeContextOrThrow } from '@/object-record/record-field-list/contexts/RecordFieldsScopeContext';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
import { useGetOpenRecordIn } from '@/object-record/record-index/hooks/useGetOpenRecordIn';
import { useResolveOpenRecordIn } from '@/object-record/record-index/hooks/useResolveOpenRecordIn';
import { useOpenRecordFromIndexView } from '@/object-record/record-index/hooks/useOpenRecordFromIndexView';
import { useActiveRecordTableRow } from '@/object-record/record-table/hooks/useActiveRecordTableRow';
import { useFocusedRecordTableRow } from '@/object-record/record-table/hooks/useFocusedRecordTableRow';
@@ -85,10 +85,10 @@ export const useOpenRecordTableCell = (recordTableId: string) => {
const { openRecordFromIndexView } = useOpenRecordFromIndexView();
const { getOpenRecordIn } = useGetOpenRecordIn();
const { objectNameSingular } = useRecordIndexContextOrThrow();
const openRecordIn = useResolveOpenRecordIn(objectNameSingular);
const openTableCell = useCallback(
({
initialValue,
@@ -122,9 +122,7 @@ export const useOpenRecordTableCell = (recordTableId: string) => {
if ((isFirstColumnCell && !isEmpty) || isNavigating) {
leaveTableFocus();
if (
getOpenRecordIn(objectNameSingular) === ViewOpenRecordIn.SIDE_PANEL
) {
if (openRecordIn === ViewOpenRecordIn.SIDE_PANEL) {
activateRecordTableRow(cellPosition.row);
unfocusRecordTableRow();
}
@@ -199,8 +197,7 @@ export const useOpenRecordTableCell = (recordTableId: string) => {
scopeInstanceId,
leaveTableFocus,
openRecordFromIndexView,
getOpenRecordIn,
objectNameSingular,
openRecordIn,
activateRecordTableRow,
unfocusRecordTableRow,
store,