79c9c75776
## What does this PR do? Fixes a bug where `NavigationMenuItem.userWorkspaceId` was being compared/set to `WorkspaceMember.id` instead of the correct `UserWorkspace.id`, causing the favorites functionality to not work correctly. Fixes #21291 ## Problem The `isFavorite` check in `ViewPickerOptionDropdown` and `createManyNavigationMenuItems` calls in multiple files were using `currentWorkspaceMemberId` (which is `WorkspaceMember.id` from the `workspace_*` schema) instead of the correct `UserWorkspace.id` (from the `core` schema). This caused: - `isFavorite` to always return `false` for user favorites - Navigation menu items to be created with incorrect `userWorkspaceId` ## Root Cause In `useNavigationMenuItemsData.ts`: - `currentWorkspaceMemberId` was derived from `currentWorkspaceMember?.id` (WorkspaceMember.id) - But `NavigationMenuItem.userWorkspaceId` expects a `UserWorkspace.id` - These are two different entities from different schemas (core vs workspace) ## Solution 1. Added `currentUserWorkspaceId` to the `useNavigationMenuItemsData` hook return type 2. `currentUserWorkspaceId` is derived from `currentWorkspaceMember?.userWorkspaceId` 3. Updated all comparisons and assignments to use `currentUserWorkspaceId` when dealing with `userWorkspaceId` ## Files Changed - `packages/twenty-front/src/modules/navigation-menu-item/display/hooks/useNavigationMenuItemsData.ts` - Added `currentUserWorkspaceId` to return type - `packages/twenty-front/src/modules/views/view-picker/components/ViewPickerOptionDropdown.tsx` - Fixed `isFavorite` check and `createManyNavigationMenuItems` call - `packages/twenty-front/src/modules/command-menu-item/engine-command/record/single-record/components/AddToFavoritesSingleRecordCommand.tsx` - Fixed `createManyNavigationMenuItems` call - `packages/twenty-front/src/modules/navigation-menu-item/edit/hooks/useNavigationMenuItemEditController.ts` - Fixed `targetUserWorkspaceId` assignment ## Testing - No existing tests directly cover the `useNavigationMenuItemsData` hook - The fix is a simple type/field correction that should not affect other components - CI will verify TypeScript compilation and linting ## Checklist - [x] I have read the [CONTRIBUTING.md](https://github.com/twentyhq/twenty/blob/main/.github/CONTRIBUTING.md) file - [x] Changes are tested locally (TypeScript compilation) - [x] Commit message follows repository conventions - [x] PR is linked to the relevant issue (#21291) --------- Co-authored-by: Mani bharadwaj <Manibharadwaj@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
169 lines
5.6 KiB
TypeScript
169 lines
5.6 KiB
TypeScript
import { useCreateManyNavigationMenuItems } from '@/navigation-menu-item/common/hooks/useCreateManyNavigationMenuItems';
|
|
import { useNavigationMenuItemsData } from '@/navigation-menu-item/display/hooks/useNavigationMenuItemsData';
|
|
import { useHasPermissionFlag } from '@/settings/roles/hooks/useHasPermissionFlag';
|
|
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
|
|
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
|
|
import { useCloseDropdown } from '@/ui/layout/dropdown/hooks/useCloseDropdown';
|
|
import { MenuItemWithOptionDropdown } from '@/ui/navigation/menu-item/components/MenuItemWithOptionDropdown';
|
|
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
|
|
import { type View } from '@/views/types/View';
|
|
import { useDestroyViewFromCurrentState } from '@/views/view-picker/hooks/useDestroyViewFromCurrentState';
|
|
import { viewPickerReferenceViewIdComponentState } from '@/views/view-picker/states/viewPickerReferenceViewIdComponentState';
|
|
import { useLingui } from '@lingui/react/macro';
|
|
import { NavigationMenuItemType } from 'twenty-shared/types';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import {
|
|
IconHeart,
|
|
IconLock,
|
|
IconPencil,
|
|
IconTrash,
|
|
useIcons,
|
|
} from 'twenty-ui/display';
|
|
import { MenuItem } from 'twenty-ui/navigation';
|
|
import {
|
|
PermissionFlagType,
|
|
ViewVisibility,
|
|
} from '~/generated-metadata/graphql';
|
|
|
|
type ViewPickerOptionDropdownProps = {
|
|
isIndexView: boolean;
|
|
isLastView: boolean;
|
|
view: Pick<
|
|
View,
|
|
'id' | 'name' | 'icon' | 'visibility' | 'createdByUserWorkspaceId'
|
|
>;
|
|
onEdit: (event: React.MouseEvent<HTMLElement>, viewId: string) => void;
|
|
handleViewSelect: (viewId: string) => void;
|
|
};
|
|
|
|
export const ViewPickerOptionDropdown = ({
|
|
isIndexView,
|
|
isLastView,
|
|
onEdit,
|
|
view,
|
|
handleViewSelect,
|
|
}: ViewPickerOptionDropdownProps) => {
|
|
const dropdownId = `view-picker-options-${view.id}`;
|
|
|
|
const { t } = useLingui();
|
|
const { closeDropdown } = useCloseDropdown();
|
|
const { getIcon } = useIcons();
|
|
const { destroyViewFromCurrentState } = useDestroyViewFromCurrentState();
|
|
const setViewPickerReferenceViewId = useSetAtomComponentState(
|
|
viewPickerReferenceViewIdComponentState,
|
|
);
|
|
const hasViewsPermission = useHasPermissionFlag(PermissionFlagType.VIEWS);
|
|
|
|
const { createManyNavigationMenuItems } = useCreateManyNavigationMenuItems();
|
|
const { navigationMenuItems, currentUserWorkspaceId } =
|
|
useNavigationMenuItemsData();
|
|
|
|
// 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(
|
|
(item) =>
|
|
item.viewId === view.id &&
|
|
item.userWorkspaceId === currentUserWorkspaceId,
|
|
);
|
|
|
|
const handleDelete = () => {
|
|
setViewPickerReferenceViewId(view.id);
|
|
destroyViewFromCurrentState();
|
|
closeDropdown(dropdownId);
|
|
};
|
|
|
|
const handleAddToFavorites = () => {
|
|
if (!isFavorite) {
|
|
const relevantItems = navigationMenuItems.filter(
|
|
(item) => !isDefined(item.folderId) && isDefined(item.userWorkspaceId),
|
|
);
|
|
|
|
const maxPosition = Math.max(
|
|
...relevantItems.map((item) => item.position),
|
|
0,
|
|
);
|
|
|
|
createManyNavigationMenuItems([
|
|
{
|
|
id: uuidv4(),
|
|
type: NavigationMenuItemType.VIEW,
|
|
viewId: view.id,
|
|
userWorkspaceId: currentUserWorkspaceId,
|
|
position: maxPosition + 1,
|
|
},
|
|
]);
|
|
}
|
|
closeDropdown(dropdownId);
|
|
};
|
|
|
|
const getVisibilityIcon = () => {
|
|
if (isIndexView) {
|
|
return IconLock;
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
const shouldShowIconAlways = isIndexView;
|
|
|
|
return (
|
|
<>
|
|
<MenuItemWithOptionDropdown
|
|
text={view.name}
|
|
LeftIcon={getIcon(view.icon)}
|
|
onClick={() => handleViewSelect(view.id)}
|
|
isIconDisplayedOnHoverOnly={!shouldShowIconAlways}
|
|
RightIcon={getVisibilityIcon()}
|
|
dropdownPlacement="bottom-start"
|
|
dropdownId={`view-picker-options-${view.id}`}
|
|
dropdownContent={
|
|
<DropdownContent>
|
|
<DropdownMenuItemsContainer>
|
|
{isIndexView ? (
|
|
<MenuItem
|
|
LeftIcon={IconHeart}
|
|
text={isFavorite ? t`Manage favorite` : t`Add to Favorite`}
|
|
onClick={handleAddToFavorites}
|
|
/>
|
|
) : (
|
|
<>
|
|
<MenuItem
|
|
LeftIcon={IconHeart}
|
|
text={isFavorite ? t`Manage favorite` : t`Add to Favorite`}
|
|
onClick={handleAddToFavorites}
|
|
/>
|
|
|
|
{canEditView && (
|
|
<>
|
|
<MenuItem
|
|
LeftIcon={IconPencil}
|
|
text={t`Edit`}
|
|
onClick={(event) => {
|
|
onEdit(event, view.id);
|
|
closeDropdown(dropdownId);
|
|
}}
|
|
/>
|
|
{!isLastView && (
|
|
<MenuItem
|
|
LeftIcon={IconTrash}
|
|
text={t`Delete`}
|
|
onClick={handleDelete}
|
|
accent="danger"
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
</>
|
|
)}
|
|
</DropdownMenuItemsContainer>
|
|
</DropdownContent>
|
|
}
|
|
/>
|
|
</>
|
|
);
|
|
};
|