Open History to see the last commit
1import {
2 type ApprovalTextPart,
3 approvalTextParts,
4} from "@t3tools/client-runtime/state/thread-requests";
5import { T, useGT } from "gt-react";
6import { useState } from "react";
7import { observeResize } from "~/lib/observeResize";
8import { cn } from "~/lib/utils";
9
10/**
11 * What an approval asks to run, as it will run: characters a screen would not draw are shown by
12 * name, and a note says when the text runs past its box. Text too long to show in full is shown
13 * as its beginning and its end, with a note between them of how much is left out.
14 */
15export function ApprovalText({
16 text,
17 label,
18 className,
19}: {
20 text: string;
21 label?: string;
22 className?: string;
23}) {
24 const gt = useGT();
25 const [clipped, setClipped] = useState(false);
26 const [endClipped, setEndClipped] = useState(false);
27 const { parts, tail, hiddenLength } = approvalTextParts(text);
28 const cut = hiddenLength > 0;
29 return (
30 <>
31 <ApprovalTextBox
32 parts={parts}
33 label={label}
34 className={className}
35 detail={cut ? "cut" : "complete"}
36 onClipped={setClipped}
37 />
38 {cut ? (
39 <>
40 <span className="block font-sans text-xs text-warning-foreground">
41 {gt(
42 "{count, plural, one {… {hidden} character in the middle is not shown.} other {… {hidden} characters in the middle are not shown.}} The text ends with:",
43 { count: hiddenLength, hidden: hiddenLength.toLocaleString() },
44 )}
45 </span>
46 <ApprovalTextBox
47 parts={tail}
48 className={className}
49 detail="end"
50 fromEnd
51 onClipped={setEndClipped}
52 />
53 {clipped || endClipped ? (
54 <span className="block font-sans text-xs text-warning-foreground">
55 <T>Scroll each part to read all of it.</T>
56 </span>
57 ) : null}
58 </>
59 ) : clipped ? (
60 <span className="block font-sans text-xs text-warning-foreground">
61 <T>Scroll to read all of it.</T>
62 </span>
63 ) : null}
64 </>
65 );
66}
67
68function ApprovalTextBox({
69 parts,
70 label,
71 className,
72 detail,
73 fromEnd = false,
74 onClipped,
75}: {
76 parts: ReadonlyArray<ApprovalTextPart>;
77 label?: string | undefined;
78 className?: string | undefined;
79 detail: "complete" | "cut" | "end";
80 /** Opens scrolled to its last line: the end of the text is what this box is for. */
81 fromEnd?: boolean;
82 onClipped: (clipped: boolean) => void;
83}) {
84 const watch = (box: HTMLElement | null) => {
85 if (!box) return;
86 if (fromEnd) box.scrollTop = box.scrollHeight;
87 const measure = () => onClipped(box.scrollHeight > box.clientHeight + 1);
88 measure();
89 return observeResize(box, measure);
90 };
91 return (
92 <code
93 ref={watch}
94 aria-label={label}
95 className={cn(
96 "block max-h-40 min-w-0 overflow-auto whitespace-pre-wrap font-mono text-xs leading-5 text-foreground select-text! wrap-anywhere [scrollbar-width:thin] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
97 className,
98 )}
99 data-approval-detail={detail}
100 tabIndex={0}
101 >
102 {parts.map((part, index) =>
103 part.named ? (
104 // oxlint-disable-next-line react/no-array-index-key -- parts never reorder and two may read alike
105 <span key={index} className="rounded-xs bg-warning-surface text-warning-foreground">
106 {part.text}
107 </span>
108 ) : (
109 part.text
110 ),
111 )}
112 </code>
113 );
114}