Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
33.2 KB
Raw Blame
"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
  ChevronDown,
  ChevronRight,
  FileText,
  LogOut,
  Menu,
  MessageSquare,
  X,
} from "lucide-react";
import { useDenFlow } from "../../_providers/den-flow-provider";
import { DEFAULT_AUTH_NAME } from "../../_lib/den-flow";
import {
  formatRoleLabel,
  getAnalyticsRoute,
  getAutomationsRoute,
  getBackgroundAgentsRoute,
  getApiKeysRoute,
  getBrandAppearanceRoute,
  getBillingRoute,
  getCustomLlmProvidersRoute,
  getDiagnosticsRoute,
  getDesktopPoliciesRoute,
  getManagedDashboardsRoute,
  getOrgAccessFlags,
  getIntegrationsRoute,
  getInferenceRoute,
  getLibraryRoute,
  getMcpConnectionsRoute,
  getManagedBrandIconUrl,
  getMembersRoute,
  getToolTesterRoute,
  getYourConnectionsRoute,
  getOrgDashboardRoute,
  getOrgSettingsRoute,
  getMarketplacesRoute,
  getMarketplaceOnboardingRoute,
  getPluginsRoute,
  getSsoRoute,
  getScimRoute,
  getWorkflowRunsRoute,
  getWebRoute,
} from "../../_lib/den-org";
import { useOrgListWindow } from "../../_lib/use-org-list-window";
import { useOrgDashboard } from "../_providers/org-dashboard-provider";
import { buildDenFeedbackUrl } from "../../_lib/feedback";
import { OrgSelectionScreen } from "../_features/org-selection/org-selection-screen";
import {
  buildDashboardNavSections,
  type DashboardNavChild,
} from "../_lib/dashboard-navigation";
import { DenCommandPalette } from "./command-palette/den-command-palette";
import {
  DenSearchBar,
  type DenSearchBarHandle,
} from "./command-palette/den-search-bar";
import { UserProfileDialog } from "./user-profile-dialog";

const OMNIRUSH_DOCS_URL = "https://github.com/omnirush-ai/omnirush-gui#readme";

function OrgMark({ name }: { name: string }) {
  const initials = useMemo(() => {
    const parts = name.trim().split(/\s+/).filter(Boolean);
    return (parts[0]?.slice(0, 1) ?? "O") + (parts[1]?.slice(0, 1) ?? "");
  }, [name]);

  return (
    <div className="flex h-10 w-10 items-center justify-center rounded-full bg-[#011627] text-xs font-semibold uppercase tracking-[0.08em] text-white">
      {initials}
    </div>
  );
}

function OmniRushMark({ className = "h-9 w-auto" }: { className?: string }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 834 649"
      fill="none"
      className={className}
      aria-label="OmniRush.ai"
    >
      <path
        fill="#011627"
        d="M445.095 7.09371C465.376 6.15629 479.12 14.7057 495.962 24.2006L526.535 41.3366L562.91 61.6421C572.209 66.8088 584.43 72.9805 592.216 79.7283C605.112 90.9218 613.007 107.518 613.57 124.621C613.997 137.564 613.785 151.186 613.771 164.285L613.743 233.167L613.724 302.115C613.724 328.043 615.147 351.097 609.112 376.5C602.601 403.733 589.274 428.855 570.372 449.495C549.311 472.84 531.218 480.587 504.269 495.433L435.717 533.297L369.268 570.017C349.148 581.007 338.445 590.166 314.978 591.343C295.336 592.765 280.624 583.434 264.332 574.332L231.209 555.796L197.159 536.707C188.064 531.606 176.78 525.84 169.138 519.247C155.537 507.509 147.236 489.12 146.689 471.221C146.261 457.224 146.479 442.102 146.479 427.951L146.495 345.546L146.52 273.548C146.53 254.27 145.49 230.956 149.51 212.464C154.532 189.864 165.167 168.888 180.427 151.489C188.245 142.605 197.223 134.814 207.121 128.324C220.854 119.307 239.559 109.953 254.414 101.931L324.032 63.8708L377.708 34.3028C389.942 27.4909 403.011 19.8636 415.79 14.2429C424.983 10.1982 434.435 8.96958 445.095 7.09371Z"
      />
      <path
        fill="#FFFFFF"
        d="M551.317 90.4398C557.678 89.5674 565.764 91.1466 571.495 93.8628C579.57 97.6845 585.756 104.611 588.643 113.063C593.053 125.734 591.473 156.67 591.443 171.112L591.314 249.733L591.238 310.947C591.227 325.186 591.691 340.89 590.054 354.92C588.069 370.594 583.473 385.826 576.46 399.982C555.363 442.986 527.973 455.45 488.286 477.122L422.355 513.332L365.248 544.928C353.229 551.61 337.931 561.062 325.256 565.404C303.927 570.03 288.668 560.584 286.41 537.983C285.155 525.413 285.813 512.071 285.819 499.363L285.877 428.201L285.838 335.271C285.834 319.126 284.849 293.286 287.551 278.43C291.03 259.848 299.063 242.413 310.931 227.699C318.408 218.335 327.295 210.186 337.275 203.548C346.99 197.101 362.755 189.212 373.491 183.383L431.093 151.71L500.183 113.742C508.673 109.063 517.232 104.321 525.662 99.5446C534.307 94.6455 540.968 91.4752 551.317 90.4398Z"
      />
      <path
        fill="#011627"
        d="M500.082 178.001C526.778 177.772 523.894 205.211 523.884 223.719L523.898 262.499L523.914 317.09C523.91 328.358 524.422 343.13 522.698 354.018C520.708 366.296 516.186 378.028 509.412 388.459C503.656 397.432 496.335 405.297 487.795 411.689C481.432 416.447 474.925 419.72 467.987 423.536L442.835 437.398L405.739 457.871C398 462.106 386.024 469.486 377.74 471.261L377.429 471.295C371.837 471.855 366.369 470.989 361.995 467.199C353.196 459.977 353.708 447.985 353.675 437.935C353.658 432.922 353.668 427.909 353.67 422.896L353.695 376.464L353.657 326.944C353.647 313.866 353.091 297.438 355.615 284.836C358.159 272.209 363.878 260.447 372.266 250.342C376.745 244.958 381.997 240.295 387.801 236.377C393.985 232.272 401.996 228.073 408.612 224.459L440.329 207.201L468.44 191.684C477.65 186.588 489.038 179.021 500.082 178.001Z"
      />
      <path
        fill="#FFFFFF"
        d="M500.225 291.464L500.59 291.556C501.213 292.643 501.002 340.865 500.638 345.536C500.306 350.339 499.443 355.09 498.065 359.703C494.788 370.842 488.588 380.902 480.112 388.834C472.165 396.184 462.79 400.931 453.37 406.067L431.052 418.227L377.328 447.628L376.894 447.414C376.568 445.467 376.757 441.034 376.763 438.896L376.794 421.911C376.893 401.013 376.885 380.115 376.77 359.217C382.142 355.849 390.96 351.452 396.691 348.372L427.925 331.276L469.656 308.362C479.711 302.761 490.055 296.768 500.225 291.464Z"
      />
      <path
        fill="#FFFFFF"
        d="M497.337 201.62C500.344 201.36 500.962 203.237 501.131 205.91C501.599 213.274 501.389 220.747 501.367 228.135L501.431 265.103C460.969 287.74 420.329 310.058 379.523 332.068L376.452 333.794C376.365 312.962 373.253 285.726 386.024 268.182C393.365 258.104 404.145 253.143 414.788 247.296L441.211 232.769L476.823 212.874C483.353 209.216 490.623 204.921 497.337 201.62Z"
      />
      <path
        fill="#FFFFFF"
        d="M443.216 29.48C452.02 29.0815 460.018 30.0261 467.903 34.1434C489.625 45.4892 510.693 58.4477 532.373 69.8693C514.905 78.2946 493.564 90.995 476.372 100.542L386.895 149.628C376.357 155.498 365.774 161.287 355.148 166.992C337.373 176.588 322.776 183.695 307.595 197.464C287.772 215.608 273.675 239.14 267.014 265.17C262.116 284.284 262.909 298.302 262.917 317.836L262.939 357.47L262.926 471.524L262.961 530.447C262.98 532.198 263.562 543.941 263.164 544.751L262.58 544.549L215.582 518.061C189.232 503.261 169.189 495.747 169.845 460.795C170.068 448.934 169.804 435.617 169.812 423.605L169.831 344.391L169.818 269.769C169.814 254.383 168.977 231.859 171.873 217.311C175.825 198.048 184.641 180.127 197.478 165.236C204.056 157.596 211.686 150.929 220.143 145.432C231.916 137.708 249.246 128.979 262.061 121.995L328.787 85.3185L391.28 50.97C401.594 45.3095 412 39.3027 422.528 34.3441C428.812 31.3849 436.148 30.2484 443.216 29.48Z"
      />
    </svg>
  );
}

export function SidebarBrandMark({
  metadata,
  organizationName,
}: {
  metadata: string | null | undefined;
  organizationName: string;
}) {
  const [loadedUrl, setLoadedUrl] = useState<string | null>(null);
  const [failedUrl, setFailedUrl] = useState<string | null>(null);

  if (metadata === undefined) {
    return (
      <div
        className="h-10 w-10 rounded-xl"
        aria-label="Loading organization icon"
        data-sidebar-brand-icon="loading"
      />
    );
  }

  const iconUrl = getManagedBrandIconUrl(metadata);
  if (!iconUrl || failedUrl === iconUrl) {
    return (
      <div data-sidebar-brand-icon="fallback">
        <OmniRushMark />
      </div>
    );
  }

  return (
    <div
      className="h-10 w-10 overflow-hidden rounded-xl"
      data-sidebar-brand-icon={loadedUrl === iconUrl ? "ready" : "loading"}
    >
      <img
        src={iconUrl}
        alt={`${organizationName} icon`}
        className={`h-full w-full object-contain transition-opacity ${loadedUrl === iconUrl ? "opacity-100" : "opacity-0"}`}
        onLoad={() => setLoadedUrl(iconUrl)}
        onError={() => setFailedUrl(iconUrl)}
      />
    </div>
  );
}

const DEFAULT_WORKSPACE_FAVICON_HREF = "/omnirush-mark.svg";

export function WorkspaceFavicon({
  metadata,
}: {
  metadata: string | null | undefined;
}) {
  const orgContextLoading = metadata === undefined;
  const iconUrl = getManagedBrandIconUrl(metadata ?? null);

  useEffect(() => {
    if (orgContextLoading) {
      // Keep the server-rendered favicon until the org context is known.
      return;
    }

    let favicon = document.head.querySelector<HTMLLinkElement>('link[rel="icon"]');
    if (!favicon) {
      favicon = document.createElement("link");
      favicon.rel = "icon";
      document.head.appendChild(favicon);
    }

    if (!iconUrl) {
      favicon.href = DEFAULT_WORKSPACE_FAVICON_HREF;
      favicon.removeAttribute("type");
      return;
    }

    const previousHref = favicon.getAttribute("href");
    const previousType = favicon.getAttribute("type");
    favicon.href = iconUrl;
    favicon.type = iconUrl.toLowerCase().endsWith(".jpg")
      || iconUrl.toLowerCase().endsWith(".jpeg")
      ? "image/jpeg"
      : "image/png";

    return () => {
      if (previousHref === null) {
        favicon.setAttribute("href", DEFAULT_WORKSPACE_FAVICON_HREF);
      } else {
        favicon.setAttribute("href", previousHref);
      }
      if (previousType === null) {
        favicon.removeAttribute("type");
      } else {
        favicon.setAttribute("type", previousType);
      }
    };
  }, [orgContextLoading, iconUrl]);

  return null;
}

function getDashboardPageTitle(pathname: string, orgSlug: string | null) {
  if (!orgSlug) {
    return "Home";
  }

  const dashboardRoot = getOrgDashboardRoute(orgSlug);

  if (pathname === dashboardRoot) {
    return "Home";
  }
  if (pathname.startsWith(getAnalyticsRoute(orgSlug))) {
    return "Analytics";
  }
  if (pathname.startsWith(getMembersRoute(orgSlug))) {
    return "Members";
  }
  if (pathname.startsWith(getApiKeysRoute(orgSlug))) {
    return "API Keys";
  }
  if (pathname.startsWith(getScimRoute(orgSlug))) {
    return "SCIM";
  }
  if (pathname.startsWith(getSsoRoute(orgSlug))) {
    return "SSO";
  }
  if (pathname.startsWith(getBackgroundAgentsRoute(orgSlug))) {
    return "Background Tasks";
  }
  if (pathname.startsWith(getAutomationsRoute(orgSlug))) {
    return "My Automations";
  }
  if (pathname.startsWith(getCustomLlmProvidersRoute(orgSlug))) {
    return "Bring your Own Keys";
  }
  if (
    pathname.startsWith(getDesktopPoliciesRoute(orgSlug))
    || pathname.startsWith(getMarketplacesRoute(orgSlug))
    || pathname.startsWith(getBrandAppearanceRoute(orgSlug))
  ) {
    return "Advanced";
  }
  if (pathname.startsWith(getDiagnosticsRoute(orgSlug))) {
    return "Diagnostics";
  }
  if (pathname.startsWith(getInferenceRoute(orgSlug))) {
    return "OmniRush.ai Models";
  }
  if (pathname.startsWith(getWebRoute(orgSlug))) {
    return "OmniRush.ai Web";
  }
  if (pathname.startsWith(getLibraryRoute(orgSlug))) {
    return "My Library";
  }
  if (pathname.startsWith(getPluginsRoute(orgSlug))) {
    return "Plugin Directory";
  }
  if (pathname.startsWith(getIntegrationsRoute(orgSlug))) {
    return "Plugin Directory";
  }
  if (pathname.startsWith(getMcpConnectionsRoute(orgSlug))) {
    return "Connectors";
  }
  if (pathname.startsWith(getManagedDashboardsRoute(orgSlug))) {
    return "Dashboards";
  }
  if (pathname.startsWith(getYourConnectionsRoute(orgSlug))) {
    return "Your Connections";
  }
  if (pathname.startsWith(getWorkflowRunsRoute(orgSlug))) {
    return "Workflow Runs";
  }
  if (pathname.startsWith(getToolTesterRoute(orgSlug))) {
    return "Tool Tester";
  }
  if (pathname.startsWith(getBillingRoute(orgSlug))) {
    return "Billing";
  }
  if (pathname.startsWith(getOrgSettingsRoute(orgSlug))) {
    return "Org Settings";
  }

  return "Home";
}

export function OrgDashboardShell({ children }: { children: React.ReactNode }) {
  const pathname = usePathname();
  const onboardingRoute = getMarketplaceOnboardingRoute();
  const isOnboarding = pathname === onboardingRoute || pathname.startsWith(`${onboardingRoute}/`);
  const { user, signOut, updateUserProfile, runtimeConfig, runtimeConfigLoaded, setupPending, setupOrganizationId } = useDenFlow();
  const {
    activeOrg,
    orgDirectory,
    orgContext,
    orgSelectionOpen,
    orgBusy,
    orgError,
    mutationBusy,
    switchOrganization,
  } = useOrgDashboard();
  const [switcherOpen, setSwitcherOpen] = useState(false);
  const [sidebarOpen, setSidebarOpen] = useState(false);
  const [commandPaletteOpen, setCommandPaletteOpen] = useState(false);
  const [profilePromptDismissed, setProfilePromptDismissed] = useState(false);
  const switcherTriggerRef = useRef<HTMLButtonElement>(null);
  const searchBarRef = useRef<DenSearchBarHandle>(null);
  const commandPaletteWasOpenRef = useRef(false);
  const isSingleOrgMode = runtimeConfigLoaded && runtimeConfig.orgMode === "single_org";
  const {
    query: switcherQuery,
    setQuery: setSwitcherQuery,
    visible: visibleOrgDirectory,
    filteredCount: switcherFilteredCount,
    hiddenCount: switcherHiddenCount,
    hasMore: switcherHasMore,
    showMore: showMoreOrgDirectory,
    showSearch: showSwitcherSearch,
  } = useOrgListWindow(orgDirectory, 20);

  const handleCommandPaletteOpenChange = useCallback((open: boolean) => {
    setCommandPaletteOpen(open);
  }, []);

  useEffect(() => {
    function handleKeyDown(event: KeyboardEvent) {
      if (isOnboarding) return;
      if (
        event.key.toLowerCase() !== "k"
        || (!event.metaKey && !event.ctrlKey)
        || event.shiftKey
        || event.altKey
      ) {
        return;
      }
      event.preventDefault();
      setCommandPaletteOpen((current) => !current);
    }

    document.addEventListener("keydown", handleKeyDown);
    return () => document.removeEventListener("keydown", handleKeyDown);
  }, [isOnboarding]);

  useEffect(() => {
    if (commandPaletteWasOpenRef.current && !commandPaletteOpen) {
      searchBarRef.current?.focus();
    }
    commandPaletteWasOpenRef.current = commandPaletteOpen;
  }, [commandPaletteOpen]);

  useEffect(() => {
    if (!switcherOpen) return;

    function handlePointerDown(event: PointerEvent) {
      const clickedInsideSwitcher = event.composedPath().some(
        (target) => target instanceof Element && target.hasAttribute("data-workspace-switcher-root"),
      );
      if (!clickedInsideSwitcher) {
        setSwitcherOpen(false);
      }
    }

    function handleKeyDown(event: KeyboardEvent) {
      if (event.key !== "Escape") return;
      setSwitcherOpen(false);
      switcherTriggerRef.current?.focus();
    }

    document.addEventListener("pointerdown", handlePointerDown);
    document.addEventListener("keydown", handleKeyDown);
    return () => {
      document.removeEventListener("pointerdown", handlePointerDown);
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, [switcherOpen]);

  // The picker replaces the whole shell until a workspace is chosen.
  if (orgSelectionOpen) {
    return (
      <OrgSelectionScreen
        orgs={orgDirectory}
        pending={mutationBusy === "switch-organization"}
        errorMessage={orgError}
        onPick={switchOrganization}
        onSignOut={() => void signOut()}
      />
    );
  }

  // Setup owns the full page until the user leaves for their dashboard.
  if (isOnboarding) {
    return (
      <>
        <WorkspaceFavicon metadata={orgContext?.organization.metadata} />
        <main data-testid="den-onboarding-shell">{children}</main>
      </>
    );
  }

  const access = getOrgAccessFlags(
    orgContext?.currentMember.role ?? "member",
    orgContext?.currentMember.isOwner ?? false,
    orgContext?.roles,
  );

  const pageTitle = getDashboardPageTitle(pathname, activeOrg?.slug ?? null);
  const shouldShowProfilePrompt = Boolean(
    user &&
      !profilePromptDismissed &&
      user.name?.trim() === DEFAULT_AUTH_NAME,
  );
  const showFeedbackLink = runtimeConfigLoaded && runtimeConfig.orgMode === "multi_org";
  const feedbackHref = buildDenFeedbackUrl({
    pathname,
    orgSlug: activeOrg?.slug,
  });
  const navSections = buildDashboardNavSections({
    orgSlug: activeOrg?.slug ?? null,
    access,
    capabilities: orgContext?.capabilities ?? {
      cloud: false,
      installLinks: false,
      mcpConnections: false,
      omnirushWeb: false,
      orgManagedDashboards: false,
      workflows: false,
    },
    orgMode: runtimeConfig.orgMode,
    runtimeConfigLoaded,
  });

  const orgSwitcher = isSingleOrgMode ? (
    <div className="flex w-full items-center justify-between gap-3 rounded-xl px-2 py-2">
      <div className="flex min-w-0 items-center gap-3">
        <OrgMark name={activeOrg?.name ?? runtimeConfig.singleOrgName} />
        <div className="min-w-0">
          <p className="truncate text-[14px] font-medium text-gray-900">
            {activeOrg?.name ?? runtimeConfig.singleOrgName}
          </p>
          <p className="truncate text-[12px] text-gray-500">
            {activeOrg ? formatRoleLabel(activeOrg.role) : "Preparing workspace"}
          </p>
        </div>
      </div>
      <button
        type="button"
        onClick={() => void signOut()}
        className="shrink-0 rounded-lg p-1.5 text-gray-400 transition hover:bg-gray-100 hover:text-gray-900"
        aria-label="Sign out"
      >
        <LogOut className="h-4 w-4" />
      </button>
    </div>
  ) : (
    <div data-workspace-switcher-root="" className="relative">
      <button
        ref={switcherTriggerRef}
        type="button"
        data-testid="workspace-switcher-trigger"
        className="flex w-full items-center justify-between gap-3 rounded-xl px-2 py-2 text-left transition-colors hover:bg-gray-100"
        onClick={() => setSwitcherOpen((current) => !current)}
        aria-expanded={switcherOpen}
        aria-haspopup="dialog"
      >
        <div className="flex min-w-0 items-center gap-3">
          <OrgMark name={activeOrg?.name ?? "OmniRush.ai"} />
          <div className="min-w-0">
            <p className="truncate text-[14px] font-medium text-gray-900">
              {activeOrg?.name ?? "Loading..."}
            </p>
            <p className="truncate text-[12px] text-gray-500">
              {activeOrg ? formatRoleLabel(activeOrg.role) : "Preparing workspace"}
            </p>
          </div>
        </div>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="text-gray-400 shrink-0">
          <path d="M17 3l4 4-4 4"/>
          <path d="M3 7h18"/>
          <path d="M7 21l-4-4 4-4"/>
          <path d="M21 17H3"/>
        </svg>
      </button>

      {switcherOpen ? (
        <div
          data-testid="workspace-switcher-menu"
          role="dialog"
          aria-label="Workspace switcher"
          className="absolute bottom-[calc(100%+0.5rem)] left-0 z-30 grid w-[240px] max-w-[calc(100vw-1.5rem)] grid-cols-[minmax(0,1fr)] gap-1 rounded-2xl border border-gray-200 bg-white py-2 shadow-[0_12px_24px_-12px_rgba(0,0,0,0.15)]"
        >
          <div className="min-w-0 px-3 py-1.5">
            <p className="truncate text-[13px] font-medium text-gray-900">
              {user?.email ?? "OmniRush.ai user"}
            </p>
          </div>
          
          <div className="mx-2 h-px bg-gray-100 my-1" />

          <div className="px-3 pb-1 pt-1">
            <p className="text-[11px] font-medium text-gray-500">
              Switch workspace
            </p>
          </div>

          {showSwitcherSearch ? (
            <div className="px-2 pb-1">
              <input
                type="search"
                value={switcherQuery}
                onChange={(event) => setSwitcherQuery(event.target.value)}
                placeholder="Search workspaces"
                className="w-full rounded-lg border border-gray-200 bg-white px-2.5 py-1.5 text-[12px] text-gray-900 outline-hidden transition focus:border-gray-400"
              />
            </div>
          ) : null}

          <div className="grid max-h-64 gap-0.5 overflow-y-auto px-1.5">
            {visibleOrgDirectory.map((org) => (
              <button
                key={org.id}
                type="button"
                onClick={() => {
                  setSwitcherOpen(false);
                  switchOrganization(org.slug);
                }}
                className={`flex items-center justify-between gap-3 rounded-lg px-2 py-1.5 text-left transition-colors ${
                  org.isActive
                    ? "bg-gray-50 text-gray-900"
                    : "text-gray-600 hover:bg-gray-50 hover:text-gray-900"
                }`}
              >
                <div className="flex min-w-0 items-center gap-2.5">
                  <div className="min-w-0">
                    <span className="block truncate text-[13px] font-medium tracking-[-0.1px]">{org.name}</span>
                    <span className="block truncate text-[12px] text-gray-500">
                      {org.role === "owner" ? "Creator plan" : "Free plan"} • {org.memberCount} {org.memberCount === 1 ? "member" : "members"}
                    </span>
                  </div>
                </div>
                {org.isActive ? (
                  <svg className="h-4 w-4 shrink-0 text-gray-900" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                    <polyline points="20 6 9 17 4 12" />
                  </svg>
                ) : null}
              </button>
            ))}
          </div>

          {switcherFilteredCount === 0 && switcherQuery ? (
            <p className="px-3 py-1 text-[12px] text-gray-500">No organizations match your search.</p>
          ) : null}

          {switcherHasMore ? (
            <div className="px-1.5">
              <button
                type="button"
                onClick={showMoreOrgDirectory}
                className="flex w-full items-center justify-center rounded-lg px-2 py-1.5 text-[12px] font-medium text-gray-600 transition-colors hover:bg-gray-50 hover:text-gray-900"
              >
                Show more ({switcherHiddenCount})
              </button>
            </div>
          ) : null}

          <div className="px-1.5 mt-0.5">
            <Link
              href="/organization"
              className="flex w-full items-center gap-2 rounded-lg px-2 py-1.5 text-[13px] font-medium text-gray-600 transition-colors hover:bg-gray-50 hover:text-gray-900"
              onClick={() => setSwitcherOpen(false)}
            >
              <span className="text-gray-400 text-[16px] leading-none">+</span> Create or join workspace
            </Link>
          </div>

          <div className="mx-2 h-px bg-gray-100 my-1" />

          <div className="px-1.5">
            <button
              type="button"
              onClick={() => void signOut()}
              className="flex w-full items-center gap-2 rounded-lg px-2 py-1.5 text-[13px] font-medium text-gray-600 transition-colors hover:bg-gray-50 hover:text-gray-900"
            >
              <LogOut className="h-4 w-4 text-gray-400" />
              Sign out
            </button>
          </div>
        </div>
      ) : null}
    </div>
  );

  const sidebarContent = (
    <div className="flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
      <div className="shrink-0 border-b border-gray-100 px-4 pb-4 pt-5">
        <div className="flex items-center justify-between gap-3">
          <SidebarBrandMark
            metadata={orgContext?.organization.metadata}
            organizationName={activeOrg?.name ?? runtimeConfig.singleOrgName}
          />
          <button
            type="button"
            className="rounded-lg p-1.5 text-gray-400 transition-colors hover:bg-gray-100 hover:text-gray-700 md:hidden"
            onClick={() => setSidebarOpen(false)}
            aria-label="Close menu"
          >
            <X className="h-5 w-5" />
          </button>
        </div>
      </div>

      <nav className="min-h-0 min-w-0 flex-1 overflow-x-hidden overflow-y-auto px-3 py-5" data-testid="den-org-sidebar">
        <div className="space-y-5">
          {navSections.map((section) => (
            <div key={section.label} data-sidebar-section={section.label.toLowerCase()}>
              <p className="px-3 pb-2 text-[10px] font-semibold uppercase tracking-[0.16em] text-gray-400">
                {section.label}
              </p>
              <div className="space-y-1">
                {section.items.map((item) => {
                  const isDashboardRoot =
                    activeOrg && item.href === getOrgDashboardRoute(activeOrg.slug);
                  const isLibraryItem =
                    Boolean(activeOrg && item.href === getLibraryRoute(activeOrg.slug));
                  const childActive = (child: DashboardNavChild) =>
                    pathname === child.href || pathname.startsWith(`${child.href}/`);
                  const groupActive = (item.children ?? []).some(childActive);
                  const selected =
                    item.href !== "#" &&
                    (item.children
                      ? groupActive
                      : isDashboardRoot
                        ? pathname === item.href
                        : isLibraryItem
                          ? pathname === item.href
                            || pathname.startsWith(`${item.href}/`)
                            || (activeOrg != null && (
                              pathname === getYourConnectionsRoute(activeOrg.slug)
                              || pathname.startsWith(`${getYourConnectionsRoute(activeOrg.slug)}/`)
                            ))
                          : pathname === item.href
                            || pathname.startsWith(`${item.href}/`)
                            || (item.matchHrefs ?? []).some(
                              (href) => pathname === href || pathname.startsWith(`${href}/`),
                            ));

                  return (
                    <div key={item.label}>
                      <Link
                        href={item.href}
                        data-testid={item.testId}
                        onClick={() => setSidebarOpen(false)}
                        className={`flex min-w-0 items-center justify-between gap-3 rounded-xl px-3 py-2.5 text-[13px] tracking-[-0.1px] transition-colors ${
                          selected
                            ? "bg-gray-100 text-gray-900"
                            : "text-gray-500 hover:bg-gray-50 hover:text-gray-700"
                        }`}
                      >
                        <span className="flex min-w-0 items-center gap-3">
                          <item.icon className="h-4 w-4 shrink-0" strokeWidth={1.8} />
                          <span className="min-w-0 truncate">{item.label}</span>
                        </span>
                        <span className="flex shrink-0 items-center gap-1.5">
                          {item.badge ? (
                            <span className="rounded-full bg-white px-2 py-1 text-[9px] font-semibold uppercase tracking-[0.14em] text-gray-500">
                              {item.badge}
                            </span>
                          ) : null}
                          {item.children ? (
                            groupActive
                              ? <ChevronDown className="h-3.5 w-3.5 text-gray-400" strokeWidth={2} />
                              : <ChevronRight className="h-3.5 w-3.5 text-gray-300" strokeWidth={2} />
                          ) : null}
                        </span>
                      </Link>
                      {item.children && groupActive ? (
                        <div className="ml-[22px] mt-1 min-w-0 space-y-0.5 border-l border-gray-100 pl-3">
                          {item.children.map((child) => (
                            <Link
                              key={child.label}
                              href={child.href}
                              onClick={() => setSidebarOpen(false)}
                              className={`flex min-w-0 items-center justify-between gap-3 rounded-lg px-2.5 py-1.5 text-[13px] tracking-[-0.1px] transition-colors ${
                                childActive(child)
                                  ? "font-medium text-gray-900"
                                  : "text-gray-500 hover:text-gray-700"
                              }`}
                            >
                              <span className="min-w-0 truncate">{child.label}</span>
                              {child.badge ? (
                                <span className="shrink-0 rounded-full bg-white px-2 py-1 text-[9px] font-semibold uppercase tracking-[0.14em] text-gray-500">
                                  {child.badge}
                                </span>
                              ) : null}
                            </Link>
                          ))}
                        </div>
                      ) : null}
                    </div>
                  );
                })}
              </div>
            </div>
          ))}
        </div>
      </nav>

      <div className="mt-auto shrink-0 p-3" data-testid="den-org-sidebar-footer">
        {orgSwitcher}

        {orgBusy ? (
          <p className="mt-3 px-2 text-[11px] text-gray-400">Refreshing workspace…</p>
        ) : null}
        {orgError ? (
          <p className="mt-3 px-2 text-[11px] font-medium text-rose-600">{orgError}</p>
        ) : null}
      </div>
    </div>
  );

  return (
    <div className="flex min-h-screen flex-col bg-[#fafafa] md:h-screen md:flex-row">
      <WorkspaceFavicon metadata={orgContext?.organization.metadata} />
      {/* Desktop sidebar — always visible at md+ */}
      <aside className="hidden min-h-0 shrink-0 overflow-hidden border-r border-gray-100 bg-white md:flex md:h-screen md:w-[260px] md:flex-col">
        {sidebarContent}
      </aside>

      {/* Mobile sidebar — off-canvas drawer */}
      {sidebarOpen ? (
        <div className="fixed inset-0 z-50 md:hidden">
          <div className="absolute inset-0 bg-black/30" onClick={() => setSidebarOpen(false)} aria-hidden />
          <aside className="relative z-10 flex h-full min-h-0 w-[280px] max-w-[85vw] flex-col overflow-hidden bg-white shadow-xl">
            {sidebarContent}
          </aside>
        </div>
      ) : null}

      <div className="flex min-w-0 flex-1 flex-col">
        <header className="flex h-14 shrink-0 items-center justify-between border-b border-gray-100 bg-white px-4 md:px-6">
          <div className="flex min-w-0 items-center gap-3">
            <button
              type="button"
              className="rounded-lg p-1.5 text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 md:hidden"
              onClick={() => setSidebarOpen(true)}
              aria-label="Open menu"
            >
              <Menu className="h-5 w-5" />
            </button>
            <span className="text-[14px] tracking-[-0.1px] text-gray-900">
              {pageTitle}
            </span>
          </div>

          <div className="flex flex-1 justify-center px-4">
            <DenSearchBar
              ref={searchBarRef}
              onOpen={() => handleCommandPaletteOpenChange(true)}
            />
          </div>

          <div className="flex shrink-0 items-center gap-1">
            {showFeedbackLink ? (
              <a
                href={feedbackHref}
                target="_blank"
                rel="noreferrer"
                className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-[13px] text-gray-500 transition-colors hover:bg-gray-50 hover:text-gray-700"
              >
                <MessageSquare className="h-4 w-4" />
                <span className="hidden sm:inline">Feedback</span>
              </a>
            ) : null}
            <a
              href={OMNIRUSH_DOCS_URL}
              target="_blank"
              rel="noreferrer"
              className="flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-[13px] text-gray-500 transition-colors hover:bg-gray-50 hover:text-gray-700"
            >
              <FileText className="h-4 w-4" />
              <span className="hidden sm:inline">Docs</span>
            </a>
          </div>
        </header>

        <main className="flex-1 overflow-y-auto bg-[#fafafa]">
          {setupPending && setupOrganizationId === activeOrg?.id ? (
            <div className="border-b border-gray-100 px-4 py-3 text-sm md:px-6">
              <Link href={onboardingRoute} className="font-medium text-gray-900 underline underline-offset-4">Back to setup</Link>
            </div>
          ) : null}
          {children}
        </main>
      </div>

      <DenCommandPalette
        open={commandPaletteOpen}
        onOpenChange={handleCommandPaletteOpenChange}
      />

      {shouldShowProfilePrompt && user ? (
        <UserProfileDialog
          key={user.id}
          user={user}
          descriptor="Change how your name appears in the organization"
          onCancel={() => setProfilePromptDismissed(true)}
          onSave={async (input) => {
            await updateUserProfile(input);
            setProfilePromptDismissed(true);
          }}
        />
      ) : null}
    </div>
  );
}