Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { LegendList } from "@legendapp/list/react";
3import type { KeyboardEventHandler } from "react";
4
5import { Spinner } from "~/components/ui/spinner";
6import { FileMarkdownPreview } from "./FileMarkdownPreview";
7import {
8 LARGE_MARKDOWN_CHUNK_MAX_CHARS,
9 type MarkdownChunk,
10 withMarkdownDefinitions,
11} from "@t3tools/shared/markdownChunks";
12
13const ESTIMATED_LINE_PX = 24;
14
15interface ChunkItem {
16 readonly index: number;
17 readonly start: number;
18 readonly chunk: MarkdownChunk;
19}
20
21/** Renders Markdown pieces in a virtualized list; task offsets are file offsets. */
22export function ChunkedMarkdown(props: {
23 readonly chunks: readonly MarkdownChunk[];
24 /** Link definitions from the whole file, so references resolve across pieces. */
25 readonly definitions: string;
26 /** Shows a spinner after the last chunk while more of the file streams in. */
27 readonly loading?: boolean;
28 readonly cwd: string;
29 readonly relativePath: string;
30 readonly threadRef: ScopedThreadRef;
31 readonly workspaceMutationId: string | null;
32 readonly onKeyDown?: KeyboardEventHandler<HTMLDivElement>;
33 readonly onTaskListChange?:
34 | ((input: { readonly markerOffset: number; readonly checked: boolean }) => void)
35 | undefined;
36}) {
37 const { chunks, onTaskListChange } = props;
38 const items: ChunkItem[] = [];
39 let start = 0;
40 let lines = 0;
41 for (const [index, chunk] of chunks.entries()) {
42 items.push({ index, start, chunk });
43 lines += chunk.lines;
44 start += chunk.text.length + 1;
45 }
46 return (
47 <div
48 // Clicking the text focuses the preview, so Select All stays inside it.
49 tabIndex={-1}
50 className="flex min-h-0 flex-1 flex-col outline-none"
51 onKeyDown={props.onKeyDown}
52 onCopy={(event) => {
53 // Select All only reaches rendered pieces; copying all of it copies the whole file.
54 const list = event.currentTarget.firstElementChild;
55 if (!list || !window.getSelection()?.containsNode(list)) return;
56 event.preventDefault();
57 event.clipboardData.setData("text/plain", chunks.map((chunk) => chunk.text).join("\n"));
58 }}
59 >
60 <LegendList<ChunkItem>
61 data={items}
62 extraData={props.definitions}
63 keyExtractor={(item) => String(item.index)}
64 estimatedItemSize={(lines / Math.max(items.length, 1)) * ESTIMATED_LINE_PX}
65 drawDistance={800}
66 recycleItems={false}
67 className="h-full scrollbar-gutter-both"
68 contentContainerClassName="py-5"
69 ListFooterComponent={
70 props.loading ? (
71 <div className="flex justify-center py-4">
72 <Spinner size="md" tone="muted" />
73 </div>
74 ) : null
75 }
76 renderItem={({ item }) => (
77 <div className="pb-5">
78 {item.chunk.text.length > LARGE_MARKDOWN_CHUNK_MAX_CHARS ? (
79 // One block this long (a huge fence or HTML block) is too slow to parse.
80 <pre className="mx-auto max-w-4xl overflow-x-auto px-6 font-mono text-xs whitespace-pre">
81 {item.chunk.text}
82 </pre>
83 ) : (
84 <FileMarkdownPreview
85 text={withMarkdownDefinitions(item.chunk.text, props.definitions)}
86 cwd={props.cwd}
87 relativePath={props.relativePath}
88 threadRef={props.threadRef}
89 workspaceMutationId={props.workspaceMutationId}
90 className="mx-auto max-w-4xl px-6"
91 onTaskListChange={
92 onTaskListChange &&
93 ((input) =>
94 onTaskListChange({
95 markerOffset: item.start + input.markerOffset,
96 checked: input.checked,
97 }))
98 }
99 />
100 )}
101 </div>
102 )}
103 />
104 </div>
105 );
106}