Skip to content

The open alternative to Salesforce, designed for AI.

8.7 KB
Raw Blame
import { NAVIGATION_DRAWER_COLLAPSED_BUTTON_SIZE } from '@/ui/navigation/navigation-drawer/constants/NavigationDrawerCollapsedButtonSize';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { motion, useReducedMotion } from 'framer-motion';
import { Badge } from 'twenty-ui/primitives/data-display';
import { Tooltip } from 'twenty-ui/primitives/surfaces';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';

import { useAgentChatOpenThreadsSummary } from '@/ai/hooks/useAgentChatOpenThreadsSummary';
import { isLayoutCustomizationModeEnabledState } from '@/layout-customization/states/isLayoutCustomizationModeEnabledState';
import { useActiveNavigationDrawerMode } from '@/navigation/hooks/useActiveNavigationDrawerMode';
import { useIsNavigationDrawerContentExpanded } from '@/ui/navigation/navigation-drawer/hooks/useIsNavigationDrawerContentExpanded';
import { useNavigationDrawerModes } from '@/navigation/hooks/useNavigationDrawerModes';
import { useSwitchNavigationDrawerMode } from '@/navigation/hooks/useSwitchNavigationDrawerMode';
import { TooltipDelay } from '@/ui/layout/tooltip/constants/TooltipDelay';
import {
  type NavigationDrawerActiveTab,
  NAVIGATION_DRAWER_TABS,
} from '@/ui/navigation/states/navigationDrawerTabs';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
import { FeatureFlagKey } from '~/generated-metadata/graphql';
import { useIsMobile } from 'twenty-ui/utilities';

const MAX_DISPLAYED_UNREAD_COUNT = 99;

// Expanded, the row matches the page card header so their borders read as one line.
const StyledSwitcher = styled.div<{ isExpanded: boolean }>`
  align-items: ${({ isExpanded }) => (isExpanded ? 'center' : 'flex-start')};
  border-bottom: ${({ isExpanded }) =>
    isExpanded ? `1px solid ${themeCssVariables.border.color.light}` : 'none'};
  box-sizing: border-box;
  display: flex;
  flex-direction: ${({ isExpanded }) => (isExpanded ? 'row' : 'column')};
  gap: ${({ isExpanded }) =>
    isExpanded
      ? themeCssVariables.spacing['0.5']
      : themeCssVariables.betweenSiblingsGap};
  height: ${({ isExpanded }) =>
    isExpanded ? themeCssVariables.spacing[10] : 'auto'};
  min-width: 0;
`;

const StyledMode = styled.button<{ isActive: boolean; isExpanded: boolean }>`
  align-items: center;
  background: ${({ isActive }) =>
    isActive ? themeCssVariables.background.transparent.light : 'transparent'};
  border: none;
  border-radius: ${({ isExpanded }) =>
    isExpanded
      ? themeCssVariables.border.radius.smRound
      : themeCssVariables.border.radius.mdRound};
  color: ${({ isActive }) =>
    isActive
      ? themeCssVariables.font.color.primary
      : themeCssVariables.font.color.tertiary};
  corner-shape: round;
  cursor: pointer;
  display: flex;
  // Only the labelled mode may shrink: long translations otherwise push Settings off the drawer.
  flex-shrink: ${({ isActive, isExpanded }) =>
    isActive && isExpanded ? 1 : 0};
  font-family: inherit;
  font-size: ${themeCssVariables.font.size.md};
  font-weight: ${themeCssVariables.font.weight.medium};
  gap: ${({ isActive, isExpanded }) =>
    isActive && isExpanded ? themeCssVariables.spacing[1] : '0'};
  height: ${({ isExpanded }) =>
    isExpanded
      ? themeCssVariables.spacing[7]
      : `${NAVIGATION_DRAWER_COLLAPSED_BUTTON_SIZE}px`};
  justify-content: ${({ isExpanded }) =>
    isExpanded ? 'flex-start' : 'center'};
  min-width: 0;
  padding: ${({ isExpanded }) =>
    isExpanded ? `0 ${themeCssVariables.spacing['1.5']}` : '0'};
  transition:
    background calc(${themeCssVariables.animation.duration.fast} * 1s) ease,
    color calc(${themeCssVariables.animation.duration.fast} * 1s) ease,
    gap calc(${themeCssVariables.animation.duration.normal} * 1s) ease;
  width: ${({ isExpanded }) =>
    isExpanded ? 'auto' : `${NAVIGATION_DRAWER_COLLAPSED_BUTTON_SIZE}px`};

  &[aria-disabled='true'] {
    color: ${themeCssVariables.font.color.light};
    cursor: not-allowed;
  }

  &:hover:not([aria-disabled='true']) {
    background: ${({ isActive }) =>
      isActive
        ? themeCssVariables.background.transparent.light
        : themeCssVariables.background.transparent.lighter};
    color: ${themeCssVariables.font.color.primary};
  }

  &:focus-visible {
    outline: 2px solid ${themeCssVariables.color.blue};
    outline-offset: 1px;
  }
`;

const StyledModeIcon = styled.span`
  align-items: center;
  display: flex;
  flex-shrink: 0;
  height: ${themeCssVariables.spacing[4]};
  justify-content: center;
  position: relative;
  width: ${themeCssVariables.spacing[4]};
`;

const StyledModeLabelBase = styled.span`
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

const StyledModeLabel = motion.create(StyledModeLabelBase);

const StyledUnreadBadge = styled.span`
  pointer-events: none;
  position: absolute;
  right: calc(-1 * ${themeCssVariables.spacing['1.5']});
  top: calc(-1 * ${themeCssVariables.spacing['1.5']});
`;

export const MainNavigationDrawerModeSwitcher = () => {
  const { t } = useLingui();
  const theme = useTheme();

  const isLayoutCustomizationModeEnabled = useAtomStateValue(
    isLayoutCustomizationModeEnabledState,
  );
  const isMobile = useIsMobile();
  const isExpanded = useIsNavigationDrawerContentExpanded();
  const modes = useNavigationDrawerModes();
  const activeNavigationDrawerMode = useActiveNavigationDrawerMode();
  const { switchNavigationDrawerMode } = useSwitchNavigationDrawerMode();
  const shouldReduceMotion = useReducedMotion();
  const { unreadOpenThreadCount } = useAgentChatOpenThreadsSummary();
  const isAiChatInboxEnabled = useIsFeatureEnabled(
    FeatureFlagKey.IS_AI_CHAT_INBOX_ENABLED,
  );

  const getDisabledModeTooltip = (mode: NavigationDrawerActiveTab) => {
    if (mode === NAVIGATION_DRAWER_TABS.SETTINGS) {
      return t`Finish editing the layout to open Settings`;
    }

    return isAiChatInboxEnabled
      ? t`Finish editing the layout to open Inbox`
      : t`Finish editing the layout to open AI`;
  };

  if (modes.length === 0) {
    return null;
  }

  const shouldShowTooltips = !isExpanded && !isMobile;

  return (
    <StyledSwitcher
      isExpanded={isExpanded}
      role="group"
      aria-label={t`Navigation modes`}
    >
      {modes.map(({ Icon, label, mode }) => {
        const isActive = mode === activeNavigationDrawerMode;
        // Inside the inbox, its Open item already shows what is unread
        const isUnread =
          isAiChatInboxEnabled &&
          mode === NAVIGATION_DRAWER_TABS.AI_CHAT_HISTORY &&
          unreadOpenThreadCount > 0 &&
          !isActive;
        const isDisabled =
          mode !== NAVIGATION_DRAWER_TABS.NAVIGATION_MENU &&
          isLayoutCustomizationModeEnabled;

        return (
          <Tooltip
            key={mode}
            content={isDisabled ? getDisabledModeTooltip(mode) : label}
            disabled={!shouldShowTooltips && !isDisabled}
            delay={TooltipDelay.noDelay}
            side={isExpanded ? 'bottom' : 'right'}
            positionMethod="fixed"
          >
            <StyledMode
              type="button"
              isActive={isActive}
              isExpanded={isExpanded}
              aria-label={
                isUnread ? t`${label}, ${unreadOpenThreadCount} unread` : label
              }
              aria-current={isActive || undefined}
              aria-disabled={isDisabled}
              onClick={() => {
                if (isDisabled) {
                  return;
                }

                switchNavigationDrawerMode(mode);
              }}
            >
              <StyledModeIcon>
                <Icon size={theme.icon.size.md} />
                {isUnread && (
                  <Badge
                    render={<StyledUnreadBadge />}
                    aria-hidden="true"
                    size="xs"
                    color="primary"
                    shape={unreadOpenThreadCount < 10 ? 'circle' : 'pill'}
                  >
                    {unreadOpenThreadCount > MAX_DISPLAYED_UNREAD_COUNT
                      ? `${MAX_DISPLAYED_UNREAD_COUNT}+`
                      : unreadOpenThreadCount}
                  </Badge>
                )}
              </StyledModeIcon>
              <StyledModeLabel
                initial={false}
                animate={{ width: isExpanded && isActive ? 'auto' : 0 }}
                transition={{
                  duration: shouldReduceMotion
                    ? 0
                    : theme.animation.duration.normal,
                  ease: 'easeInOut',
                }}
              >
                {label}
              </StyledModeLabel>
            </StyledMode>
          </Tooltip>
        );
      })}
    </StyledSwitcher>
  );
};