import { Link, Outlet, useLocation } from "@tanstack/react-router"; import { useEffect, useRef, useState, type ReactNode } from "react"; import { useAtomValue } from "@effect/atom-react"; import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; import type { Integration } from "@executor-js/sdk/shared"; import { integrationsOptimisticAtom } from "../api/atoms"; import { Button } from "../components/button"; import { Skeleton } from "../components/skeleton"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "../components/dropdown-menu"; import { IntegrationFavicon, integrationInferredUrl, integrationPresetIconUrl, } from "../components/integration-favicon"; import { CommandPalette } from "../components/command-palette"; import { useIntegrationPlugins } from "@executor-js/sdk/client"; import { useAuth } from "./auth-context"; // --------------------------------------------------------------------------- // Shared multiplayer shell (cloud + self-host). // // Provider-neutral: identity comes from the shared `useAuth()` seam. The bits // that genuinely differ per product are injected: // - `onSignOut` how the session is ended (WorkOS logout vs Better Auth) // - `orgMenuSlot` org switcher / create-org (cloud only) // - `supportSlot` support dialog button (cloud only) // - `navItems` which sections show (e.g. cloud adds Billing) // Everything visual is identical so both products look the same. // --------------------------------------------------------------------------- export type ShellNavItem = { readonly to: string; readonly label: string }; /** Integrations lives at "/", plus the standard tool-management sections. Hosts * spread this and append their own (e.g. Organization, Billing). */ export const defaultShellNavItems: ReadonlyArray = [ { to: "/", label: "Integrations" }, { to: "/secrets", label: "Providers" }, { to: "/oauth-apps", label: "OAuth apps" }, { to: "/policies", label: "Policies" }, ]; export interface ShellProps { /** End the session. Cloud POSTs its logout path; self-host calls Better Auth. */ readonly onSignOut: () => void | Promise; /** Nav sections; defaults to {@link defaultShellNavItems}. */ readonly navItems?: ReadonlyArray; /** Where the "API keys" footer link goes; null hides it. Default "/api-keys". */ readonly apiKeysTo?: string | null; /** Injected into the account dropdown — cloud's org switcher / create-org. */ readonly orgMenuSlot?: ReactNode; /** Injected support button above the account footer (cloud). */ readonly supportSlot?: ReactNode; } // ── Brand ──────────────────────────────────────────────────────────────── function Brand(props: { onNavigate?: () => void }) { return ( executor Beta ); } // ── NavItem ────────────────────────────────────────────────────────────── function NavItem(props: { to: string; label: string; active: boolean; onNavigate?: () => void }) { return ( {props.label} ); } // ── IntegrationList ─────────────────────────────────────────────────────────── function IntegrationList(props: { pathname: string; onNavigate?: () => void }) { const integrations = useAtomValue(integrationsOptimisticAtom); const integrationPlugins = useIntegrationPlugins(); return AsyncResult.match(integrations, { onInitial: () => (
{[80, 65, 72, 58, 68].map((w, i) => (
))}
), onFailure: () => (
No integrations yet
), onSuccess: ({ value }) => value.length === 0 ? (
No integrations yet
) : (
{value.map((integration: Integration) => { const slug = String(integration.slug); const name = integration.description || slug; const detailPath = `/integrations/${slug}`; const active = props.pathname === detailPath || props.pathname.startsWith(`${detailPath}/`); return ( {name} ); })}
), }); } // ── Avatar / initials ────────────────────────────────────────────────────── function initialsFor(name: string | null, email: string) { if (name) { return name .split(" ") .map((n) => n[0]) .join("") .slice(0, 2) .toUpperCase(); } return email[0]!.toUpperCase(); } function Avatar(props: { url: string | null; name: string | null; email: string }) { if (props.url) { return ; } return (
{initialsFor(props.name, props.email)}
); } // ── UserFooter ────────────────────────────────────────────────────────── function UserFooter(props: Pick) { const auth = useAuth(); if (auth.status !== "authenticated") return null; const apiKeysTo = props.apiKeysTo === undefined ? "/api-keys" : props.apiKeysTo; return (
{props.orgMenuSlot} {apiKeysTo && ( <> API keys )} Signed in as

{auth.user.name ?? auth.user.email}

{auth.user.name && (

{auth.user.email}

)}
void props.onSignOut()} > Sign out
); } // ── SidebarContent ─────────────────────────────────────────────────────── function SidebarContent( props: ShellProps & { pathname: string; onNavigate?: () => void; showBrand?: boolean }, ) { const navItems = props.navItems ?? defaultShellNavItems; return ( <> {props.showBrand !== false && (
)} {props.supportSlot &&
{props.supportSlot}
} ); } // ── Shell ───────────────────────────────────────────────────────────────── export function Shell(props: ShellProps) { const location = useLocation(); const pathname = location.pathname; const lastPathname = useRef(pathname); const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false); if (lastPathname.current !== pathname) { lastPathname.current = pathname; if (mobileSidebarOpen) setMobileSidebarOpen(false); } useEffect(() => { if (!mobileSidebarOpen) return; const prev = document.body.style.overflow; document.body.style.overflow = "hidden"; return () => { document.body.style.overflow = prev; }; }, [mobileSidebarOpen]); return (
{/* Desktop sidebar */} {/* Mobile sidebar overlay */} {mobileSidebarOpen && (
{/* oxlint-disable-next-line react/forbid-elements */}
setMobileSidebarOpen(false)} showBrand={false} />
)} {/* Main content */}
{/* Mobile top bar */}
); }