The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { CommandOutputView } from "@t3tools/client-runtime/state/command-output"; |
| 2 | import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts"; |
| 3 | import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput"; |
| 4 | import { T, Var } from "gt-react"; |
| 5 | import { useLayoutEffect, useRef } from "react"; |
| 6 | |
| 7 | import { useTheme } from "../../hooks/useTheme"; |
| 8 | import { useServerConfigs } from "../../state/entities"; |
| 9 | import { orchestrationEnvironment } from "../../state/orchestration"; |
| 10 | import { useEnvironmentQuery } from "../../state/query"; |
| 11 | import { useInitialOutputReady, type TurnItemSource } from "./timelineRowContext"; |
| 12 | |
| 13 | const FOLLOW_SLACK_PX = 8; |
| 14 | |
| 15 | export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean { |
| 16 | return ( |
| 17 | useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Output of one expanded command row: streams while the command runs, then |
| 23 | * shows its final output. Mount it only when `useCommandOutputStreaming` holds |
| 24 | * or a `source` serves the output. |
| 25 | * It subscribes only while mounted, so collapsed rows and other threads receive nothing. |
| 26 | */ |
| 27 | export function CommandOutputPanel(props: { |
| 28 | readonly onInitialOutputReady?: (() => void) | undefined; |
| 29 | readonly environmentId: EnvironmentId; |
| 30 | readonly threadId: ThreadId; |
| 31 | readonly itemId: TurnItemId; |
| 32 | readonly source?: TurnItemSource["commandOutput"] | undefined; |
| 33 | }) { |
| 34 | const input = { threadId: props.threadId, itemId: props.itemId }; |
| 35 | const { data, error } = useEnvironmentQuery<CommandOutputView, unknown>( |
| 36 | props.source |
| 37 | ? props.source(input) |
| 38 | : orchestrationEnvironment.v2.commandOutput({ environmentId: props.environmentId, input }), |
| 39 | ); |
| 40 | useInitialOutputReady(data === null && !error, props.onInitialOutputReady); |
| 41 | const scrollRef = useRef<HTMLDivElement>(null); |
| 42 | const followRef = useRef(true); |
| 43 | const text = data?.output.text ?? ""; |
| 44 | const { resolvedTheme } = useTheme(); |
| 45 | |
| 46 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pins the output scroller's scrollTop to the bottom after each commit while the user follows |
| 47 | useLayoutEffect(() => { |
| 48 | const element = scrollRef.current; |
| 49 | if (element && followRef.current) element.scrollTop = element.scrollHeight; |
| 50 | }); |
| 51 | |
| 52 | if (text.length === 0) { |
| 53 | if (error) { |
| 54 | return ( |
| 55 | <div className="text-destructive"> |
| 56 | <T> |
| 57 | Couldn't load output: <Var>{error}</Var> |
| 58 | </T> |
| 59 | </div> |
| 60 | ); |
| 61 | } |
| 62 | if (data === null) { |
| 63 | return ( |
| 64 | <div className="text-muted-foreground italic"> |
| 65 | <T>Loading output…</T> |
| 66 | </div> |
| 67 | ); |
| 68 | } |
| 69 | return data.running ? null : ( |
| 70 | <div className="text-muted-foreground italic"> |
| 71 | <T>No output.</T> |
| 72 | </div> |
| 73 | ); |
| 74 | } |
| 75 | // Parse after all hooks to preserve the text/theme compiler cache. |
| 76 | const spans = terminalOutputSpans(text, TERMINAL_PALETTES[resolvedTheme]); |
| 77 | return ( |
| 78 | <div |
| 79 | ref={scrollRef} |
| 80 | onScroll={(event) => { |
| 81 | const element = event.currentTarget; |
| 82 | followRef.current = |
| 83 | element.scrollHeight - element.scrollTop - element.clientHeight <= FOLLOW_SLACK_PX; |
| 84 | }} |
| 85 | className="max-h-80 overflow-auto text-muted-foreground" |
| 86 | > |
| 87 | {data?.output.truncated ? ( |
| 88 | <div className="italic"> |
| 89 | <T>Earlier output not shown</T> |
| 90 | </div> |
| 91 | ) : null} |
| 92 | {spans.map((span, index) => |
| 93 | span.style === null ? ( |
| 94 | span.text |
| 95 | ) : ( |
| 96 | // Spans never reorder and are rebuilt on each update. |
| 97 | // oxlint-disable-next-line react/no-array-index-key |
| 98 | <span key={index} style={span.style}> |
| 99 | {span.text} |
| 100 | </span> |
| 101 | ), |
| 102 | )} |
| 103 | </div> |
| 104 | ); |
| 105 | } |