|
| 1 | +// A bot's use of the owner's desktop beside its chat (spec 24.9): the |
| 2 | +// window it read or acted in last, live, what it did, and Stop. Stopped, |
| 3 | +// the panel says so and offers to let it go on. |
| 4 | + |
| 5 | +import { Monitor, Play, Square, X } from "lucide-react"; |
| 6 | +import type { CSSProperties } from "react"; |
| 7 | +import { useT } from "../../i18n"; |
| 8 | +import type { Bot, DesktopAction } from "../../lib/protocol.gen"; |
| 9 | +import { useApi } from "../../store/context"; |
| 10 | +import { Badge } from "../../ui/Badge"; |
| 11 | +import { Button } from "../../ui/Button"; |
| 12 | +import { Callout } from "../../ui/Callout"; |
| 13 | +import { EmptyState } from "../../ui/EmptyState"; |
| 14 | +import { SidePanel } from "../../ui/SidePanel"; |
| 15 | +import { attempt } from "../../ui/toast"; |
| 16 | +import { BotAvatar } from "../bots/BotAvatar"; |
| 17 | +import { ComputerFooter } from "../terminal/ComputerDock"; |
| 18 | +import { useDesktopView } from "./useDesktopView"; |
| 19 | + |
| 20 | +type Words = ReturnType<typeof useT>["desktop"]["panel"]; |
| 21 | + |
| 22 | +/** What the bot did, in the owner's language. */ |
| 23 | +export function actionText(action: DesktopAction | null, t: Words): string { |
| 24 | + if (!action) { |
| 25 | + return t.read; |
| 26 | + } |
| 27 | + const target = action.target.trim(); |
| 28 | + switch (action.kind) { |
| 29 | + case "click": |
| 30 | + return target ? t.click(target) : t.clickSomething; |
| 31 | + case "type": |
| 32 | + return target ? t.type(target) : t.typeSomething; |
| 33 | + case "select": |
| 34 | + return t.select(action.option ?? ""); |
| 35 | + case "scroll": |
| 36 | + return target ? t.scroll(target) : t.scrollSomething; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export function DesktopPanel({ bot, onClose }: { bot: Bot; onClose(): void }) { |
| 41 | + const t = useT().desktop.panel; |
| 42 | + const api = useApi(); |
| 43 | + const { state, frame } = useDesktopView(bot.id); |
| 44 | + const stopped = state?.stopped === true; |
| 45 | + const window = state?.window ?? null; |
| 46 | + const stop = () => attempt(t.stopFailed, () => api.call("desktop.stop", { botId: bot.id })); |
| 47 | + const resume = () => attempt(t.resumeFailed, () => api.call("desktop.resume", { botId: bot.id })); |
| 48 | + |
| 49 | + return ( |
| 50 | + <SidePanel label={t.label(bot.name)} name="computer" defaultWidth={600}> |
| 51 | + <header className="flex h-11 shrink-0 items-center justify-between border-line border-b pr-1.5 pl-4"> |
| 52 | + <h2 className="flex items-center gap-2 font-semibold text-sm"> |
| 53 | + {t.heading} |
| 54 | + {stopped ? ( |
| 55 | + <Badge tone="warn">{t.stoppedBadge}</Badge> |
| 56 | + ) : ( |
| 57 | + frame && |
| 58 | + window && ( |
| 59 | + <Badge tone="danger"> |
| 60 | + <span aria-hidden className="live-dot" /> |
| 61 | + {t.live} |
| 62 | + </Badge> |
| 63 | + ) |
| 64 | + )} |
| 65 | + </h2> |
| 66 | + <div className="flex items-center gap-0.5"> |
| 67 | + {state && !stopped && ( |
| 68 | + <Button variant="ghost" size="sm" icon={Square} onClick={() => void stop()}> |
| 69 | + {t.stop} |
| 70 | + </Button> |
| 71 | + )} |
| 72 | + <Button variant="ghost" size="sm" icon={X} label={t.close} onClick={onClose} /> |
| 73 | + </div> |
| 74 | + </header> |
| 75 | + <div className="flex min-h-0 flex-1 flex-col gap-3 overflow-y-auto p-3"> |
| 76 | + {stopped && ( |
| 77 | + <Callout tone="warn" title={t.stoppedTitle(bot.name)}> |
| 78 | + <p>{t.stoppedBody(bot.name)}</p> |
| 79 | + <div className="mt-2.5"> |
| 80 | + <Button variant="primary" size="sm" icon={Play} onClick={() => void resume()}> |
| 81 | + {t.resume} |
| 82 | + </Button> |
| 83 | + </div> |
| 84 | + </Callout> |
| 85 | + )} |
| 86 | + {state && !window && ( |
| 87 | + <EmptyState |
| 88 | + color={bot.color} |
| 89 | + icon={Monitor} |
| 90 | + title={t.emptyTitle(bot.name)} |
| 91 | + body={t.emptyBody} |
| 92 | + /> |
| 93 | + )} |
| 94 | + {window && ( |
| 95 | + <> |
| 96 | + <p className="flex min-w-0 items-baseline gap-2 text-sm"> |
| 97 | + <span className="shrink-0 font-medium">{window.app}</span> |
| 98 | + <span className="truncate text-muted" data-selectable> |
| 99 | + {window.title} |
| 100 | + </span> |
| 101 | + </p> |
| 102 | + <div |
| 103 | + className="bot-stage grid min-h-40 flex-1 place-items-center p-2.5" |
| 104 | + style={{ "--bot": bot.color } as CSSProperties} |
| 105 | + > |
| 106 | + {frame ? ( |
| 107 | + <img |
| 108 | + src={`data:image/jpeg;base64,${frame.data}`} |
| 109 | + alt={window.title} |
| 110 | + draggable={false} |
| 111 | + className="max-h-full max-w-full select-none rounded-lg object-contain shadow-lift" |
| 112 | + /> |
| 113 | + ) : ( |
| 114 | + <p className="text-sm text-white/80">{t.waiting}</p> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + <p aria-live="polite" className="flex items-center gap-2 text-ink-soft text-sm"> |
| 118 | + <BotAvatar color={bot.color} size={18} mood={stopped ? "sleeping" : "working"} /> |
| 119 | + <span className="truncate">{actionText(state?.action ?? null, t)}</span> |
| 120 | + </p> |
| 121 | + </> |
| 122 | + )} |
| 123 | + <p className="mt-auto text-muted text-xs">{t.shortcut}</p> |
| 124 | + </div> |
| 125 | + <ComputerFooter /> |
| 126 | + </SidePanel> |
| 127 | + ); |
| 128 | +} |
0 commit comments