Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.8 KB
Raw Blame
"use client";

import type { CSSProperties } from "react";
import { cn } from "@multica/ui/lib/utils";
import { SidebarTrigger, useSidebarSafe } from "@multica/ui/components/ui/sidebar";
import { ActionShortcutKeycaps } from "../common/shortcut-keycaps";
import {
  useWindowTitleBar,
  WINDOW_TITLE_BAR_CLASS,
  windowTitleBarStyle,
} from "../platform/window-title-bar";
import { useAppShell } from "./app-shell-context";

/**
 * The left edge every page shares: the header, the toolbar under it, and any
 * body row that has to line up with them.
 *
 * It is a constant rather than a per-page class because the pages drifted
 * apart exactly when it was one — collection pages sat at `px-5` and the
 * issues family at `px-4`, so switching tabs moved the title 4px. Import this
 * anywhere that edge matters instead of writing the class again; a page that
 * spells its own gutter is the bug coming back.
 */
export const PAGE_GUTTER = "px-[var(--page-padding-x,var(--space-md,1rem))]";

/**
 * The centred column a detail page reads inside: `runtimes`, and the agent and
 * skill detail pages. A detail page is a document about one entity, not a
 * table, so past a point more width stops helping and starts stretching a
 * two-column layout apart.
 *
 * Pair it with `PAGE_GUTTER` on the same element, or on a child that already
 * carries the gutter itself (a toolbar, a secondary nav rail). Every band on
 * the page — header, tab bar, banners, each panel — has to read this same
 * constant, because the bug it fixes is chrome sitting on the rail while the
 * content under it does not: below ~1730px they agree, and above it the tabs
 * walk away from what they label (MUL-7107).
 *
 * Full-width borders and backgrounds stay on the outer element; the rail goes
 * on the child that holds the content.
 *
 * Not for list surfaces that are genuinely tables — Issues, My Issues, member
 * detail — which stay full-bleed on `PAGE_GUTTER` alone.
 */
export const PAGE_RAIL = "mx-auto w-full max-w-[1440px]";

/**
 * The filter/actions row directly under a `PageHeader`: same height and
 * gutter so the two read as one chrome block. Shared for the same reason as
 * `PAGE_GUTTER` — the toolbars drifted when each page spelled its own row.
 */
export const PAGE_TOOLBAR = cn(
  "flex h-12 shrink-0 items-center justify-between gap-2",
  PAGE_GUTTER,
);

/**
 * The way back to the nav wherever it is not a permanent column: a sheet below
 * the compact breakpoint and, in the app shell, a collapsed space panel.
 *
 * Every surface needs one of these — unless the shell around it
 * already keeps one on screen. `PageHeader` supplies it for free, so this is
 * exported for the pages that build their own chrome instead; without it a
 * touch user has no way to reopen the nav at all.
 *
 * Renders nothing in the two cases where it would not be the way back:
 * outside a `SidebarProvider` (a page that stands alone, with no nav to
 * reopen), and under a shell that declares `hasExternalTrigger` — the desktop
 * window toolbar's trigger never scrolls away or hides, so a second copy in
 * every page header stacked two identical icons 50px apart, and a third
 * whenever a detail pane brought its own header along (MUL-6218).
 */
export function CollapsedNavTrigger() {
  const sidebar = useSidebarSafe();
  const shell = useAppShell();
  if (!sidebar || sidebar.hasExternalTrigger) return null;
  // In the app shell the rail is always on screen from `md` up, so the
  // trigger only has a job there while the space panel or list column is
  // collapsed. The `md:hidden` gate is CSS so the compact case is right
  // before hydration.
  const collapsed = shell
    ? shell.hasPanel
      ? sidebar.state === "collapsed"
      : shell.listCollapsed
    : false;
  const className = shell ? (collapsed ? undefined : "md:hidden") : "lg:hidden";
  return (
    <SidebarTrigger
      className={className}
      shortcut={<ActionShortcutKeycaps action="toggleSidebar" />}
      onClick={
        shell
          ? (event) => {
              event.preventDefault();
              shell.toggleNav();
            }
          : undefined
      }
    />
  );
}

/**
 * What a page's top row needs when the window has no title bar of its own
 * (see `WindowTitleBarProvider`): the drag region, and a leading inset past
 * the native window controls. Empty everywhere else.
 *
 * `PageHeader` applies it itself. A surface that draws its own header-shaped
 * row in that slot (a loading or error state standing in for the page) spreads
 * it too, and renders the row even when it has nothing to put in it, so the
 * window stays draggable whichever state is on screen.
 */
export function usePageHeaderTitleBar(): {
  active: boolean;
  className?: string;
  style?: CSSProperties;
} {
  const titleBar = useWindowTitleBar();
  if (!titleBar) return { active: false };
  return {
    active: true,
    className: WINDOW_TITLE_BAR_CLASS,
    style: windowTitleBarStyle(titleBar),
  };
}

interface PageHeaderProps {
  children: React.ReactNode;
  /**
   * Replaces the mobile sidebar trigger at the far left; `false` leaves the
   * slot empty.
   *
   * Replacing it takes the nav away from a phone on this screen, so only a
   * surface whose own chrome still reaches the nav should. A drill-in detail
   * is not one: a "back to the list" here locks the reader out of the nav
   * sheet, which is where the Inbox's list lives (MUL-10233).
   */
  leading?: React.ReactNode;
  className?: string;
}

/**
 * Push actions right with `flex-1` on the content group, never with
 * `justify-between` on the header.
 *
 * The leading slot below is a flex item too, so a header that reads as two
 * zones in source is three at runtime, and `justify-between` splits the free
 * space on BOTH sides of the title — parking it mid-header. Narrow windows
 * sit below `lg`, where the trigger renders, so that is where it surfaces.
 *
 * `gap-2` is the base for the same reason: it makes the header's own gap the
 * single source of the leading-slot spacing. When the trigger carried its own
 * margin as well, every header that declared a gap paid both and its title
 * started further right than the ones that declared none. Override the gap
 * per header if the zones need more air; do not add margin to the trigger.
 *
 * Do not pass a `px-*` through `className`. The gutter is `PAGE_GUTTER` for
 * every page, and the toolbar beneath the header reads the same constant.
 */
export function PageHeader({ children, leading, className }: PageHeaderProps) {
  const titleBar = usePageHeaderTitleBar();
  return (
    <header
      data-mc="page-header"
      className={cn(
        "flex h-12 shrink-0 items-center gap-2 border-b",
        className,
        PAGE_GUTTER,
        titleBar.className,
      )}
      style={titleBar.style}
    >
      {leading ?? <CollapsedNavTrigger />}
      {children}
    </header>
  );
}