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

LargeFilePreview.tsx

125 lines4.5 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { DEFAULT_TOKENIZE_MAX_LENGTH } from "@pierre/diffs";
3import type { FileOptions } from "@pierre/diffs/react";
4import { useGT } from "gt-react";
5import { lazy, Suspense, useImperativeHandle, type RefObject } from "react";
6​
7import { ChunkedMarkdown } from "./ChunkedMarkdown";
8import { FileSurfaceLoading } from "./fileSurfaceChrome";
9import { useFullFileMarkdown, useFullFileText } from "./fullFileText";
10import { FULL_TEXT_MAX_BYTES } from "./largeTextProtocol";
11import SourceFilePreview from "./ReadOnlySourcePreview";
12​
13const LargeTextViewer = lazy(() => import("./LargeTextViewer"));
14​
15/**
16 * A file past the 1 MB read, shown like any other file once its full text arrives. Tables, and
17 * files too large to hold or lay out, use the worker-backed plain viewer.
18 */
19export default function LargeFilePreview(props: {
20 readonly src: string;
21 readonly name: string;
22 /** The first megabyte already read. */
23 readonly preview: string;
24 readonly sizeBytes: number;
25 readonly delimiter: "," | "\t" | null;
26 /** Renders the file as Markdown resolving links against this workspace; null shows source. */
27 readonly markdownCwd: string | null;
28 readonly threadRef: ScopedThreadRef;
29 readonly workspaceMutationId: string | null;
30 readonly copyContents: RefObject<(() => Promise<string>) | null>;
31 readonly onRetry: () => void;
32 readonly onPostRender?: FileOptions<unknown, undefined>["onPostRender"];
33 readonly revealLine: number | null;
34 readonly revealRequestId: number;
35 readonly wordWrap: boolean;
36}) {
37 const plain = props.delimiter !== null || props.sizeBytes > FULL_TEXT_MAX_BYTES;
38 return plain ? <PlainViewer {...props} /> : <FullTextPreview {...props} />;
40​
41// Pierre keeps every token of a highlighted file in memory: a 320k-line log took about 280 MB.
42const HIGHLIGHT_MAX_LINES = DEFAULT_TOKENIZE_MAX_LENGTH;
43​
44/** Estimates the whole file's lines from the preview, so highlighting is decided once. */
45function estimatedLines(preview: string, sizeBytes: number): number {
46 let lines = 1;
47 for (let index = preview.indexOf("\n"); index !== -1; index = preview.indexOf("\n", index + 1))
48 lines += 1;
49 return (lines / Math.max(1, new TextEncoder().encode(preview).length)) * sizeBytes;
51​
52function FullTextPreview(props: Parameters<typeof LargeFilePreview>[0]) {
53 const file = useFullFileText(props.src, props.preview);
54 // Deciding from the full text would highlight the preview, then drop it once the file arrives.
55 const highlight = estimatedLines(props.preview, props.sizeBytes) <= HIGHLIGHT_MAX_LINES;
56 if (file === "too-large" || file === "failed") return <PlainViewer {...props} />;
57 return (
58 <>
59 <CopyText copyContents={props.copyContents} text={file.done ? file.text : null} />
60 {props.markdownCwd !== null ? (
61 <MarkdownText {...props} markdownCwd={props.markdownCwd} file={file} />
62 ) : (
63 <SourceFilePreview
64 name={props.name}
65 text={file.text}
66 onPostRender={props.onPostRender}
67 wordWrap={props.wordWrap}
68 tokenizeMaxLength={highlight ? Number.POSITIVE_INFINITY : 0}
69 />
70 )}
71 </>
72 );
74​
75function MarkdownText(
76 props: Parameters<typeof LargeFilePreview>[0] & {
77 readonly markdownCwd: string;
78 readonly file: { readonly text: string; readonly done: boolean };
79 },
80) {
81 return (
82 <ChunkedMarkdown
83 {...useFullFileMarkdown(props.src, props.file.text)}
84 loading={!props.file.done}
85 cwd={props.markdownCwd}
86 relativePath={props.name}
87 threadRef={props.threadRef}
88 workspaceMutationId={props.workspaceMutationId}
89 />
90 );
92​
93function CopyText(props: {
94 readonly copyContents: RefObject<(() => Promise<string>) | null>;
95 readonly text: string | null;
96}) {
97 const gt = useGT();
98 const { text } = props;
99 useImperativeHandle(
100 props.copyContents,
101 () => () =>
102 text === null
103 ? Promise.reject(new Error(gt("The file is still loading. Try again in a moment.")))
104 : Promise.resolve(text),
105 [text, gt],
106 );
107 return null;
109​
110function PlainViewer(props: Parameters<typeof LargeFilePreview>[0]) {
111 return (
112 <Suspense fallback={<FileSurfaceLoading />}>
113 <LargeTextViewer
114 name={props.name}
115 src={props.src}
116 copyContents={props.copyContents}
117 onRetry={props.onRetry}
118 delimiter={props.delimiter}
119 revealLine={props.revealLine}
120 revealRequestId={props.revealRequestId}
121 wordWrap={props.wordWrap}
122 />
123 </Suspense>
124 );