Skip to content

The open alternative to Salesforce, designed for AI.

7.8 KB
Raw Blame
'use client';

import { styled } from '@linaria/react';
import {
  IconChevronDown,
  IconLayoutSidebarLeftCollapse,
  IconSearch,
} from '@tabler/icons-react';

import { AppleWorkspaceMark } from '@/icons';
import { mediaUp } from '@/tokens';
import { THEME_LIGHT } from 'twenty-ui/theme';
import { previewFontSize } from '@/app-preview/preview-font-size';
import { APP_PREVIEW_CHROME } from '@/app-preview/app-preview-chrome';

import { SidebarFolder } from './SidebarFolder';
import { SidebarItem } from './SidebarItem';
import { SidebarModeSwitcher } from './SidebarModeSwitcher';
import { renderPreviewIcon } from '../primitives/PreviewIcon';
import { isSidebarFolder } from './is-sidebar-folder';
import { type SidebarEntry, type SidebarItemDef } from '../types';

const DRAWER_PADDING_PX = APP_PREVIEW_CHROME.navigationDrawerPaddingPx;

const SidebarPanel = styled.aside`
  background: ${THEME_LIGHT.grayScale.gray3};
  border-right: 1px solid ${THEME_LIGHT.border.color.medium};
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
  width: 48px;

  ${mediaUp('md')} {
    &[data-desktop-mode='expanded'] {
      width: ${APP_PREVIEW_CHROME.navigationDrawerWidthPx}px;
    }
  }
`;

const Header = styled.div`
  align-items: center;
  display: flex;
  flex-direction: column;
  padding: ${DRAWER_PADDING_PX}px 4px;
  row-gap: 16px;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      border-bottom: 1px solid ${THEME_LIGHT.border.color.medium};
      box-sizing: border-box;
      flex-direction: row;
      height: ${APP_PREVIEW_CHROME.appHeaderHeightPx}px;
      justify-content: space-between;
      padding: 0 ${DRAWER_PADDING_PX}px;
      row-gap: 0;
    }
  }
`;

const HeaderLeft = styled.div`
  align-items: center;
  display: flex;
  justify-content: center;
  width: 100%;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      column-gap: ${DRAWER_PADDING_PX}px;
      justify-content: flex-start;
      padding: 0 4px;
      width: auto;
    }
  }
`;

const WorkspaceName = styled.span`
  display: none;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      display: block;
      color: ${THEME_LIGHT.font.color.primary};
      font-family: var(--font-product), sans-serif;
      font-size: ${previewFontSize(THEME_LIGHT.font.size.md)};
      font-weight: ${THEME_LIGHT.font.weight.medium};
      line-height: 1.4;
    }
  }
`;

const DesktopOnly = styled.span`
  display: none;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      display: flex;
    }
  }
`;

const HeaderRight = styled.div`
  align-items: center;
  display: flex;
  flex-direction: column;
  row-gap: 16px;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      column-gap: 2px;
      flex-direction: row;
    }
  }
`;

const HeaderButton = styled.span`
  align-items: center;
  display: flex;
  height: 24px;
  justify-content: center;
  width: 24px;
`;

const DrawerContent = styled.div`
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  padding: 0 4px ${DRAWER_PADDING_PX}px;
  row-gap: ${DRAWER_PADDING_PX}px;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      padding: 0 ${DRAWER_PADDING_PX}px ${DRAWER_PADDING_PX * 2}px;
    }
  }
`;

const SectionLabelRow = styled.div`
  align-items: center;
  display: flex;
  height: 28px;
  padding-left: 4px;
  padding-right: 2px;
`;

const SectionLabel = styled.span`
  color: ${THEME_LIGHT.font.color.light};
  font-family: var(--font-product), sans-serif;
  font-size: 11px;
  font-weight: ${THEME_LIGHT.font.weight.semiBold};
  line-height: 1;
`;

const SectionStack = styled.div`
  display: flex;
  flex-direction: column;
  min-height: 0;
  row-gap: 2px;
`;

// Mobile renders the icon rail; desktop the expanded panel.
const RailOnly = styled.div`
  align-items: center;
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  row-gap: ${DRAWER_PADDING_PX}px;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      display: none;
    }
  }
`;

const DesktopContent = styled.div`
  display: none;

  ${mediaUp('md')} {
    [data-desktop-mode='expanded'] & {
      display: flex;
      flex: 1 1 auto;
      flex-direction: column;
      min-height: 0;
      row-gap: 12px;
    }
  }
`;

const RailIconSlot = styled.div`
  align-items: center;
  display: flex;
  height: 28px;
  justify-content: center;
  width: 100%;
`;

export type DesktopSidebarMode = 'expanded' | 'collapsed';

export function PreviewSidebar({
  desktopMode = 'expanded',
  favorites,
  highlightedItemId = null,
  onSelectPageItem,
  onToggleFolder,
  openFolderIds,
  selectedItemId,
  workspace,
}: {
  desktopMode?: DesktopSidebarMode;
  favorites: SidebarItemDef[];
  highlightedItemId?: string | null;
  onSelectPageItem?: (itemId: string) => void;
  onToggleFolder?: (folderId: string) => void;
  openFolderIds?: string[];
  selectedItemId: string;
  workspace: SidebarEntry[];
}) {
  const visibleWorkspace = workspace.filter((entry) =>
    isSidebarFolder(entry) ? true : !entry.hidden,
  );
  const railItems = visibleWorkspace.filter(
    (entry): entry is SidebarItemDef => !isSidebarFolder(entry),
  );

  return (
    <SidebarPanel aria-hidden data-desktop-mode={desktopMode}>
      <Header>
        <HeaderLeft>
          <AppleWorkspaceMark sizePx={16} />
          <WorkspaceName>Apple</WorkspaceName>
          <DesktopOnly>
            <IconChevronDown
              color={THEME_LIGHT.font.color.extraLight}
              size={16}
            />
          </DesktopOnly>
        </HeaderLeft>
        <HeaderRight>
          <IconSearch color={THEME_LIGHT.font.color.secondary} size={16} />
          <DesktopOnly>
            <HeaderButton>
              <IconLayoutSidebarLeftCollapse
                color={THEME_LIGHT.font.color.secondary}
                size={16}
              />
            </HeaderButton>
          </DesktopOnly>
        </HeaderRight>
      </Header>
      <DrawerContent>
        <SidebarModeSwitcher />
        <RailOnly>
          {railItems.map((item) => (
            <RailIconSlot data-rail-item-id={item.id} key={item.id}>
              {renderPreviewIcon(item.icon)}
            </RailIconSlot>
          ))}
        </RailOnly>
        <DesktopContent>
          <div>
            <SectionLabelRow>
              <SectionLabel>Favorites</SectionLabel>
            </SectionLabelRow>
            <SectionStack>
              {favorites.map((item) => (
                <SidebarItem
                  active={item.id === selectedItemId}
                  item={item}
                  key={item.id}
                  onSelect={onSelectPageItem}
                />
              ))}
            </SectionStack>
          </div>
          <div>
            <SectionLabelRow>
              <SectionLabel>Workspace</SectionLabel>
            </SectionLabelRow>
            <SectionStack>
              {visibleWorkspace.map((entry) =>
                isSidebarFolder(entry) ? (
                  <SidebarFolder
                    expanded={openFolderIds?.includes(entry.id) ?? false}
                    folder={entry}
                    key={entry.id}
                    onSelectItem={onSelectPageItem}
                    onToggleExpanded={
                      onToggleFolder
                        ? () => onToggleFolder(entry.id)
                        : undefined
                    }
                    selectedItemId={selectedItemId}
                  />
                ) : (
                  <SidebarItem
                    active={entry.id === selectedItemId}
                    highlighted={entry.id === highlightedItemId}
                    item={entry}
                    key={entry.id}
                    onSelect={onSelectPageItem}
                  />
                ),
              )}
            </SectionStack>
          </div>
        </DesktopContent>
      </DrawerContent>
    </SidebarPanel>
  );
}