Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

5.0 KB
Raw Blame
import { useMediaQuery } from "@mantine/hooks";
import { Tooltip } from "@mantine/core";
import { ActionIcon, NavItem, NavSurface } from "@editor/ui";
import { SidebarToggleButton } from "@editor/components/shared/SidebarToggleButton";
import { Logo } from "@editor/ui/Logo";
import { NavFooter } from "@editor/components/shared/navFooter/NavFooter";
import { useAccountIdentity } from "@editor/hooks/useAccountIdentity";
import { useFreeCreditsSummary } from "@processor/hooks/useFreeCreditsSummary";
import { useAdminNavVisible } from "@processor/hooks/useAdminNavVisible";
import { useOpenPlan } from "@processor/hooks/useOpenPlan";
import { useTranslation } from "react-i18next";
import { useView, type ViewId } from "@processor/contexts/ViewContext";
import { useUI } from "@processor/contexts/UIContext";
import { LinkAccountFooterItem } from "@processor/components/LinkAccountFooterItem";
import { Icon } from "@editor/ui/Icon";
import {
  GROUP_PROCESSOR,
  GROUP_PLATFORM,
  type NavEntry,
  type NavGroup,
} from "@processor/components/sidebarGroups";
import "@processor/components/Sidebar.css";

// Empty groups are dropped: a flavor (or a move to the settings page) can leave
// one with nothing in it, and an empty card is not a section.
const NAV_SECTIONS: NavGroup[] = [
  { labelKey: "portal.nav.section.processor", entries: GROUP_PROCESSOR },
  { labelKey: "portal.nav.section.platform", entries: GROUP_PLATFORM },
].filter((section) => section.entries.length > 0);

/** Must match the shell breakpoint in AppShell.css / Sidebar.css. */
export const MOBILE_QUERY = "(max-width: 48rem)";

export function Sidebar() {
  const { activeView, setActiveView } = useView();
  const {
    mobileNavOpen,
    closeMobileNav,
    sidebarCollapsed,
    toggleSidebarCollapsed,
  } = useUI();
  const { t } = useTranslation();
  const isMobile = useMediaQuery(MOBILE_QUERY, false, {
    getInitialValueInEffect: false,
  });
  const { displayName, profilePictureUrl } = useAccountIdentity();
  const credits = useFreeCreditsSummary();
  const adminNavVisible = useAdminNavVisible();
  const openPlan = useOpenPlan();

  // Collapse is a desktop-only affordance: on mobile the sidebar is an
  // off-canvas drawer, so the icon-rail state never applies there.
  const collapsed = sidebarCollapsed && !isMobile;

  function renderGroup(entries: NavEntry[]) {
    return entries
      .filter((entry) => adminNavVisible || !entry.requiresAdmin)
      .map((entry) => {
        const label = t(`portal.nav.${entry.id}`);
        const item = (
          <NavItem
            key={entry.id}
            id={entry.id}
            label={label}
            icon={entry.icon}
            isActive={activeView === entry.id}
            onClick={(id) => {
              // Route changes also close the drawer (AppShell), but re-selecting the
              // active view or opening an external tab changes no route — close here.
              closeMobileNav();
              if (entry.externalUrl) {
                window.open(entry.externalUrl, "_blank", "noopener,noreferrer");
              } else {
                setActiveView(id as ViewId);
              }
            }}
          />
        );
        return collapsed ? (
          <Tooltip key={entry.id} label={label} position="right" withinPortal>
            <div className="portal-sidebar__navtip">{item}</div>
          </Tooltip>
        ) : (
          item
        );
      });
  }

  return (
    <aside
      className={
        mobileNavOpen ? "portal-sidebar portal-sidebar--open" : "portal-sidebar"
      }
      data-collapsed={collapsed || undefined}
      aria-label={t("portal.shell.sidebar.primaryNav")}
      // Off-canvas on mobile: remove from the tab order and accessibility tree.
      inert={isMobile && !mobileNavOpen}
    >
      <div className="portal-sidebar__header">
        {!collapsed && <Logo variant="textOnly" textHeight="1.3rem" />}

        <SidebarToggleButton
          collapsed={collapsed}
          onToggle={toggleSidebarCollapsed}
        />

        <ActionIcon
          variant="tertiary"
          className="portal-sidebar__close"
          aria-label={t("portal.shell.topbar.closeNav")}
          onClick={closeMobileNav}
        >
          <Icon name="x" size={18} />
        </ActionIcon>
      </div>

      <nav className="portal-sidebar__nav">
        {NAV_SECTIONS.map((section) => (
          <NavSurface
            key={section.labelKey}
            as="section"
            className="portal-sidebar__section"
          >
            <h2 className="portal-sidebar__section-label">
              {t(section.labelKey)}
            </h2>
            <div className="portal-sidebar__group">
              {renderGroup(section.entries)}
            </div>
          </NavSurface>
        ))}
      </nav>

      <NavFooter
        className="portal-sidebar__footer"
        displayName={displayName}
        profilePictureUrl={profilePictureUrl}
        showAccount={false}
        credits={credits}
        onOpenPlan={openPlan ?? undefined}
        accountExtras={<LinkAccountFooterItem />}
        collapsed={collapsed}
      />
    </aside>
  );
}