Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CommandOutputPanel.tsx

83 lines3.3 KB

1import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
3import { useLayoutEffect, useRef } from "react";
4​
5import { useTheme } from "../../hooks/useTheme";
6import { useServerConfigs } from "../../state/entities";
7import { orchestrationEnvironment } from "../../state/orchestration";
8import { useEnvironmentQuery } from "../../state/query";
9​
10// Within this many pixels of the end still counts as following the output.
11const FOLLOW_SLACK_PX = 8;
12​
13/** True when the environment streams command output; older servers only return final output. */
14export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean {
15 return (
16 useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true
17 );
19​
20/**
21 * Output of one expanded command row: streams while the command runs, then
22 * shows its final output. Mount it only when `useCommandOutputStreaming` holds.
23 * It subscribes only while mounted, so collapsed rows and other threads receive nothing.
24 */
25export function CommandOutputPanel(props: {
26 readonly environmentId: EnvironmentId;
27 readonly threadId: ThreadId;
28 readonly itemId: TurnItemId;
29}) {
30 const { data, error } = useEnvironmentQuery(
31 orchestrationEnvironment.v2.commandOutput({
32 environmentId: props.environmentId,
33 input: { threadId: props.threadId, itemId: props.itemId },
34 }),
35 );
36 const scrollRef = useRef<HTMLDivElement>(null);
37 // Follow new output until the reader scrolls up; scrolling back down resumes.
38 const followRef = useRef(true);
39 const text = data?.output.text ?? "";
40 const { resolvedTheme } = useTheme();
41​
42 // This panel only re-renders when its output changes, so keep the end in view after each.
43 useLayoutEffect(() => {
44 const element = scrollRef.current;
45 if (element && followRef.current) element.scrollTop = element.scrollHeight;
46 });
47​
48 if (text.length === 0) {
49 if (error) return <div className="text-destructive">Couldn&apos;t load output: {error}</div>;
50 if (data === null) return <div className="text-muted-foreground italic">Loading output…</div>;
51 return data.running ? null : <div className="text-muted-foreground italic">No output.</div>;
52 }
53 // Parsed after the layout effect, the last hook, so the compiler caches it on text and theme.
54 const spans = terminalOutputSpans(text, TERMINAL_PALETTES[resolvedTheme]);
55 return (
56 <div>
57 {data?.output.truncated ? (
58 <div className="text-muted-foreground italic">Earlier output not shown</div>
59 ) : null}
60 <div
61 ref={scrollRef}
62 onScroll={(event) => {
63 const element = event.currentTarget;
64 followRef.current =
65 element.scrollHeight - element.scrollTop - element.clientHeight <= FOLLOW_SLACK_PX;
66 }}
67 className="max-h-80 overflow-auto text-muted-foreground"
68 >
69 {spans.map((span, index) =>
70 span.style === null ? (
71 span.text
72 ) : (
73 // Spans are rebuilt from scratch on every update and never reorder.
74 // oxlint-disable-next-line react/no-array-index-key
75 <span key={index} style={span.style}>
76 {span.text}
77 </span>
78 ),
79 )}
80 </div>
81 </div>
82 );