"use client"; import { useState, useCallback } from "react"; import { Badge } from "@kandev/ui/badge"; import { Button } from "@kandev/ui/button"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table"; import { CardContent, CardHeader, CardTitle, CardDescription } from "@kandev/ui/card"; import { Separator } from "@kandev/ui/separator"; import { IconTrash, IconTestPipe, IconLoader2, IconCheck, IconX, IconSparkles, } from "@tabler/icons-react"; import { useSprites } from "@/hooks/domains/settings/use-sprites"; import { useAppStore } from "@/components/state-provider"; import { testSpritesConnection, destroySprite, destroyAllSprites, } from "@/lib/api/domains/sprites-api"; import type { SpritesInstance, SpritesTestResult, SpritesTestStep } from "@/lib/types/http-sprites"; import { Trans, useTranslation } from "react-i18next"; import { GENERAL_SETTINGS_TARGETS } from "@/lib/settings-discovery/catalog/preferences"; import { SettingsCard } from "./settings-card"; const SPRITES_TOKEN_ENV_VAR = "SPRITES_API_TOKEN"; export function SpritesConnectionCard({ secretId }: { secretId?: string }) { const { t } = useTranslation(); const { status } = useSprites(secretId); const [testing, setTesting] = useState(false); const [testResult, setTestResult] = useState(null); const handleTest = useCallback(async () => { setTesting(true); setTestResult(null); try { const result = await testSpritesConnection(secretId); setTestResult(result); } catch { setTestResult({ success: false, steps: [], total_duration_ms: 0, sprite_name: "", error: t("settings:failedToConnectToBackend"), }); } finally { setTesting(false); } }, [secretId, t]); return ( // `sprites-connection-card` is the pseudo-coverage oracle's render anchor for // this screen (e2e/tests/i18n/pseudo-coverage.spec.ts). Renaming it makes that // spec time out rather than silently scan an unrendered route.
{t("settings:connection")} {t("settings:spritesDevProvidesEphemeralCloudSandboxes")}
{status?.token_configured ? (

{t("settings:apiTokenIsConfigured")}{" "} {status.connected ? t("settings:activeSprites", { count: status.instance_count }) : t("settings:unableToConnect")}

) : (

Configure a {SPRITES_TOKEN_ENV_VAR} environment variable in the executor profile, referencing a secret with your Sprites.dev API token.

)}
{testResult && }
); } function TokenBadge({ configured, connected }: { configured: boolean; connected: boolean }) { const { t } = useTranslation(); if (!configured) { return {t("settings:notConfigured")}; } if (connected) { return ( {t("settings:connected")} ); } return {t("settings:disconnected")}; } function TestResultDisplay({ result }: { result: SpritesTestResult }) { const { t } = useTranslation(); return (
{result.success ? ( ) : ( )} {result.success ? t("settings:connectionTestPassed") : t("settings:connectionTestFailed")} ({result.total_duration_ms}ms)
{result.steps.map((step: SpritesTestStep) => ( ))} {result.error && !result.steps.some((s) => s.error) && (

{result.error}

)}
); } function StepRow({ step }: { step: SpritesTestStep }) { return (
{step.success ? ( ) : ( )} {step.name} ({step.duration_ms}ms) {step.error && {step.error}}
); } export function SpritesInstancesCard({ secretId }: { secretId?: string }) { const { t } = useTranslation(); const { instances, loading } = useSprites(secretId); const removeSpritesInstance = useAppStore((state) => state.removeSpritesInstance); const [destroying, setDestroying] = useState(null); const [destroyingAll, setDestroyingAll] = useState(false); const handleDestroy = useCallback( async (name: string) => { setDestroying(name); try { await destroySprite(name, secretId); removeSpritesInstance(name); } finally { setDestroying(null); } }, [secretId, removeSpritesInstance], ); const handleDestroyAll = useCallback(async () => { setDestroyingAll(true); try { await destroyAllSprites(secretId); for (const inst of instances) { removeSpritesInstance(inst.name); } } finally { setDestroyingAll(false); } }, [secretId, instances, removeSpritesInstance]); return (
{t("settings:runningSprites")} {t("settings:activeKandevSpritesSpritesAreDestroyed")}
{instances.length > 0 && ( )}
); } function InstancesContent({ loading, instances, destroying, onDestroy, }: { loading: boolean; instances: SpritesInstance[]; destroying: string | null; onDestroy: (name: string) => void; }) { const { t } = useTranslation(); if (loading) { return (
{t("settings:loading")}
); } if (instances.length === 0) { return

{t("settings:noActiveSprites")}

; } return ( {t("settings:name")} {t("settings:health")} {t("settings:uptime")} {instances.map((inst) => ( {inst.name} {formatUptime(inst.uptime_seconds)} ))}
); } /** * Health status arrives from the backend as a sentinel and is switched on below, * so it is never translated — only the rendered label is, via a per-status key. * * The backend lower-cases whatever Sprites.dev reports and does not constrain it * (`NormalizeSpriteStatus`), so a status outside this map is possible; those keep * the previous capitalize-the-sentinel rendering rather than showing raw * lowercase. */ const HEALTH_STATUS_LABEL_KEYS: Record = { running: "settings:spriteHealthRunning", cold: "settings:spriteHealthCold", starting: "settings:spriteHealthStarting", stopped: "settings:spriteHealthStopped", stopping: "settings:spriteHealthStopping", failed: "settings:spriteHealthFailed", healthy: "settings:spriteHealthHealthy", unhealthy: "settings:spriteHealthUnhealthy", unknown: "settings:spriteHealthUnknown", }; function HealthBadge({ status }: { status: string }) { const { t } = useTranslation(); const labelKey = HEALTH_STATUS_LABEL_KEYS[status]; const label = labelKey ? t(labelKey) : status.charAt(0).toUpperCase() + status.slice(1); switch (status) { case "running": return ( {label} ); case "cold": return ( {label} ); case "starting": return {label}; case "stopped": case "stopping": case "failed": return {label}; default: return {label}; } } function formatUptime(seconds: number): string { if (seconds < 60) return `${seconds}s`; const mins = Math.floor(seconds / 60); if (mins < 60) return `${mins}m`; const hours = Math.floor(mins / 60); const remainMins = mins % 60; return `${hours}h ${remainMins}m`; } export function SpritesSettings() { const { t } = useTranslation(); return (
{/* Brand noun — not translatable copy (docs/i18n.md, "Do not translate"). */}

Sprites.dev

{t("settings:manageSpritesDevRemoteSandboxIntegration")}

); }