Open History to see the last commit
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?:
33 | ((complete: boolean) => FileOptions<unknown, undefined>["onPostRender"])
34 | undefined;
35 readonly revealLine: number | null;
36 readonly revealRequestId: number;
37 readonly wordWrap: boolean;
38}) {
39 const plain = props.delimiter !== null || props.sizeBytes > FULL_TEXT_MAX_BYTES;
40 return plain ? <PlainViewer {...props} /> : <FullTextPreview {...props} />;
41}
42
43// Pierre keeps every token of a highlighted file in memory: a 320k-line log took about 280 MB.
44const HIGHLIGHT_MAX_LINES = DEFAULT_TOKENIZE_MAX_LENGTH;
45
46/** Estimates the whole file's lines from the preview, so highlighting is decided once. */
47function estimatedLines(preview: string, sizeBytes: number): number {
48 let lines = 1;
49 for (let index = preview.indexOf("\n"); index !== -1; index = preview.indexOf("\n", index + 1))
50 lines += 1;
51 return (lines / Math.max(1, new TextEncoder().encode(preview).length)) * sizeBytes;
52}
53
54function FullTextPreview(props: Parameters<typeof LargeFilePreview>[0]) {
55 const file = useFullFileText(props.src, props.preview);
56 // Deciding from the full text would highlight the preview, then drop it once the file arrives.
57 const highlight = estimatedLines(props.preview, props.sizeBytes) <= HIGHLIGHT_MAX_LINES;
58 if (file === "too-large" || file === "failed") return <PlainViewer {...props} />;
59 return (
60 <>
61 <CopyText copyContents={props.copyContents} text={file.done ? file.text : null} />
62 {props.markdownCwd !== null ? (
63 <MarkdownText {...props} markdownCwd={props.markdownCwd} file={file} />
64 ) : (
65 <SourceFilePreview
66 name={props.name}
67 text={file.text}
68 onPostRender={props.onPostRender?.(file.done)}
69 wordWrap={props.wordWrap}
70 tokenizeMaxLength={highlight ? Number.POSITIVE_INFINITY : 0}
71 />
72 )}
73 </>
74 );
75}
76
77function MarkdownText(
78 props: Parameters<typeof LargeFilePreview>[0] & {
79 readonly markdownCwd: string;
80 readonly file: { readonly text: string; readonly done: boolean };
81 },
82) {
83 return (
84 <ChunkedMarkdown
85 {...useFullFileMarkdown(props.src, props.file.text)}
86 loading={!props.file.done}
87 cwd={props.markdownCwd}
88 relativePath={props.name}
89 threadRef={props.threadRef}
90 workspaceMutationId={props.workspaceMutationId}
91 />
92 );
93}
94
95function CopyText(props: {
96 readonly copyContents: RefObject<(() => Promise<string>) | null>;
97 readonly text: string | null;
98}) {
99 const gt = useGT();
100 const { text } = props;
101 useImperativeHandle(
102 props.copyContents,
103 () => () =>
104 text === null
105 ? Promise.reject(new Error(gt("The file is still loading. Try again in a moment.")))
106 : Promise.resolve(text),
107 [text, gt],
108 );
109 return null;
110}
111
112function PlainViewer(props: Parameters<typeof LargeFilePreview>[0]) {
113 return (
114 <Suspense fallback={<FileSurfaceLoading />}>
115 <LargeTextViewer
116 name={props.name}
117 src={props.src}
118 copyContents={props.copyContents}
119 onRetry={props.onRetry}
120 delimiter={props.delimiter}
121 revealLine={props.revealLine}
122 revealRequestId={props.revealRequestId}
123 wordWrap={props.wordWrap}
124 />
125 </Suspense>
126 );
127}