Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

34.9 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

import { createFileRoute, Link, useNavigate } from "@tanstack/react-router";
import { ArrowDownCircleIcon, ChevronLeftIcon, MonitorIcon, PictureInPicture2Icon, PlayIcon, SquareIcon, Trash2Icon, UserPlusIcon } from "lucide-react";
import { useEffect, useRef, useState, type ReactNode } from "react";

import { hostResources, parseDropped, runCreateFix, spaceResources, useBridge, useResources, useDevices, useMachines, useSession, useShareSheet, useSpaceDetail, useSpaceFiles, useSpaces, useSpaceThread, useTeleport, type CreateReport, type Machine, type ResourcesSnapshot, type Space, type StreamRow, type ThreadView } from "@/bridge";
import { DiagnosticReportDialog } from "@/components/diagnostic-report-dialog";
import { OsIcon } from "@/components/os-icon";
import { EmptyState, Page } from "@/components/page";
import { AgentsSection } from "@/components/space-detail/agents-section";
import { DropWell } from "@/components/space-detail/drop-well";
import { FactList } from "@/components/space-detail/facts";
import { SpaceResourcesPanel } from "@/components/resources/space-resources-panel";
import { ownerLabel } from "@/lib/space-access";
import { systemName } from "@/lib/resources";
import { StreamSection } from "@/components/space-detail/stream-section";
import { StreamSurface } from "@/components/space-detail/stream-surface";
import { SpaceHeader } from "@/components/space-detail/space-header";
import { SpaceCall } from "@/components/space-thread/space-call";
import { ThisMacCall } from "@/components/space-detail/this-mac-desktop";
import { StateLabel } from "@/components/state-dot";
import { PageTip } from "@/components/tips";
import { creatingThread } from "@/components/space-thread/queued";
import { PreparePanel } from "@/components/prepare/prepare-panel";
import { usePrepare } from "@/lib/prepare/store";
import { usePrepareAction } from "@/components/prepare/use-prepare-action";
import { useAskTasks } from "@/lib/ask-first";
import { ConfirmDialog } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/toast";
import { thisComputerText } from "@/lib/host-labels";
import { useSpaceVolumeError } from "@/lib/space-volume";
import { cn } from "@/lib/utils";
import { volumeUnavailableNote } from "@/lib/volume-notes";
import { canDelete, canPower, createError, isUnsharedDesktop, createProgress, machineName, powerLabel, powerPending, spacePlace, spaceState } from "@/lib/spaces";
import { useFlag } from "@/bridge/flags";
import { useSpaceLayout } from "@/components/space-thread/layout-mode";
import { NetworkSheet } from "@/components/network/network-sheet";
import { BlockedNotice, ExitDownNotice, NetworkNotice, NetworkRow, ParentLine, RunsOnMachinesRow, SecurityRows, WithClipboardPrompt } from "@/components/network/space-security";
import { VolumeGrantDialog } from "@/components/network/volume-grant-dialog";

export const Route = createFileRoute("/spaces/$spaceId")({
  component: SpaceDetailPage,
  // Apps and files dropped on this Space's tile in the notch, by name (the host holds their paths).
  validateSearch: (s: Record<string, unknown>): { dropped?: string[] } => {
    const dropped = parseDropped(s.dropped);
    return dropped ? { dropped } : {};
  },
});

type Confirm = "stop" | "delete" | null;

const message = (e: unknown) => (e instanceof Error ? e.message : String(e));

function SpaceDetailPage() {
  const { spaceId } = Route.useParams();
  const { dropped } = Route.useSearch();
  const navigate = useNavigate();
  const {
    data: spaces,
    isLoading,
    error,
    startSpace,
    stopSpace,
    deleteSpace,
    openSpace,
    dismissCreate,
    retryCreate,
    canRetryCreate,
    createdId,
    createReport,
    sendCreateReport,
  } = useSpaces();
  // The failed create's diagnostic report, while its review sheet is open.
  const [report, setReport] = useState<CreateReport | null>(null);
  const [reportOpen, setReportOpen] = useState(false);
  const { data: machines = [] } = useMachines();
  const resources = useResources();
  const [confirm, setConfirm] = useState<Confirm>(null);
  // The window the stage shows instead of the desktop (the Stream rail's pick), for this Space only.
  const [shown, setShown] = useState<{ spaceId: string; row: string } | null>(null);
  const teleport = useTeleport();
  const sharing = useShareSheet();
  const { data: session, openExternal } = useSession();
  const { core } = useBridge();
  const space = spaces?.find((s) => s.id === spaceId);
  // Apps and files dropped on this Space's notch tile: the same as a drop on the well (an app opens Teleport at it), once.
  const files = useSpaceFiles(space ?? { id: spaceId, name: spaceId });
  const droppedOnce = useRef<string[] | null>(null);
  useEffect(() => {
    if (!dropped || !space || droppedOnce.current === dropped) return;
    droppedOnce.current = dropped;
    void files.drop(dropped);
    // Off the address, so a reload does not teleport again.
    void navigate({ to: "/spaces/$spaceId", params: { spaceId }, search: {}, replace: true });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [dropped, space?.id]);
  const thisMachine = machines.find((m) => m.current);
  const hostArch = thisMachine?.arch ?? null;
  // As this device sees it: a machine on the relay greyed out while this
  // device is not enrolled.
  const access = thisMachine?.accessNotice ?? null;
  const devices = useDevices();
  const { detail, stream, windowsUnsupported, pip, query, setQuery, agents, copy } = useSpaceDetail(space, hostArch, { access });
  // Call view or thread view, remembered per Space.
  const [layout, setLayout] = useSpaceLayout(space?.id ?? "");
  const volumeError = useSpaceVolumeError(space, space ? spaceState(space) === "running" : false);
  // The Space's thread (CUA-2155). A daemon without it answers unsupported,
  // and the page keeps its classic layout; a create still in progress has none yet.
  const thread = useSpaceThread(space && !space.id.startsWith("pending:") ? space.id : null);
  const askFirstOn = useFlag("spaces_ask_first_home");
  // Ask first's tasks: a Space being made for one draws as the Space page in its creating state.
  const tasks = useAskTasks();
  // The first Space being prepared while the person was in onboarding: its page shows what is happening.
  const prep = usePrepare();
  const onPrepareAction = usePrepareAction();
  // Open on a creating row: once the create finished and the registry lists
  // the Space, its row (and this id) goes; follow it to the Space's own id.
  // Only a page that showed the row follows: the address of a row that is
  // already gone (Back after Remove from list) says so, and never jumps into
  // another Space.
  const [showed, setShowed] = useState<string | null>(null);
  // Who shared it, when it was shared with you: if the row goes while this page is open,
  // the owner most likely stopped sharing it, and the page says so.
  const [sharedBy, setSharedBy] = useState<{ spaceId: string; owner: string | null } | null>(null);
  useEffect(() => {
    if (!space) return;
    setShowed(spaceId);
    const a = space.access;
    setSharedBy(a && a.kind !== "mine" ? { spaceId, owner: ownerLabel(a) } : null);
  }, [space, spaceId]);
  const created = !space && showed === spaceId ? createdId(spaceId) : undefined;
  useEffect(() => {
    if (created) void navigate({ to: "/spaces/$spaceId", params: { spaceId: created }, replace: true });
  }, [created, navigate]);
  if (created) return null;

  const removedAccess = !space && !error && sharedBy?.spaceId === spaceId ? sharedBy : null;
  if (error || (!isLoading && !space)) {
    return (
      <Page>
        <BackLink />
        <EmptyState
          icon={<MonitorIcon />}
          title={error ? "Couldn't load this Space" : removedAccess ? "You no longer have access" : "This Space isn't here"}
          action={
            <Button variant="outline" render={<Link to="/spaces" />}>
              Back to Spaces
            </Button>
          }
        >
          {error
            ? error.message
            : removedAccess
              ? `${removedAccess.owner ?? "The owner"} stopped sharing this Space with you.`
              : "It may have been deleted, or it runs on a machine this app can't see."}
        </EmptyState>
      </Page>
    );
  }
  if (!space || !detail) return null;

  const state = spaceState(space);
  const machine = machineName(machines, space);
  const pending = powerPending(space);
  // "Turn on" / "Turn off" (or Resume / Suspend), the SwiftUI app's words.
  const turnOn = powerLabel(core, space, true);
  const turnOff = powerLabel(core, space, false);

  const run = (label: string, action: () => Promise<void>) =>
    action().catch((e: unknown) => toast(`Couldn't ${label} ${space.name}`, { description: message(e), type: "error" }));

  const start = () => run("start", () => startSpace(space.id));
  // The core's detail actions: present only where they apply (Share follows
  // Settings, Experiments), enabled only when the Space can stream and this
  // device may connect.
  const action = (id: string) => detail.actions.find((a) => a.id === id);
  const teleportAction = action("teleport");
  // A teammate who may only watch is not offered Share or control (the host refuses both anyway).
  const viewOnly = space.access?.role === "viewer";
  const sharedOwner = space.access && space.access.kind === "withMe" ? { name: ownerLabel(space.access) ?? "", email: space.access.ownerEmail } : null;
  const mayShare = space.access?.canShare !== false && !viewOnly;
  const shareAction = mayShare ? action("share") : undefined;
  // Signed in, not enrolled: the access line's action opens Settings,
  // Devices with the enroll sheet.
  const enroll = () => {
    devices.startEnroll();
    void navigate({ to: "/machines" });
  };
  const teleportApp = () => run("open Teleport for", () => teleport.open(space));
  const share = () => run("share", () => sharing.open(space, session?.signedIn ?? false));
  const stop = () => run("stop", () => stopSpace(space.id));
  // Leaving a Space that is going away: the grid takes this page's place in
  // the history, so Back never returns to a page for a Space that is gone.
  const leave = () => navigate({ to: "/spaces", replace: true });
  // Delete, or "Remove from List" (a Space in your cloud: forget it here,
  // keep it running there; `AppModel.delete(_:removeOnly:)`).
  const remove = (removeOnly = false) => {
    void leave();
    void run(removeOnly ? "remove" : "delete", async () => {
      await deleteSpace(space.id, removeOnly);
      toast(removeOnly ? `Removed ${space.name} from the list` : `Deleted ${space.name}`, { type: "success" });
    });
  };
  // A failed create only leaves the list (here and on the host). The grid
  // opens first, then the row goes: this page never shows "isn't here" in
  // between, and nothing else decides where the person lands.
  const dismiss = () => {
    void leave().then(() => run("remove", () => dismissCreate(space.id)));
  };
  // Try again: the same create as a new row; this page follows it.
  const retry = canRetryCreate(space.id)
    ? () => {
        const next = retryCreate(space.id);
        if (next) void navigate({ to: "/spaces/$spaceId", params: { spaceId: next }, replace: true });
      }
    : undefined;
  // Send report: the review sheet with the core's redacted report; nothing
  // is sent until the person presses Send there.
  const openReport = () =>
    void createReport(space.id).then((r) => {
      if (!r) return;
      setReport(r);
      setReportOpen(true);
    });
  // It failed the same way twice: the core's fix for that cause (copy a
  // command, open the help page, or the details) instead of Try again.
  const createFix = space.progress?.fix;
  const fix = createFix
    ? () =>
        void runCreateFix(createFix, {
          copy: async (text) => {
            await navigator.clipboard?.writeText(text);
            toast(`Copied ${text}`, { description: "Paste it in Terminal.", type: "success" });
          },
          openExternal: (url) => openExternal(url),
          details: openReport,
        }).catch((e: unknown) => toast("Couldn't do that", { description: message(e), type: "error" }))
    : undefined;
  // Picture in picture where the host has panels; its own window where it doesn't.
  const onPip = (row: StreamRow) =>
    run("open", async () => {
      if ((await pip(row.id)) === "open-window") {
        await openSpace(space.id);
        toast(`Opening ${space.name}`, { description: "Picture in picture isn't available here, so its desktop opens in a separate window." });
      }
    });

  const desktopRow = stream?.rows.find((r) => r.kind === "desktop");
  // The Stream rail's pick: one of the Space's windows on the stage, or the desktop (none). A window that
  // closes puts the desktop back.
  const shownRow = shown && shown.spaceId === space?.id ? stream?.rows.find((r) => r.kind === "window" && r.id === shown.row) : undefined;
  // What a post says the person is looking at: the shown window, in the core's fields (`stream.viewContext`).
  const shownView = shownRow ? threadViewOf(core.tryCall<StreamViewContext | null>("stream.viewContext", { row: shownRow }) ?? null) : null;
  const display = desktopRow?.resolution?.split("×").map(Number);
  // The desktop plays in its picture-in-picture panel: said here, with Bring
  // it back, instead of a second stream (`SpaceScreenView`'s placeholder).
  const poppedOut = desktopRow?.actions.some((a) => a.id === "pip" && a.active) ?? false;
  const poppedOutState = poppedOut ? (
    <div data-popped-out="" className="flex flex-col items-center gap-2">
      <PictureInPicture2Icon className="size-7 text-muted-foreground" />
      <p className="text-xs text-muted-foreground">Playing in a floating window</p>
      <Button variant="outline" size="sm" onClick={() => void onPip(desktopRow!)}>
        Bring it back
      </Button>
    </div>
  ) : undefined;
  const aspect = display?.length === 2 && display[0]! > 0 && display[1]! > 0 ? display[0]! / display[1]! : undefined;
  const showStream = detail.sections.includes("Stream") && stream;
  // The core's sections after Stream: Agents (where the host lists runs)
  // and Teleport (the drop well), as the SwiftUI detail draws them.
  const showAgents = detail.sections.includes("Agents") && !agents.unsupported;
  const showTeleport = detail.sections.includes("Teleport");

  const actions = (
    <div className="flex shrink-0 items-center gap-2">
      {teleportAction ? (
        <Button variant="outline" disabled={!teleportAction.enabled} title={teleportAction.help} onClick={teleportApp}>
          <ArrowDownCircleIcon /> {teleportAction.label}
        </Button>
      ) : null}
      {shareAction ? (
        <Button variant="outline" data-share-space="" disabled={!shareAction.enabled} title={shareAction.help} onClick={share}>
          <UserPlusIcon /> {shareAction.label}
        </Button>
      ) : null}
      {canPower(space) ? (
        state === "stopped" ? (
          <Button variant="outline" disabled={Boolean(pending)} onClick={start}>
            <PlayIcon /> {turnOn}
          </Button>
        ) : (
          <Button variant="outline" disabled={Boolean(pending)} onClick={() => setConfirm("stop")}>
            <SquareIcon className="size-3.5" /> {turnOff}
          </Button>
        )
      ) : null}
      {state === "failed" ? (
        <Button variant="outline" data-dismiss-create="" onClick={dismiss}>
          <Trash2Icon className="text-destructive" /> Remove
        </Button>
      ) : null}
      {canDelete(space) ? (
        <Button variant="outline" aria-label={`${detail.deleteLabel} ${space.name}`} onClick={() => setConfirm("delete")}>
          <Trash2Icon className="text-destructive" /> {detail.deleteLabel}
        </Button>
      ) : null}
    </div>
  );
  const place = (
    <p className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-[13px] text-muted-foreground">
      <span>{spacePlace(space, machine)}</span>
      {pending ? (
        <span className="text-xs">{pending}</span>
      ) : space.id === "this-mac" ? (
        <StateLabel state={isUnsharedDesktop(space) ? "stopped" : "running"} word={isUnsharedDesktop(space) ? "Not sharing" : "Sharing"} />
      ) : (
        <StateLabel state={state} space={space} />
      )}
      <ParentLine spaceId={space.id} />
    </p>
  );
  const osMark = (
    <div className="mt-0.5 flex size-9 shrink-0 items-center justify-center rounded-lg border bg-card shadow-xs">
      <OsIcon os={space.os} className="size-4.5 text-muted-foreground" />
    </div>
  );
  const preparing = askFirstOn && space.id.startsWith("pending:") && prep.state?.wanted && prep.state.pendingId === space.id ? prep.view : null;
  const preparePanel = preparing ? <PreparePanel view={preparing} onRetry={onPrepareAction} /> : null;
  const surface = (className: string, windowId?: string | null, windowName?: string | null, interactive = !viewOnly) => (
    <StreamSurface
      interactive={interactive}
      viewOnly={viewOnly}
      spaceId={space.id}
      windowId={windowId}
      windowName={windowName}
      os={space.os}
      aspectRatio={aspect}
      canStream={detail.canStream}
      previewText={detail.previewText}
      access={detail.access}
      onAccessAction={enroll}
      className={className}
    >
      {preparePanel ?? surfaceState(space, pending, turnOn, start, dismiss, retry, openReport, fix) ?? poppedOutState}
    </StreamSurface>
  );
  // The classic page's sections under the desktop. With the thread they open from Details, More.
  const sections = (
    <>
      <ExitDownNotice spaceId={space.id} />
      <SpaceResources space={space} spaces={spaces ?? []} snapshot={resources} machines={machines} />

      <div className="mt-8 grid items-start gap-x-6 first:mt-0 md:grid-cols-2">
        <div>
          <FactList title="Details" facts={detail.facts} extra={<RunsOnMachinesRow spaceId={space.id} />} />
          <SecurityRows spaceId={space.id} className="mt-6" />
          {volumeError ? (
            <p className="mt-3 px-1 text-[13px] text-muted-foreground" data-volume-note="" title={volumeError}>
              {volumeUnavailableNote(volumeError)}
            </p>
          ) : null}
          {showTeleport ? (
            <div className="mt-7">
              <DropWell
                title="Teleport"
                space={space}
                copy={{ caption: copy?.dropCaption ?? "Drop a file or window", sendFile: copy?.sendFile ?? "Send file…", teleportApp: copy?.teleportApp ?? "Teleport an app…" }}
                onTeleport={teleportApp}
              />
            </div>
          ) : null}
        </div>
        <div>
          {showStream ? (
            <StreamSection
              title="Stream"
              section={stream}
              query={query}
              onQuery={setQuery}
              onPip={(row) => void onPip(row)}
              hideStatus={windowsUnsupported}
            />
          ) : null}
          {showAgents ? (
            <AgentsSection
              title="Agents"
              runs={agents.runs}
              failed={agents.failed}
              copy={{
                loading: copy?.agentsLoading ?? "Looking for this Space’s agents…",
                empty: copy?.agentsEmpty ?? "No agents have been started in this Space.",
                failed: copy?.agentsFailed ?? "Could not read this Space’s agents.",
              }}
            />
          ) : null}
        </div>
      </div>
    </>
  );
  const dialogs = (
    <>
      <ConfirmDialog
        open={confirm === "stop"}
        onOpenChange={(o) => !o && setConfirm(null)}
        title={`${turnOff} ${space.name}?`}
        description={
          space.power?.control === "suspend"
            ? "It's suspended in memory and picks up where it left off when you start it again."
            : "It shuts down. Anything unsaved inside it is lost."
        }
        confirmLabel={turnOff}
        destructive={space.power?.control !== "suspend"}
        onConfirm={stop}
      />
      <ConfirmDialog
        open={confirm === "delete"}
        onOpenChange={(o) => !o && setConfirm(null)}
        title={detail.confirm.title}
        description={detail.confirm.message}
        confirmLabel={detail.confirm.confirmLabel}
        cancelLabel={detail.confirm.cancelLabel}
        disabledReason={detail.confirm.confirmEnabled ? null : (detail.confirm.disabledReason ?? null)}
        destructive
        onConfirm={() => remove()}
        alternative={detail.confirm.removeLabel ? { label: detail.confirm.removeLabel, onClick: () => remove(true) } : null}
      />
      <NetworkSheet spaceName={(id) => spaces?.find((s) => s.id === id)?.name ?? id} />
      <VolumeGrantDialog spaceId={space.id} spaceName={space.name} />
      <DiagnosticReportDialog
        open={reportOpen}
        onOpenChange={setReportOpen}
        report={report}
        onSend={(subject, parts) => sendCreateReport({ subject, parts })}
      />
    </>
  );
  const desktopPip = desktopRow?.actions.find((a) => a.id === "pip");
  const powerError = detail.powerError ? <p className="mb-4 text-[13px] text-destructive">{detail.powerError}</p> : null;
  // One tip at a time (the core's `tips.view`, place `space`).
  // A Space made for an Ask-first job is busy with its agent: no tip (it would also appear once ready and move the page).
  const askSpace = askFirstOn && tasks.some((t) => t.pendingId === space.id || (t.inSpace && t.spaceId === space.id));
  const tip = askSpace ? null : <PageTip place="space" space={space} onShare={shareAction?.enabled ? share : undefined} />;
  // In the call layout the tip sits directly on the stage and the rail: no gap under it, so the rail meets it flush.
  const callTip = askSpace ? null : <PageTip place="space" space={space} onShare={shareAction?.enabled ? share : undefined} className="mb-0" />;

  // The call layout: the desktop on top, the thread below, the people on the
  // right. A machine's own page (no desktop to call into) and a daemon
  // without the thread keep the classic page.
  // A create still in progress has no thread yet: its progress shows on the classic page.
  // A Space being made for an Ask-first job: the same page in its creating state (progress on
  // the stage, the job queued in the thread); a failed create keeps the classic page's fixes.
  const creating = space.id.startsWith("pending:") && askFirstOn && (state !== "failed" || (preparing != null && preparing.phase !== "failed")) && (tasks.some((t) => t.pendingId === space.id) || preparing != null);
  const callThread = creating ? creatingThread(space.id) : thread;
  const later = "Available once the Space is ready";
  // This Mac's own desktop row: a Space's page as any other, with the share prompt where the stream would be.
  if (space.id === "this-mac") {
    return (
      <>
        <ThisMacCall
          spaceName={space.name}
          shared={!isUnsharedDesktop(space)}
          live={thread}
          identity={session?.signedIn ? (session.account.identity ?? null) : null}
          facts={detail.facts}
          more={sections}
          header={<SpaceHeader back={<BackLink inline />} os={space.os} title={detail.title} place={place} />}
        />
        {dialogs}
      </>
    );
  }
  if (!detail.desktopNote && callThread.status !== "unsupported" && (creating || !space.id.startsWith("pending:"))) {
    return (
      <>
        <SpaceCall
          spaceId={space.id}
          spaceName={space.name}
          identity={session?.signedIn ? (session.account.identity ?? null) : null}
          header={
            <SpaceHeader
              back={<BackLink inline />}
              os={space.os}
              title={detail.title}
              place={place}
              power={
                creating
                  ? { state: "pending", label: "Being made", onClick: () => {} }
                  : canPower(space)
                  ? pending
                    ? { state: "pending", label: pending, onClick: () => {} }
                    : state === "stopped"
                      ? { state: "off", label: turnOn, onClick: start }
                      : { state: "on", label: turnOff, onClick: () => setConfirm("stop") }
                  : undefined
              }
              pip={
                !creating && desktopPip
                  ? { id: desktopRow!.id, label: desktopPip.help ?? "Pop out", help: desktopPip.help ?? undefined, enabled: true, active: desktopPip.active, onClick: () => void onPip(desktopRow!) }
                  : undefined
              }
              teleport={creating ? { label: "Teleport an app", help: later, enabled: false, onClick: () => {} } : teleportAction ? { label: teleportAction.label, help: teleportAction.help ?? undefined, enabled: teleportAction.enabled, onClick: teleportApp } : undefined}
              share={creating ? { label: "Share", help: later, enabled: false, onClick: () => {} } : shareAction ? { label: shareAction.label, help: shareAction.help ?? undefined, enabled: shareAction.enabled, onClick: share } : undefined}
              remove={!creating && canDelete(space) ? { label: detail.deleteLabel, enabled: true, onClick: () => setConfirm("delete") } : undefined}
              dismiss={state === "failed" ? { label: "Remove", enabled: true, onClick: dismiss } : undefined}
            />
          }
          notice={
            <>
              {callTip}
              <NetworkNotice spaceId={space.id} />
              <BlockedNotice spaceId={space.id} />
              {detail.powerError ? <p className="px-5 pb-2 text-[13px] text-destructive">{detail.powerError}</p> : null}
            </>
          }
          stage={
            <WithClipboardPrompt spaceId={space.id} spaceName={space.name}>
              {surface("h-full", shownRow?.id ?? null, shownView?.app ?? null)}
            </WithClipboardPrompt>
          }
          layout={layout}
          onLayout={setLayout}
          smallStage={surface("h-full", shownRow?.id ?? null, shownView?.app ?? null, false)}
          aspect={aspect}
          stageLabel={shownRow?.label ?? "Desktop"}
          thread={callThread}
          creating={creating}
          facts={detail.facts}
          detailsExtra={<NetworkRow spaceId={space.id} />}
          more={sections}
          windows={creating ? [] : (stream?.rows.filter((r) => r.kind === "window") ?? [])}
          desktop={creating ? null : (desktopRow ?? null)}
          selected={shownRow?.id ?? null}
          onSelect={(row) => setShown(row.kind === "window" ? { spaceId: space.id, row: row.id } : null)}
          view={shownView}
          onStream={(row) => void onPip(row)}
          onShare={shareAction?.enabled ? share : undefined}
          owner={sharedOwner}
        />
        {dialogs}
      </>
    );
  }

  return (
    <Page>
      <BackLink />
      <header className="mb-5 flex items-start justify-between gap-4">
        <div className="flex min-w-0 items-start gap-3">
          {osMark}
          <div className="min-w-0">
            <h1 data-detail-title className="truncate text-[22px] leading-tight font-semibold tracking-[-0.01em]">{detail.title}</h1>
            {place}
          </div>
        </div>
        {actions}
      </header>

      {tip}

      {powerError}

      {detail.desktopNote ? (
        // One of your machines that keeps its desktop private: why, in place
        // of the desktop, then the Spaces it provides (no New Space button
        // beside the note).
        <MachineSpaces
          note={detail.desktopNote}
          listSpaces={Boolean(detail.newSpace)}
          spaces={hostedSpaces(spaces ?? [], space.id)}
          // This machine's own screen: its switches live on its Machines page.
          action={
            detail.isHost ? (
              <Button variant="outline" size="sm" data-host-desktop-action="" render={<Link to="/machines" />}>
                Open Machines
              </Button>
            ) : null
          }
        />
      ) : (
        surface("h-72")
      )}

      {sections}
      {dialogs}
    </Page>
  );
}

/** The Spaces one of your machines provides (the core's `hosted_rows`:
 * those whose host is the machine). */
function hostedSpaces(spaces: readonly Space[], machineSpaceId: string): Space[] {
  if (!machineSpaceId.startsWith("relay:")) return [];
  const machine = machineSpaceId.slice("relay:".length);
  return spaces.filter((s) => s.host === machine);
}

/** A machine that does not share its desktop, or this machine's own
 * screen (never streamed here): the core's line and its one action, then
 * the Spaces it provides when it provides any. */
function MachineSpaces({ note, listSpaces, spaces, action }: { note: string; listSpaces: boolean; spaces: Space[]; action?: ReactNode }) {
  return (
    <section data-machine-desktop-note="">
      <div className="flex items-center justify-between gap-4 rounded-xl border px-4 py-3">
        <p className="text-[13px] text-muted-foreground">{note}</p>
        {action}
      </div>
      {listSpaces ? (
        <div data-machine-spaces="" className="mt-6">
          <h2 className="mb-2 px-1 text-[13px] font-semibold">Spaces</h2>
          {spaces.length === 0 ? (
            <p className="px-1 text-[13px] text-muted-foreground">No Spaces yet</p>
          ) : (
            <ul className="divide-y rounded-xl border">
              {spaces.map((s) => (
                <li key={s.id}>
                  <Link
                    to="/spaces/$spaceId"
                    params={{ spaceId: s.id }}
                    className="flex h-10 items-center gap-2.5 px-4 text-[13px] outline-none hover:bg-muted/60 focus-visible:bg-muted/60"
                  >
                    <OsIcon os={s.os} className="size-3.5 shrink-0 text-muted-foreground" />
                    <span className="min-w-0 flex-1 truncate">{s.name}</span>
                    <StateLabel state={spaceState(s)} space={s} />
                  </Link>
                </li>
              ))}
            </ul>
          )}
        </div>
      ) : null}
    </section>
  );
}

function BackLink({ inline }: { inline?: boolean }) {
  return (
    <Link
      to="/spaces"
      className={cn(
        "-ml-1 inline-flex h-6 shrink-0 items-center gap-0.5 rounded-md pr-1.5 text-xs text-muted-foreground outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/60",
        inline ? "mr-1" : "mb-4",
      )}
    >
      <ChevronLeftIcon className="size-3.5" /> Spaces
    </Link>
  );
}

/** What replaces the stream while there is no desktop to show: a create's
 * progress, a delete, a stopped Space. Undefined: the stream surface. */
function surfaceState(
  space: Space,
  pending: string | null,
  startLabel: string,
  onStart: () => void,
  onDismiss: () => void,
  onRetry?: () => void,
  onReport?: () => void,
  onFix?: () => void,
): ReactNode {
  const state = spaceState(space);
  if (state === "failed") {
    // The same failure twice: its fix replaces a plain Try again.
    const fix = space.progress?.fix;
    return (
      <>
        <p className="text-[13px] font-medium">Couldn't create this Space</p>
        <p data-create-error="" className="mt-2 max-w-md text-center text-xs text-destructive">
          {createError(space) || "No reason was reported."}
        </p>
        {fix ? (
          <p data-create-fix="" className="mt-2 max-w-md text-center text-xs text-muted-foreground">
            {fix.text}
          </p>
        ) : null}
        <div className="mt-4 flex flex-wrap justify-center gap-2">
          {fix && onFix ? (
            <Button data-create-fix-button="" onClick={onFix}>
              {fix.label}
            </Button>
          ) : onRetry ? (
            <Button onClick={onRetry}>Try again</Button>
          ) : null}
          <Button variant="outline" onClick={onDismiss}>
            Remove from list
          </Button>
          {onReport && fix?.action !== "details" ? (
            <Button variant="outline" data-send-report="" onClick={onReport}>
              Send a report…
            </Button>
          ) : null}
        </div>
      </>
    );
  }
  if (state === "creating") {
    return (
      <>
        <p className="text-[13px] font-medium">{space.progress?.label ?? "Creating…"}</p>
        <div className="mt-3 h-1 w-56 overflow-hidden rounded-full bg-foreground/10">
          <div className="h-full rounded-full bg-brand transition-[width]" style={{ width: `${Math.round(createProgress(space) * 100)}%` }} />
        </div>
        {space.progress?.transfer ? <p className="mt-2 text-xs text-muted-foreground tabular-nums">{space.progress.transfer}</p> : null}
        {space.progress?.error ? <p className="mt-2 text-xs text-destructive">{space.progress.error}</p> : null}
      </>
    );
  }
  if (state === "deleting") return <p className="text-[13px] text-muted-foreground">Deleting…</p>;
  if (state === "stopped") {
    return (
      <>
        <OsIcon os={space.os} className="mb-4 size-8 text-muted-foreground/30" />
        <p className="text-[13px] text-muted-foreground">
          {canPower(space) ? "Start this Space to open its desktop." : "This Space is stopped, and it can't be started from this app."}
        </p>
        {canPower(space) ? (
          <Button className="mt-4" variant="outline" disabled={Boolean(pending)} onClick={onStart}>
            <PlayIcon /> {pending ?? startLabel}
          </Button>
        ) : null}
      </>
    );
  }
  return undefined;
}

/** Processor, memory and disk for this Space, when its computer sends them. */
function SpaceResources({ space, spaces, snapshot, machines }: { space: Space; spaces: Space[]; snapshot: ResourcesSnapshot | null; machines: Machine[] }) {
  const mine = spaceResources(snapshot, space.id);
  const state = spaceState(space);
  if (!mine || (state !== "running" && state !== "stopped")) return null;
  const local = space.provider === "local";
  // A Space on another computer comes with only its size there: while it
  // runs, that alone would read as stopped.
  if (!local && mine.powerState === "running" && !mine.usage) return null;
  const machine = local ? machines.find((m) => m.current) : machines.find((m) => m.id === space.host || m.spaceIds.includes(space.id));
  const host = hostResources(snapshot, machine);
  const computer = local ? thisComputerText() : (machine?.name ?? space.hostName ?? "its machine");
  // The Spaces on the same computer, with the numbers it sent, for how it fits.
  const sameComputer = (s: Space) => (local ? s.provider === "local" : s.host === space.host);
  const onComputer = spaces.flatMap((s) => {
    const r = sameComputer(s) ? spaceResources(snapshot, s.id) : null;
    return r ? [r] : [];
  });
  return (
    <SpaceResourcesPanel
      space={mine}
      name={space.name}
      host={host}
      computer={computer}
      spaces={onComputer}
      baseName={systemName(space.osPrettyName)}
    />
  );
}

/** `stream.viewContext`'s answer (camelCase). */
interface StreamViewContext {
  kind: string;
  app: string | null;
  title: string | null;
  windowId: string | null;
}

/** The core's view of the shown window, as `thread.post` takes it. */
function threadViewOf(v: StreamViewContext | null): ThreadView | null {
  if (!v || v.kind !== "window") return null;
  return { kind: "window", app: v.app ?? null, title: v.title ?? null, windowId: v.windowId ?? null };
}