|
| 1 | +import { |
| 2 | + isAtomCommandInterrupted, |
| 3 | + squashAtomCommandFailure, |
| 4 | +} from "@t3tools/client-runtime/state/runtime"; |
| 5 | +import type { SandboxStatusResult, ScopedThreadRef } from "@t3tools/contracts"; |
| 6 | +import { AlertTriangleIcon, LoaderCircleIcon, PlayIcon } from "lucide-react"; |
| 7 | +import { useCallback, useState } from "react"; |
| 8 | + |
| 9 | +import { cn } from "~/lib/utils"; |
| 10 | +import type { EnvironmentQueryView } from "~/state/query"; |
| 11 | +import { serversEnvironment } from "~/state/servers"; |
| 12 | +import { useAtomCommand } from "~/state/use-atom-command"; |
| 13 | + |
| 14 | +import { Button } from "./ui/button"; |
| 15 | + |
| 16 | +type SandboxStatus = SandboxStatusResult["sandboxStatus"]; |
| 17 | + |
| 18 | +const STATUS_COPY: Record<SandboxStatus, { readonly tone: "muted" | "warning" | "error" }> = { |
| 19 | + not_created: { |
| 20 | + tone: "warning", |
| 21 | + }, |
| 22 | + initializing: { |
| 23 | + tone: "muted", |
| 24 | + }, |
| 25 | + ready: { |
| 26 | + tone: "muted", |
| 27 | + }, |
| 28 | + stopped: { |
| 29 | + tone: "warning", |
| 30 | + }, |
| 31 | + removing: { |
| 32 | + tone: "muted", |
| 33 | + }, |
| 34 | + removed: { |
| 35 | + tone: "warning", |
| 36 | + }, |
| 37 | + error: { |
| 38 | + tone: "error", |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +const STARTABLE_STATUSES = new Set<SandboxStatus>(["not_created", "stopped", "removed", "error"]); |
| 43 | + |
| 44 | +interface SandboxStatusControlProps { |
| 45 | + readonly threadRef: ScopedThreadRef; |
| 46 | + readonly status: EnvironmentQueryView<SandboxStatusResult>; |
| 47 | + readonly className?: string | undefined; |
| 48 | +} |
| 49 | + |
| 50 | +function failureMessage(error: unknown): string { |
| 51 | + return error instanceof Error && error.message.trim().length > 0 |
| 52 | + ? error.message |
| 53 | + : "Could not start the sandbox."; |
| 54 | +} |
| 55 | + |
| 56 | +export function SandboxStatusControl({ threadRef, status, className }: SandboxStatusControlProps) { |
| 57 | + const startSandbox = useAtomCommand(serversEnvironment.startSandbox, { reportFailure: false }); |
| 58 | + const [isStarting, setIsStarting] = useState(false); |
| 59 | + const [startError, setStartError] = useState<string | null>(null); |
| 60 | + const { refresh } = status; |
| 61 | + const sandboxStatus = status.data?.sandboxStatus ?? null; |
| 62 | + |
| 63 | + const startable = |
| 64 | + !status.isPending && sandboxStatus !== null && STARTABLE_STATUSES.has(sandboxStatus); |
| 65 | + |
| 66 | + const handleStart = useCallback(async () => { |
| 67 | + setStartError(null); |
| 68 | + setIsStarting(true); |
| 69 | + const result = await startSandbox({ |
| 70 | + environmentId: threadRef.environmentId, |
| 71 | + input: { threadId: threadRef.threadId }, |
| 72 | + }); |
| 73 | + setIsStarting(false); |
| 74 | + refresh(); |
| 75 | + if (result._tag === "Success" || isAtomCommandInterrupted(result)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + setStartError(failureMessage(squashAtomCommandFailure(result))); |
| 79 | + }, [refresh, startSandbox, threadRef.environmentId, threadRef.threadId]); |
| 80 | + |
| 81 | + const statusMeta = !status.isPending && sandboxStatus ? STATUS_COPY[sandboxStatus] : null; |
| 82 | + const tone = |
| 83 | + startError || status.error |
| 84 | + ? "error" |
| 85 | + : status.isPending |
| 86 | + ? "muted" |
| 87 | + : (statusMeta?.tone ?? "warning"); |
| 88 | + const label = |
| 89 | + status.isPending && !startError && !status.error |
| 90 | + ? "Checking sandbox status" |
| 91 | + : sandboxStatus === "ready" |
| 92 | + ? "Sandbox running" |
| 93 | + : "Sandbox unavailable"; |
| 94 | + const showRetry = Boolean(status.error) && !startable; |
| 95 | + const actionLabel = sandboxStatus === "error" || startError ? "Retry" : "Start"; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div |
| 99 | + aria-live="polite" |
| 100 | + className={cn( |
| 101 | + "flex min-w-0 shrink-0 items-center gap-2 rounded-md border border-border/70 bg-background px-2 py-1 shadow-xs", |
| 102 | + tone === "error" && "border-destructive/35 bg-destructive/5", |
| 103 | + tone === "warning" && "border-warning/35 bg-warning/5", |
| 104 | + className, |
| 105 | + )} |
| 106 | + > |
| 107 | + <div className="flex min-w-0 items-center gap-2"> |
| 108 | + {status.isPending || sandboxStatus === "initializing" || sandboxStatus === "removing" ? ( |
| 109 | + <LoaderCircleIcon className="size-3.5 shrink-0 animate-spin text-muted-foreground" /> |
| 110 | + ) : tone === "error" ? ( |
| 111 | + <AlertTriangleIcon className="size-3.5 shrink-0 text-destructive" /> |
| 112 | + ) : ( |
| 113 | + <span className="size-2 shrink-0 rounded-full bg-warning" /> |
| 114 | + )} |
| 115 | + <div className="min-w-0 truncate text-xs font-medium text-foreground">{label}</div> |
| 116 | + </div> |
| 117 | + {showRetry ? ( |
| 118 | + <Button size="xs" variant="outline" onClick={status.refresh}> |
| 119 | + Retry |
| 120 | + </Button> |
| 121 | + ) : startable ? ( |
| 122 | + <Button size="xs" variant="outline" disabled={isStarting} onClick={handleStart}> |
| 123 | + {isStarting ? ( |
| 124 | + <LoaderCircleIcon className="size-3.5 animate-spin" /> |
| 125 | + ) : ( |
| 126 | + <PlayIcon className="size-3.5" /> |
| 127 | + )} |
| 128 | + <span>{actionLabel}</span> |
| 129 | + </Button> |
| 130 | + ) : null} |
| 131 | + </div> |
| 132 | + ); |
| 133 | +} |
0 commit comments