Skip to content

The open alternative to Salesforce, designed for AI.

7.3 KB
Raw Blame
import { SettingsPath } from 'twenty-shared/types';

import { useAuth } from '@/auth/hooks/useAuth';
import { currentUserState } from '@/auth/states/currentUserState';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { billingState } from '@/client-config/states/billingState';
import { supportChatState } from '@/client-config/states/supportChatState';
import { permissionFlagMapSelector } from '@/settings/roles/states/permissionFlagMapSelector';
import { getDocumentationUrl } from '@/support/utils/getDocumentationUrl';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
import { type NavigationDrawerItemModifier } from '@/ui/navigation/navigation-drawer/types/NavigationDrawerItemModifier';
import { type NavigationDrawerItemIndentationLevel } from '@/ui/navigation/navigation-drawer/types/NavigationDrawerItemIndentationLevel';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { isNonEmptyString } from '@sniptt/guards';
import {
  IconAppWindow,
  IconApps,
  IconAt,
  IconCalendarEvent,
  IconCode,
  IconColorSwatch,
  type IconComponent,
  IconCreditCard,
  IconDoorEnter,
  IconHelpCircle,
  IconHierarchy,
  IconMail,
  IconMessage,
  IconMessageCircle,
  IconServer,
  IconSettings2,
  IconSparkles,
  IconUserCircle,
  IconUsers,
} from 'twenty-ui/icon';
import {
  FeatureFlagKey,
  PermissionFlagType,
} from '~/generated-metadata/graphql';
import { openUrlInNewTab } from '~/utils/openUrlInNewTab';
import { useLingui } from '@lingui/react/macro';

export type SettingsNavigationSection = {
  label: string;
  items: SettingsNavigationItem[];
};

export type SettingsNavigationItem = {
  label: string;
  path?: SettingsPath;
  onClick?: () => void;
  Icon: IconComponent;
  indentationLevel?: NavigationDrawerItemIndentationLevel;
  matchSubPages?: boolean;
  isHidden?: boolean;
  subItems?: SettingsNavigationItem[];
  modifier?: NavigationDrawerItemModifier;
};

const useSettingsNavigationItems = (): SettingsNavigationSection[] => {
  const { t } = useLingui();
  const billing = useAtomStateValue(billingState);
  const { signOut } = useAuth();
  const supportChat = useAtomStateValue(supportChatState);
  const currentWorkspaceMember = useAtomStateValue(currentWorkspaceMemberState);

  const isBillingEnabled = billing?.isBillingEnabled ?? false;
  const currentUser = useAtomStateValue(currentUserState);
  const isAdminEnabled =
    (currentUser?.canImpersonate || currentUser?.canAccessFullAdminPanel) ??
    false;
  const isSupportChatConfigured =
    supportChat?.supportDriver === 'FRONT' &&
    isNonEmptyString(supportChat.supportFrontChatId);

  const permissionFlagMap = useAtomStateValue(permissionFlagMapSelector);
  const isAppPreferencesEnabled = useIsFeatureEnabled(
    FeatureFlagKey.IS_APP_PREFERENCES_ENABLED,
  );

  return [
    {
      label: t`User`,
      items: [
        {
          label: t`Profile`,
          path: SettingsPath.ProfilePage,
          Icon: IconUserCircle,
        },
        {
          label: t`Experience`,
          path: SettingsPath.Experience,
          Icon: IconColorSwatch,
        },
        {
          label: isAppPreferencesEnabled ? t`Apps preferences` : t`Accounts`,
          path: isAppPreferencesEnabled
            ? SettingsPath.AppPreferences
            : SettingsPath.Accounts,
          Icon: isAppPreferencesEnabled ? IconApps : IconAt,
          isHidden: !permissionFlagMap[PermissionFlagType.CONNECTED_ACCOUNTS],
          subItems: isAppPreferencesEnabled
            ? undefined
            : [
                {
                  label: t`Emails`,
                  path: SettingsPath.AccountsEmails,
                  Icon: IconMail,
                  isHidden:
                    !permissionFlagMap[PermissionFlagType.CONNECTED_ACCOUNTS],
                  indentationLevel: 2,
                },
                {
                  label: t`Calendars`,
                  path: SettingsPath.AccountsCalendars,
                  Icon: IconCalendarEvent,
                  isHidden:
                    !permissionFlagMap[PermissionFlagType.CONNECTED_ACCOUNTS],
                  indentationLevel: 2,
                },
              ],
        },
      ],
    },
    {
      label: t`Workspace`,
      items: [
        {
          label: t`General`,
          path: SettingsPath.General,
          Icon: IconSettings2,
          isHidden: !permissionFlagMap[PermissionFlagType.WORKSPACE],
        },
        {
          label: t`Data model`,
          path: SettingsPath.Objects,
          Icon: IconHierarchy,
          isHidden: !permissionFlagMap[PermissionFlagType.DATA_MODEL],
        },
        {
          label: t`Layout`,
          path: SettingsPath.Layout,
          Icon: IconAppWindow,
          isHidden: !permissionFlagMap[PermissionFlagType.LAYOUTS],
        },
        {
          label: t`Members`,
          path: SettingsPath.WorkspaceMembersPage,
          Icon: IconUsers,
          isHidden: !permissionFlagMap[PermissionFlagType.WORKSPACE_MEMBERS],
        },
        {
          label: t`Billing`,
          path: SettingsPath.Billing,
          Icon: IconCreditCard,
          isHidden:
            !isBillingEnabled ||
            !permissionFlagMap[PermissionFlagType.WORKSPACE],
        },
        {
          label: t`Developers`,
          path: SettingsPath.Developers,
          Icon: IconCode,
          isHidden:
            !permissionFlagMap[PermissionFlagType.API_KEYS_AND_WEBHOOKS],
        },
        // TODO: Re-enable when integrations page is ready
        // {
        //   label: t`Integrations`,
        //   path: SettingsPath.Integrations,
        //   Icon: IconApps,
        //   isHidden: !permissionFlagMap[PermissionFlagType.API_KEYS_AND_WEBHOOKS],
        // },
        {
          label: t`Apps`,
          path: SettingsPath.Applications,
          Icon: IconApps,
          isHidden: !permissionFlagMap[PermissionFlagType.APPLICATIONS],
        },
        {
          label: t`AI`,
          path: SettingsPath.AI,
          Icon: IconSparkles,
          isHidden: !permissionFlagMap[PermissionFlagType.AI_SETTINGS],
        },
        {
          label: t`Communication`,
          path: SettingsPath.WorkspaceCommunications,
          Icon: IconMessageCircle,
          isHidden: !permissionFlagMap[PermissionFlagType.WORKSPACE],
        },
      ],
    },
    {
      label: t`Other`,
      items: [
        {
          label: t`Admin Panel`,
          path: SettingsPath.AdminPanel,
          Icon: IconServer,
          isHidden: !isAdminEnabled,
        },
        {
          label: t`Community`,
          path: SettingsPath.Community,
          Icon: IconUsers,
          isHidden: !permissionFlagMap[PermissionFlagType.WORKSPACE],
        },
        {
          label: t`Support`,
          onClick: () => window.FrontChat?.('show'),
          Icon: IconMessage,
          isHidden: !isSupportChatConfigured,
        },
        {
          label: t`Documentation`,
          onClick: () =>
            openUrlInNewTab(
              getDocumentationUrl({ locale: currentWorkspaceMember?.locale }),
            ),
          Icon: IconHelpCircle,
        },
        {
          label: t`Logout`,
          onClick: signOut,
          Icon: IconDoorEnter,
          matchSubPages: false,
        },
      ],
    },
  ];
};

export { useSettingsNavigationItems };