Skip to content

The open alternative to Salesforce, designed for AI.

5.9 KB
Raw Blame
import { type NavigationMenuItemSection } from '@/navigation-menu-item/common/types/NavigationMenuItemSection';
import { useLingui } from '@lingui/react/macro';
import { type NavigationMenuItemType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { v4 } from 'uuid';
import { type NavigationMenuItem } from '~/generated-metadata/graphql';

import { useCreateManyNavigationMenuItems } from '@/navigation-menu-item/common/hooks/useCreateManyNavigationMenuItems';
import { useDeleteManyNavigationMenuItems } from '@/navigation-menu-item/common/hooks/useDeleteManyNavigationMenuItems';
import { useUpdateManyNavigationMenuItems } from '@/navigation-menu-item/common/hooks/useUpdateManyNavigationMenuItems';
import { navigationMenuItemsDraftState } from '@/navigation-menu-item/common/states/navigationMenuItemsDraftState';
import { buildCreateNavigationMenuItemInput } from '@/navigation-menu-item/common/utils/buildCreateNavigationMenuItemInput';
import { computeInsertIndexAndPosition } from '@/navigation-menu-item/common/utils/computeInsertIndexAndPosition';
import { useNavigationMenuItemsData } from '@/navigation-menu-item/display/hooks/useNavigationMenuItemsData';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { useToast } from 'twenty-ui/components/feedback';

export type NewNavigationMenuItemInput = {
  type: NavigationMenuItemType;
  targetObjectMetadataId?: string | null;
  viewId?: string | null;
  pageLayoutId?: string | null;
  icon?: string | null;
  targetRecordId?: string | null;
  targetRecordIdentifier?: NavigationMenuItem['targetRecordIdentifier'];
  name?: string | null;
  link?: string | null;
  color?: string | null;
};

type CreateItemOptions = {
  targetFolderId?: string | null;
  targetIndex?: number;
};

// Workspace edits stay in the layout draft; favorites persist immediately.
// Mutation hooks roll back failed optimistic updates, so report errors here.
export const useNavigationMenuItemEditController = (
  section: NavigationMenuItemSection,
) => {
  const { t } = useLingui();
  const { enqueueToast } = useToast();
  const {
    navigationMenuItems,
    workspaceNavigationMenuItems,
    currentUserWorkspaceId,
  } = useNavigationMenuItemsData();
  const setNavigationMenuItemsDraft = useSetAtomState(
    navigationMenuItemsDraftState,
  );
  const { createManyNavigationMenuItems } = useCreateManyNavigationMenuItems();
  const { updateManyNavigationMenuItems } = useUpdateManyNavigationMenuItems();
  const { deleteManyNavigationMenuItems } = useDeleteManyNavigationMenuItems();

  const isDraftMode = section === 'workspace';
  const currentItems = isDraftMode
    ? workspaceNavigationMenuItems
    : navigationMenuItems;
  const targetUserWorkspaceId = isDraftMode
    ? undefined
    : currentUserWorkspaceId;

  const createItem = (
    input: NewNavigationMenuItemInput,
    { targetFolderId, targetIndex }: CreateItemOptions = {},
  ): string => {
    const folderId = targetFolderId ?? null;
    const itemsInFolder = currentItems.filter(
      (item) => (item.folderId ?? null) === folderId,
    );
    const index = targetIndex ?? itemsInFolder.length;
    const { flatIndex, position } = computeInsertIndexAndPosition(
      currentItems,
      folderId,
      index,
    );

    const id = v4();
    const newItem: NavigationMenuItem = {
      __typename: 'NavigationMenuItem',
      id,
      type: input.type,
      position,
      userWorkspaceId: targetUserWorkspaceId,
      folderId: folderId ?? undefined,
      targetObjectMetadataId: input.targetObjectMetadataId ?? undefined,
      targetRecordId: input.targetRecordId ?? undefined,
      targetRecordIdentifier: input.targetRecordIdentifier,
      viewId: input.viewId ?? undefined,
      pageLayoutId: input.pageLayoutId ?? undefined,
      icon: input.icon ?? undefined,
      name: input.name ?? undefined,
      link: input.link ?? undefined,
      color: input.color ?? undefined,
      applicationId: undefined,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };

    if (isDraftMode) {
      setNavigationMenuItemsDraft((draft) => {
        const current = draft ?? currentItems;
        return [
          ...current.slice(0, flatIndex),
          newItem,
          ...current.slice(flatIndex),
        ];
      });
      return id;
    }

    if (isDefined(targetUserWorkspaceId)) {
      void createManyNavigationMenuItems([
        {
          ...buildCreateNavigationMenuItemInput(newItem, (value) => value),
          userWorkspaceId: targetUserWorkspaceId,
        },
      ]).catch(() =>
        enqueueToast({
          status: 'error',
          children: t`Couldn't add to favorites`,
        }),
      );
    } else {
      enqueueToast({
        status: 'error',
        children: t`Couldn't add to favorites`,
      });
    }

    return id;
  };

  const updateItem = async (
    id: string,
    update: Partial<NavigationMenuItem>,
  ): Promise<void> => {
    if (isDraftMode) {
      setNavigationMenuItemsDraft((draft) =>
        isDefined(draft)
          ? draft.map((item) =>
              item.id === id ? { ...item, ...update } : item,
            )
          : draft,
      );
      return;
    }

    try {
      await updateManyNavigationMenuItems([{ id, update }]);
    } catch {
      enqueueToast({ status: 'error', children: t`Couldn't update favorite` });
    }
  };

  const deleteItems = async (ids: string[]): Promise<void> => {
    if (isDraftMode) {
      setNavigationMenuItemsDraft((draft) =>
        isDefined(draft)
          ? draft.filter((item) => !ids.includes(item.id))
          : draft,
      );
      return;
    }

    try {
      await deleteManyNavigationMenuItems(ids);
    } catch {
      enqueueToast({ status: 'error', children: t`Couldn't remove favorite` });
    }
  };

  return {
    currentItems,
    isDraftMode,
    targetUserWorkspaceId,
    createItem,
    updateItem,
    deleteItems,
  };
};