Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

21.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

"use client";

import { Trans, useLingui } from "@lingui/react/macro";
import { Slot, Slottable } from "@radix-ui/react-slot";
import type { ButtonHTMLAttributes, MouseEvent, ReactNode } from "react";
import {
  createContext,
  forwardRef,
  useCallback,
  useContext,
  useEffect,
  useRef,
  useState
} from "react";
import { LuPanelLeft } from "react-icons/lu";
import type { LinkProps } from "react-router";
import { Link, useLocation } from "react-router";
import { Avatar } from "./Avatar";
import { Drawer, DrawerContent, DrawerTitle } from "./Drawer";
import { useMode } from "./hooks/useMode";
import { IconButton } from "./IconButton";
import { PrefetchLink } from "./PrefetchLink";
import { Separator } from "./Separator";
import { ShortcutKey } from "./ShortcutKey";
import { useSidebar } from "./Sidebar";
import { SHORTCUTS } from "./shortcuts";
import { Tooltip, TooltipContent, TooltipTrigger } from "./Tooltip";
import { cn } from "./utils/cn";
import { VStack } from "./VStack";

const focusRingClasses = [
  "focus:!outline-none focus:!ring-0 active:!outline-none active:!ring-0",
  "after:pointer-events-none after:absolute after:-inset-[3px] after:rounded-lg after:border after:border-blue-500 after:opacity-0 after:ring-2 after:ring-blue-500/20 after:transition-opacity focus-visible:after:opacity-100 active:after:opacity-0"
];

export const navRailItemClasses = [
  "relative text-foreground/70 hover:text-foreground",
  "h-10 w-10 group-data-[state=expanded]:w-full",
  "flex items-center rounded-md",
  "group-data-[state=collapsed]:justify-center",
  "group-data-[state=expanded]:-space-x-2",
  "font-medium shrink-0 inline-flex items-center justify-center select-none",
  "disabled:opacity-50",
  "transition-[background-color,color,width] duration-100 ease-out",
  focusRingClasses,
  "group/item"
];

/**
 * The primary left navigation shared by the ERP and MES app shells: a 56px
 * icon rail that grows to 240px while a mouse hovers it or while it is pinned
 * open, and a left drawer below `md`. The toggle beside the `header` pins it,
 * as do ⌘B and a `SidebarTrigger`. Open state
 * comes from `SidebarProvider`, so it must be rendered inside one.
 *
 * Expanding the rail, by hover or by pin, pushes the page right rather than
 * covering it.
 */
// A pointer only passing over the rail (on its way to the page) shouldn't open it.
const HOVER_OPEN_DELAY_MS = 150;

// Lets an item freeze the rail as it is (open or collapsed) while a menu or
// popover it triggered is open, so a tap on a collapsed rail never opens it.
const NavRailHoldContext = createContext<(() => () => void) | null>(null);

/**
 * Freeze the rail while the menu or popover an item triggers is open. Radix
 * triggers pass their open state to the item as `data-state`.
 */
function useNavRailHold(triggerProps: object) {
  const isOpen =
    (triggerProps as Record<string, unknown>)["data-state"] === "open";
  const hold = useContext(NavRailHoldContext);
  useEffect(() => {
    if (isOpen && hold) return hold();
  }, [isOpen, hold]);
}

export function NavRail({
  children,
  header,
  footer,
  forceExpanded = false,
  disableHover = false
}: {
  children: ReactNode;
  header?: ReactNode;
  footer?: ReactNode;
  /** Hold the rail open regardless of hover or pin (e.g. while rearranging). */
  forceExpanded?: boolean;
  /** Ignore hover, e.g. while a modal owns the pointer. */
  disableHover?: boolean;
}) {
  const { open, isMobile, openMobile, setOpenMobile } = useSidebar();
  const { pathname } = useLocation();
  const [hovered, setHovered] = useState(false);
  const navRef = useRef<HTMLElement>(null);
  const hoverCardRef = useRef<HTMLSpanElement>(null);
  const lastPointerType = useRef<string>();
  const openTimer = useRef<ReturnType<typeof setTimeout>>();
  const cancelHoverOpen = useCallback(() => {
    clearTimeout(openTimer.current);
    openTimer.current = undefined;
  }, []);
  useEffect(() => cancelHoverOpen, [cancelHoverOpen]);

  // One hover card for the whole rail, moved with a transform to the item
  // under the pointer, so the highlight travels between items instead of
  // each item fading its own. Written straight to the element: a hover must
  // not re-render the rail.
  const moveHoverCard = useCallback((item: HTMLElement | null) => {
    const card = hoverCardRef.current;
    const nav = navRef.current;
    if (!card || !nav) return;
    if (!item) {
      card.style.opacity = "0";
      return;
    }
    const top =
      item.getBoundingClientRect().top -
      nav.getBoundingClientRect().top +
      nav.scrollTop;
    // Entering the rail: appear on the item rather than slide in from
    // wherever the card was last.
    card.style.transitionProperty = card.style.opacity === "1" ? "" : "opacity";
    card.style.transform = `translateY(${top}px)`;
    card.dataset.tone = item.dataset.hoverTone ?? "";
    card.style.opacity = "1";
  }, []);

  const [holds, setHolds] = useState(0);
  const hold = useCallback(() => {
    setHolds((n) => n + 1);
    return () => setHolds((n) => n - 1);
  }, []);

  // The rail is expanded while pinned OR hovered, and the toggle sits inside
  // it, so unpinning alone changed nothing: hover still held it open. An
  // unpin (the toggle, ⌘B or a page's `SidebarTrigger`) drops the hover too,
  // and a mouse still over the rail cannot reopen it until it leaves.
  const hoverSuppressed = useRef(false);
  const wasOpen = useRef(open);
  useEffect(() => {
    if (wasOpen.current && !open) {
      cancelHoverOpen();
      setHovered(false);
      hoverSuppressed.current =
        lastPointerType.current === "mouse" &&
        (navRef.current?.matches(":hover") ?? false);
    }
    wasOpen.current = open;
  }, [open, cancelHoverOpen]);

  // Links, go-to shortcuts and redirects all land here: whatever navigated,
  // the phone drawer must not stay over the destination.
  // biome-ignore lint/correctness/useExhaustiveDependencies: runs per navigation
  useEffect(() => {
    setOpenMobile(false);
  }, [pathname, setOpenMobile]);

  // A Radix dialog or menu sets pointer-events: none on document.body while
  // open, which fires a leave on the rail as it opens and a phantom enter as
  // it closes, so pointer events are ignored while blocked. A modal (search)
  // collapses the rail; a menu opened from the rail keeps it as it was. When
  // the block lifts, hover is re-read from where the pointer really is.
  const hoverBlocked = disableHover || holds > 0;
  useEffect(() => {
    if (disableHover) setHovered(false);
  }, [disableHover]);
  useEffect(() => {
    cancelHoverOpen();
    if (hoverBlocked) {
      moveHoverCard(null);
      return;
    }
    // Touch browsers leave `:hover` stuck on the last tapped element, so only
    // a mouse's `:hover` counts.
    const raf = requestAnimationFrame(() => {
      const over =
        lastPointerType.current === "mouse" &&
        (navRef.current?.matches(":hover") ?? false);
      // The pointer left while blocked: the leave was ignored, so end a
      // collapse's hold-off here.
      if (!over) hoverSuppressed.current = false;
      setHovered(over && !hoverSuppressed.current);
    });
    return () => cancelAnimationFrame(raf);
  }, [hoverBlocked, cancelHoverOpen, moveHoverCard]);

  const content = (
    <NavRailHoldContext.Provider value={hold}>
      <VStack spacing={1} className="flex flex-col justify-between h-full px-2">
        <VStack spacing={1}>
          {header ? (
            // As tall as the app's top bar (ERP `--topbar-height`, MES
            // `--header-height`) and as far down: the rail's padding plus the
            // content panel's 1px border. The logo centres on the bar.
            <div className="mt-px mb-2 flex h-[var(--topbar-height,var(--header-height,2.5rem))] w-full shrink-0 items-center gap-1">
              {header}
              <NavRailToggle />
            </div>
          ) : null}
          {children}
        </VStack>
        {footer ? <VStack spacing={1}>{footer}</VStack> : null}
      </VStack>
    </NavRailHoldContext.Provider>
  );

  if (isMobile) {
    // A tap on a link closes the sheet, also when it is the current page and
    // so no navigation follows.
    const closeOnLink = (event: MouseEvent<HTMLElement>) => {
      if ((event.target as Element).closest("a")) setOpenMobile(false);
    };
    // The footer (Clock In, My Hours, the user) stays pinned under the
    // scrolling groups, so it shows without a scroll.
    return (
      <Drawer open={openMobile} onOpenChange={setOpenMobile}>
        <DrawerContent position="left" size="content" className="p-0">
          <DrawerTitle className="px-6 py-4 max-md:px-4 max-md:text-[17px] max-md:font-semibold max-md:text-foreground">
            <Trans>Navigation</Trans>
          </DrawerTitle>
          <NavRailHoldContext.Provider value={hold}>
            <nav
              data-state="expanded"
              onClick={closeOnLink}
              className="group min-h-0 flex-1 overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-accent"
            >
              <VStack spacing={1} className="px-2">
                {header ? <div className="w-full pb-2">{header}</div> : null}
                {children}
              </VStack>
              {/* A static fade: it pins to the bottom while rows sit under
                  the footer, and is only end padding once scrolled down. */}
              <div
                aria-hidden
                className="pointer-events-none sticky bottom-0 h-8 bg-gradient-to-t from-card"
              />
            </nav>
            {footer ? (
              <div
                data-state="expanded"
                onClick={closeOnLink}
                className="group shrink-0 border-t border-border px-2 pt-2 pb-[max(0.5rem,env(safe-area-inset-bottom))]"
              >
                <VStack spacing={1}>{footer}</VStack>
              </div>
            ) : null}
          </NavRailHoldContext.Provider>
        </DrawerContent>
      </Drawer>
    );
  }

  const pinned = forceExpanded || open;
  const state = pinned || hovered ? "expanded" : "collapsed";

  return (
    // The wrapper is the rail's footprint in the layout: it grows with the
    // rail, pushing the page right. Sticky so it stays in view in shells
    // whose page scrolls as a whole.
    <div
      data-state={state}
      className={cn(
        "sticky top-0 h-svh z-50 hidden md:block shrink-0",
        "w-14 data-[state=expanded]:w-[15rem]",
        "transition-[width] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none"
      )}
    >
      <nav
        ref={navRef}
        data-state={state}
        data-sliding-hover=""
        className={cn(
          "absolute inset-y-0 left-0 bg-background py-2 group",
          "w-14 data-[state=expanded]:w-[15rem]",
          "transition-[width] duration-200 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none",
          "flex flex-col justify-between",
          "hide-scrollbar overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-accent"
        )}
        onPointerDown={(event) => {
          lastPointerType.current = event.pointerType;
        }}
        // The gaps between items are not items: the card stays where it is
        // while the pointer crosses one, and only leaves with the pointer.
        onPointerOver={(event) => {
          if (hoverBlocked || event.pointerType !== "mouse") return;
          const item = (event.target as HTMLElement).closest<HTMLElement>(
            "[data-nav-item]"
          );
          if (item) moveHoverCard(item);
        }}
        // Mouse only: a tap on a touch tablet must not expand the rail.
        onPointerMove={(event) => {
          lastPointerType.current = event.pointerType;
          if (hoverBlocked || hovered || openTimer.current) return;
          if (event.pointerType !== "mouse" || hoverSuppressed.current) return;
          openTimer.current = setTimeout(() => {
            openTimer.current = undefined;
            setHovered(true);
          }, HOVER_OPEN_DELAY_MS);
        }}
        // A mouse event, not `onPointerLeave`: Chrome can lose its pointer
        // boundary tracking for an element and then never send a pointer
        // leave for it (it re-sends `pointerenter` on every move instead)
        // while mouse leave events keep arriving. The rail stayed expanded
        // until a reload. `hovered` is only ever set by a mouse, so there is
        // no pointer type to check here.
        onMouseLeave={() => {
          if (hoverBlocked) return;
          // The leave a Radix layer causes by disabling body pointer-events
          // can arrive before this render knows about the hold.
          if (document.body.style.pointerEvents === "none") return;
          hoverSuppressed.current = false;
          cancelHoverOpen();
          setHovered(false);
          moveHoverCard(null);
        }}
      >
        <span
          ref={hoverCardRef}
          aria-hidden
          className={cn(
            "pointer-events-none absolute left-2 right-2 top-0 h-10 rounded-md opacity-0",
            "bg-active/60 data-[tone=accent]:bg-accent",
            "transition-[transform,opacity,background-color] duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none"
          )}
        />
        {content}
      </nav>
    </div>
  );
}

type NavRailItemProps = Omit<
  ButtonHTMLAttributes<HTMLButtonElement>,
  "children"
> & {
  icon: ReactNode;
  /** Also the accessible name; the visible label span is aria-hidden. */
  label: string;
  isActive?: boolean;
  tag?: ReactNode;
  /** Shown at the right edge only while expanded (e.g. a shortcut hint). */
  trailing?: ReactNode;
  asChild?: boolean;
  children?: ReactNode;
};

/**
 * One rail entry. A `<button>` by default; with `asChild` the child element
 * becomes the item, so a `Link` or a Radix trigger keeps its own semantics.
 */
export const NavRailItem = forwardRef<HTMLButtonElement, NavRailItemProps>(
  (
    {
      icon,
      label,
      isActive = false,
      tag,
      trailing,
      asChild = false,
      className,
      children,
      ...props
    },
    ref
  ) => {
    const Comp = asChild ? Slot : "button";

    useNavRailHold(props);

    return (
      <Comp
        ref={ref}
        type={asChild ? undefined : "button"}
        aria-label={label}
        data-nav-item=""
        {...props}
        className={cn(
          navRailItemClasses,
          isActive
            ? "bg-active text-active-foreground dark:shadow-button-base"
            : // On the desktop rail one card slides between the hovered
              // items instead; the active item keeps its own background.
              "hover:bg-active/60 hover:text-active-foreground group-data-[sliding-hover]:hover:bg-transparent",
          className
        )}
      >
        {/* A 16px icon centred in this 24px box sits where a `left-3 top-3`
            icon would; the box also fits an `Avatar size="xs"`. */}
        <span className="absolute left-2 top-2 flex size-6 items-center justify-center [&>svg]:size-4">
          {icon}
        </span>
        {tag ? (
          <span className="absolute top-1 right-1 min-w-4 h-4 px-1 rounded-full bg-primary text-primary-foreground text-[10px] font-medium leading-4 text-center tabular-nums max-md:top-1/2 max-md:right-3 max-md:h-5 max-md:min-w-5 max-md:-translate-y-1/2 max-md:text-xs max-md:leading-5">
            {tag}
          </span>
        ) : null}
        {/* Label and trailing share one row so a long label truncates
            before the trailing content instead of running under it. */}
        <span
          className={cn(
            "absolute left-7 right-3 min-w-32 group-data-[state=expanded]:left-12",
            "flex items-center gap-2",
            "md:opacity-0 group-data-[state=expanded]:opacity-100",
            tag ? "max-md:right-11" : null
          )}
        >
          <span
            aria-hidden
            className="min-w-0 flex-1 truncate text-sm text-left"
          >
            {label}
          </span>
          {trailing ? (
            <span className="pointer-events-none flex shrink-0 items-center">
              {trailing}
            </span>
          ) : null}
        </span>
        <Slottable>{children}</Slottable>
      </Comp>
    );
  }
);
NavRailItem.displayName = "NavRailItem";

export function NavRailLink({
  to,
  icon,
  label,
  isActive = false,
  tag,
  trailing,
  external = false,
  target,
  rel
}: {
  to: string;
  icon: ReactNode;
  label: string;
  isActive?: boolean;
  tag?: ReactNode;
  trailing?: ReactNode;
  external?: boolean;
  target?: LinkProps["target"];
  rel?: string;
}) {
  const Anchor = external ? Link : PrefetchLink;
  return (
    <NavRailItem
      asChild
      icon={icon}
      label={label}
      isActive={isActive}
      tag={tag}
      trailing={trailing}
    >
      <Anchor
        to={to}
        target={target}
        rel={rel}
        aria-current={isActive ? "page" : undefined}
      />
    </NavRailItem>
  );
}

type NavRailSwitcherProps = Omit<
  ButtonHTMLAttributes<HTMLButtonElement>,
  "children"
> & {
  /** The company's logo for each colour mode; its initials stand in for none. */
  logoLight?: string | null;
  logoDark?: string | null;
  /** Also the accessible name unless `aria-label` is given. */
  label: string;
  /** After the name, e.g. chevrons for a menu or an arrow for a link out. */
  trailing?: ReactNode;
  asChild?: boolean;
  children?: ReactNode;
};

/**
 * For `header`: the company. The logo sits in the icon column, so it stays put
 * as the rail grows, and the name fades in beside it. A menu trigger by
 * default; with `asChild`, a link. On phones it is a full-width row.
 */
export const NavRailSwitcher = forwardRef<
  HTMLButtonElement,
  NavRailSwitcherProps
>(
  (
    {
      logoLight,
      logoDark,
      label,
      trailing,
      asChild = false,
      className,
      children,
      ...props
    },
    ref
  ) => {
    const Comp = asChild ? Slot : "button";
    const logo = useMode() === "dark" ? logoDark : logoLight;
    // Its menu is a portal: without the hold, the rail collapses under it.
    useNavRailHold(props);

    return (
      <Comp
        ref={ref}
        type={asChild ? undefined : "button"}
        aria-label={label}
        {...props}
        className={cn(
          "relative flex h-10 shrink-0 items-center rounded-md select-none",
          // Fills the header row beside the toggle. Not on phones: there it
          // sits in a sheet's column, where flex-1 would collapse its height.
          "max-md:w-full md:w-10 md:group-data-[state=expanded]:min-w-0 md:group-data-[state=expanded]:flex-1",
          "text-sm font-medium text-foreground",
          "transition-[background-color] duration-100 ease-out hover:bg-active/60 data-[state=open]:bg-active/60",
          focusRingClasses,
          className
        )}
      >
        <span className="absolute left-2 top-2 flex size-6 items-center justify-center [&>svg]:size-4">
          {logo ? (
            <img src={logo} alt="" className="size-6 rounded object-contain" />
          ) : (
            <Avatar size="xs" name={label} />
          )}
        </span>
        <span
          aria-hidden
          className={cn(
            "absolute left-12 right-2 flex min-w-0 items-center gap-1",
            "md:opacity-0 transition-opacity duration-200 group-data-[state=expanded]:opacity-100"
          )}
        >
          <span className="min-w-0 flex-1 truncate text-left">{label}</span>
          {trailing ? (
            <span className="flex shrink-0 text-muted-foreground [&>svg]:size-3.5">
              {trailing}
            </span>
          ) : null}
        </span>
        <Slottable>{children}</Slottable>
      </Comp>
    );
  }
);
NavRailSwitcher.displayName = "NavRailSwitcher";

/**
 * Keeps the rail open (pinned) or lets it collapse back to icons on mouse
 * leave. Shown only while the rail is expanded: collapsed, it is only wide
 * enough for the logo, and hovering it expands it.
 */
function NavRailToggle() {
  const { t } = useLingui();
  const { open, toggleSidebar } = useSidebar();
  const label = open ? t`Collapse navigation` : t`Keep navigation open`;

  return (
    <Tooltip>
      <TooltipTrigger asChild>
        <IconButton
          aria-label={label}
          aria-pressed={open}
          icon={<LuPanelLeft />}
          variant="ghost"
          onClick={toggleSidebar}
          className="hidden shrink-0 text-muted-foreground group-data-[state=expanded]:inline-flex"
        />
      </TooltipTrigger>
      <TooltipContent side="right">
        <span className="flex items-center gap-2">
          {label}
          <ShortcutKey shortcut={SHORTCUTS.sidebarToggle} variant="small" />
        </span>
      </TooltipContent>
    </Tooltip>
  );
}

/** A titled section; collapsed, its title turns into a divider. */
export function NavRailGroup({
  label,
  children
}: {
  label: string;
  children: ReactNode;
}) {
  return (
    <VStack spacing={1}>
      <div className="relative h-7 w-full shrink-0">
        <span
          aria-hidden
          className="absolute inset-x-0 top-1/2 mx-auto h-px w-6 bg-border opacity-100 transition-opacity duration-200 group-data-[state=expanded]:opacity-0"
        />
        <span className="absolute inset-0 flex items-center px-2 text-[11px] font-medium uppercase tracking-wider text-foreground/50 whitespace-nowrap md:opacity-0 transition-opacity duration-200 group-data-[state=expanded]:opacity-100">
          {label}
        </span>
      </div>
      {children}
    </VStack>
  );
}

export function NavRailDivider() {
  return (
    <Separator className="my-1 mx-auto w-6 group-data-[state=expanded]:w-full transition-[width] duration-200" />
  );
}