Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

4.4 KB
Raw Blame
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import { Outlet, useLocation, useMatches } from "react-router";
import type { DashboardShellTargets } from "@/app/dashboard-shell";
import {
  PROJECT_V1_ROUTE_DASHBOARD,
  WORKSPACE_ROOT_MODULE,
  WORKSPACE_ROUTE_MY_ISSUES,
} from "@/app/router/handles";
import { DashboardBodyShell } from "@/components/DashboardBodyShell";
import { DashboardSidebar } from "@/components/DashboardSidebar";
import { ProjectSidebar } from "@/components/ProjectSidebar";
import { RoutePermissionGuardShell } from "@/components/RoutePermissionGuardShell";
import { GuideContextProvider } from "@/modules/workspace-setup-guide/GuideContextProvider";
import { WorkspaceSetupGuide } from "@/modules/workspace-setup-guide/WorkspaceSetupGuide";

// Mounts the workspace `DashboardBodyShell` (header + sidebar slot + body slot)
// and portals the sidebar, routed body (`<Outlet/>`, gated by
// `RoutePermissionGuardShell` on non-project routes), and quickstart into the
// shell's reported targets. The shell owns the responsive sidebar / mobile
// drawer.
export function BodyLayout() {
  const matches = useMatches();
  const location = useLocation();
  const currentRouteName = (
    matches.at(-1)?.handle as { name?: string } | undefined
  )?.name;
  const isRootPath = currentRouteName === WORKSPACE_ROOT_MODULE;
  // Workspace "My Issues" is a standalone full-width page: header (with logo)
  // but no sidebar (the shell's `issues` variant).
  const isMyIssues = currentRouteName === WORKSPACE_ROUTE_MY_ISSUES;
  // Project-scoped routes (`workspace.project.*`) get the project sidebar; the
  // bare projects list (`workspace.project`) keeps the workspace sidebar.
  const isProjectRoute = Boolean(
    currentRouteName?.startsWith(`${PROJECT_V1_ROUTE_DASHBOARD}.`)
  );
  const Sidebar = isProjectRoute ? ProjectSidebar : DashboardSidebar;
  const variant = isMyIssues ? "issues" : "workspace";
  const routeKey = `${location.pathname}${location.search}`;

  const [targets, setTargets] = useState<DashboardShellTargets>({
    desktopSidebar: null,
    mobileSidebar: null,
    content: null,
    quickstart: null,
    mainContainer: null,
  });

  // Non-project routes render through `RoutePermissionGuardShell`: it mounts
  // into the shell's content target and reports a non-null target only once the
  // route's `requiredPermissions` are satisfied, so a restricted page never
  // mounts (it shows the permission-denied / request-role fallback instead).
  // Project routes (`workspace.project.*`) bypass this gate — their gating
  // belongs to `ProjectRouteGate`, which loads the project resource for
  // project-scoped permission checks.
  const [permissionTarget, setPermissionTarget] =
    useState<HTMLDivElement | null>(null);

  // Agent toggle shortcut (Ctrl/Cmd+Shift+A).
  useEffect(() => {
    const handler = async (e: KeyboardEvent) => {
      if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === "A") {
        e.preventDefault();
        const { useAgentStore } = await import("@/modules/agent/store/agent");
        useAgentStore.getState().toggle();
      }
    };
    window.addEventListener("keydown", handler);
    return () => window.removeEventListener("keydown", handler);
  }, []);

  const showSidebar = !isRootPath;
  const sidebarTargets = [targets.desktopSidebar, targets.mobileSidebar];

  return (
    <GuideContextProvider>
      <DashboardBodyShell
        variant={variant}
        isRootPath={isRootPath}
        routeKey={routeKey}
        onReady={setTargets}
      />

      {showSidebar
        ? sidebarTargets.map((target, i) =>
            target ? createPortal(<Sidebar />, target, `sidebar-${i}`) : null
          )
        : null}

      {isProjectRoute
        ? targets.content
          ? createPortal(<Outlet />, targets.content)
          : null
        : targets.content
          ? createPortal(
              <RoutePermissionGuardShell
                routeKey={routeKey}
                className="m-4"
                targetClassName="h-full min-h-0"
                onReady={setPermissionTarget}
              />,
              targets.content
            )
          : null}

      {!isProjectRoute && permissionTarget
        ? createPortal(<Outlet />, permissionTarget)
        : null}

      {targets.quickstart
        ? createPortal(<WorkspaceSetupGuide />, targets.quickstart)
        : null}
    </GuideContextProvider>
  );
}