Fix orphan navigation menu items for deleted views (#18791)

## Summary

Fixes #18757

This fixes a set of Favorites / navigation-menu-item integrity problems
related to deleted views, stale hidden items, and upgraded workspaces
with orphaned navigation items.

## What changed

- delete `navigationMenuItem` entries when their favorited view is
deleted
- keep the client metadata store in sync immediately when a view is
deleted
- determine whether a view is already favorited from visible valid
navigation items instead of raw stale items
- add a `1.20.0` upgrade repair command that deletes orphan navigation
menu items and normalizes positions
- add regression coverage for deletion of both record-based and
view-based navigation menu items

## Details

Server:
- extend `NavigationMenuItemDeletionService` so cleanup applies to
deleted views as well as deleted records
- add regression tests covering record-based deletion, view-based
deletion, and no-op behavior
- add `DeleteOrphanNavigationMenuItemsCommand` to remove orphaned items
pointing to:
  - deleted views
  - deleted records
  - missing folders
- normalize positions per scope (`userWorkspaceId + folderId`) after
repair
- wire the new repair command into the `1.20.0` upgrade flow

Frontend:
- add `useRemoveNavigationMenuItemByViewId`
- remove the related navigation item from client metadata immediately
when deleting a view
- use sorted / visible navigation items for favorite detection so stale
hidden rows do not block re-adding a favorite

## Why

Issue `#18757` reports mismatches between Favorites shown in the UI and
rows users can still find in the database. We found that current
Favorites behavior is driven by `navigationMenuItem`, not the legacy
`favorite` table, and that stale / orphaned `navigationMenuItem` rows
could:
- remain after deleting a favorited view
- stay hidden from the UI if they point to invalid targets
- still cause the UI to think a view was already favorited
- persist in workspaces with migration damage from skipped sequential
upgrades

This patch addresses those cases directly and adds an upgrade-time
repair path for older corrupted workspaces.

## Validation

Passed:
- `./node_modules/.bin/jest --config
packages/twenty-server/jest.config.mjs --runInBand
packages/twenty-server/src/engine/metadata-modules/navigation-menu-item/services/__tests__/navigation-menu-item-deletion.service.spec.ts`
- `./node_modules/.bin/tsc -p packages/twenty-front/tsconfig.json
--noEmit --pretty false`

Known unrelated existing failure:
- `./node_modules/.bin/tsc -p packages/twenty-server/tsconfig.json
--noEmit --pretty false`

The server typecheck failure is pre-existing and unrelated to this
branch. Current errors are around `@file-type/pdf` module resolution and
`is-psl-parsed-domain.type.ts`.

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
Sri Hari Haran Sharma
2026-03-20 20:24:33 +05:30
committed by GitHub
parent 89300564ba
commit 7c8f060b08
9 changed files with 160 additions and 8 deletions
@@ -0,0 +1,35 @@
import { useCallback } from 'react';
import { useMetadataStore } from '@/metadata-store/hooks/useMetadataStore';
import { metadataStoreState } from '@/metadata-store/states/metadataStoreState';
import { type NavigationMenuItem } from '~/generated-metadata/graphql';
import { isDefined } from 'twenty-shared/utils';
import { useStore } from 'jotai';
export const useOptimisticRemoveNavigationMenuItemsByViewId = () => {
const store = useStore();
const { replaceDraft, applyChanges } = useMetadataStore();
const removeNavigationMenuItemsByViewIds = useCallback(
(viewIds: string[]) => {
const viewIdsSet = new Set(viewIds);
const entry = store.get(
metadataStoreState.atomFamily('navigationMenuItems'),
);
const currentNavigationMenuItems =
entry.current as unknown as NavigationMenuItem[];
const updatedNavigationMenuItems = currentNavigationMenuItems.filter(
(item) => !isDefined(item.viewId) || !viewIdsSet.has(item.viewId),
);
replaceDraft('navigationMenuItems', updatedNavigationMenuItems);
applyChanges();
},
[store, replaceDraft, applyChanges],
);
return {
removeNavigationMenuItemsByViewIds,
};
};
@@ -1,5 +1,6 @@
import { useCreateNavigationMenuItem } from '@/navigation-menu-item/common/hooks/useCreateNavigationMenuItem';
import { useNavigationMenuItemsData } from '@/navigation-menu-item/display/hooks/useNavigationMenuItemsData';
import { useSortedNavigationMenuItems } from '@/navigation-menu-item/display/hooks/useSortedNavigationMenuItems';
import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
import { useHasPermissionFlag } from '@/settings/roles/hooks/useHasPermissionFlag';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
@@ -54,15 +55,15 @@ export const ViewPickerOptionDropdown = ({
const hasViewsPermission = useHasPermissionFlag(PermissionFlagType.VIEWS);
const { createNavigationMenuItem } = useCreateNavigationMenuItem();
const { navigationMenuItems, currentWorkspaceMemberId } =
useNavigationMenuItemsData();
const { currentWorkspaceMemberId } = useNavigationMenuItemsData();
const { navigationMenuItemsSorted } = useSortedNavigationMenuItems();
// Users with VIEWS permission can edit all views
// Users without VIEWS permission can only edit unlisted views (which are always their own, filtered by backend)
const canEditView =
hasViewsPermission || view.visibility === ViewVisibility.UNLISTED;
const isFavorite = navigationMenuItems.some(
const isFavorite = navigationMenuItemsSorted.some(
(item) =>
item.viewId === view.id &&
item.userWorkspaceId === currentWorkspaceMemberId,
@@ -2,6 +2,7 @@ import { useCallback } from 'react';
import { useStore } from 'jotai';
import { useContextStoreObjectMetadataItemOrThrow } from '@/context-store/hooks/useContextStoreObjectMetadataItemOrThrow';
import { useOptimisticRemoveNavigationMenuItemsByViewId } from '@/navigation-menu-item/edit/hooks/useOptimisticRemoveNavigationMenuItemsByViewId';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';
import { usePerformViewAPIPersist } from '@/views/hooks/internal/usePerformViewAPIPersist';
@@ -45,6 +46,8 @@ export const useDestroyViewFromCurrentState = (viewBarInstanceId?: string) => {
const { changeView } = useChangeView();
const { performViewAPIDestroy } = usePerformViewAPIPersist();
const { removeNavigationMenuItemsByViewIds } =
useOptimisticRemoveNavigationMenuItemsByViewId();
const store = useStore();
@@ -72,11 +75,13 @@ export const useDestroyViewFromCurrentState = (viewBarInstanceId?: string) => {
}
await performViewAPIDestroy({ id: viewPickerReferenceViewId });
removeNavigationMenuItemsByViewIds([viewPickerReferenceViewId]);
}, [
currentView,
closeAndResetViewPicker,
changeView,
performViewAPIDestroy,
removeNavigationMenuItemsByViewIds,
store,
viewPickerIsDirtyCallbackState,
viewPickerIsPersistingCallbackState,