Open History to see the last commit
1import {
2 type ApprovalTextPart,
3 approvalTextParts,
4} from "@t3tools/client-runtime/state/thread-requests";
5import { T, useGT, plural, part as gtPart } 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`${plural(
42 { count: hiddenLength },
43 {
44 one: gtPart`… ${{ hidden: hiddenLength.toLocaleString() }} character in the middle is not shown.`,
45 other: gtPart`… ${{ hidden: hiddenLength.toLocaleString() }} characters in the middle are not shown.`,
46 },
47 )} The text ends with:`}
48 </span>
49 <ApprovalTextBox
50 parts={tail}
51 className={className}
52 detail="end"
53 fromEnd
54 onClipped={setEndClipped}
55 />
56 {clipped || endClipped ? (
57 <span className="block font-sans text-xs text-warning-foreground">
58 <T>Scroll each part to read all of it.</T>
59 </span>
60 ) : null}
61 </>
62 ) : clipped ? (
63 <span className="block font-sans text-xs text-warning-foreground">
64 <T>Scroll to read all of it.</T>
65 </span>
66 ) : null}
67 </>
68 );
69}
70
71function ApprovalTextBox({
72 parts,
73 label,
74 className,
75 detail,
76 fromEnd = false,
77 onClipped,
78}: {
79 parts: ReadonlyArray<ApprovalTextPart>;
80 label?: string | undefined;
81 className?: string | undefined;
82 detail: "complete" | "cut" | "end";
83 /** Opens scrolled to its last line: the end of the text is what this box is for. */
84 fromEnd?: boolean;
85 onClipped: (clipped: boolean) => void;
86}) {
87 const watch = (box: HTMLElement | null) => {
88 if (!box) return;
89 if (fromEnd) box.scrollTop = box.scrollHeight;
90 const measure = () => onClipped(box.scrollHeight > box.clientHeight + 1);
91 measure();
92 return observeResize(box, measure);
93 };
94 return (
95 <code
96 ref={watch}
97 aria-label={label}
98 className={cn(
99 "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",
100 className,
101 )}
102 data-approval-detail={detail}
103 tabIndex={0}
104 >
105 {parts.map((part, index) =>
106 part.named ? (
107 // oxlint-disable-next-line react/no-array-index-key -- parts never reorder and two may read alike
108 <span key={index} className="rounded-xs bg-warning-surface text-warning-foreground">
109 {part.text}
110 </span>
111 ) : (
112 part.text
113 ),
114 )}
115 </code>
116 );
117}