Files
twenty/packages/twenty-front/src/modules/navigation-menu-item/hooks/useAddFolderToNavigationMenuDraft.ts
T
Abdul Rahman 9fe2a07c55 Navbar customization v2 (#18026)
Adds color support for navigation menu items.

---------

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Devessier <baptiste@devessier.fr>
Co-authored-by: Charles Bochet <charles@twenty.com>
2026-02-28 11:48:28 +01:00

64 lines
2.0 KiB
TypeScript

import { isDefined } from 'twenty-shared/utils';
import { v4 } from 'uuid';
import type { NavigationMenuItem } from '~/generated-metadata/graphql';
import { DEFAULT_NAVIGATION_MENU_ITEM_COLOR_FOLDER } from '@/navigation-menu-item/constants/NavigationMenuItemDefaultColorFolder';
import { navigationMenuItemsDraftState } from '@/navigation-menu-item/states/navigationMenuItemsDraftState';
import { computeInsertIndexAndPosition } from '@/navigation-menu-item/utils/computeInsertIndexAndPosition';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
export const useAddFolderToNavigationMenuDraft = () => {
const setNavigationMenuItemsDraft = useSetAtomState(
navigationMenuItemsDraftState,
);
const addFolderToDraft = (
name: string,
currentDraft: NavigationMenuItem[],
targetFolderId?: string | null,
targetIndex?: number,
): string => {
const folderId = targetFolderId ?? null;
const itemsInFolder = currentDraft.filter(
(item) =>
(item.folderId ?? null) === folderId &&
!isDefined(item.userWorkspaceId),
);
const index = targetIndex ?? itemsInFolder.length;
const { flatIndex, position } = computeInsertIndexAndPosition(
currentDraft,
folderId,
index,
);
const newItemId = v4();
const newItem: NavigationMenuItem = {
__typename: 'NavigationMenuItem',
id: newItemId,
viewId: undefined,
targetObjectMetadataId: undefined,
targetRecordId: undefined,
folderId: folderId ?? undefined,
position,
userWorkspaceId: undefined,
name: name.trim(),
color: DEFAULT_NAVIGATION_MENU_ITEM_COLOR_FOLDER,
applicationId: undefined,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
const newDraft = [
...currentDraft.slice(0, flatIndex),
newItem,
...currentDraft.slice(flatIndex),
];
setNavigationMenuItemsDraft(newDraft);
return newItemId;
};
return { addFolderToDraft };
};