@cueloop/client 0.1.0-alpha.66 → 0.1.0-alpha.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/package.json +6 -5
  2. package/src/App.autoclose.test.tsx +2 -2
  3. package/src/App.diff.test.tsx +71 -39
  4. package/src/App.plan-review.test.tsx +20 -21
  5. package/src/App.readonly.test.tsx +7 -3
  6. package/src/App.test.tsx +73 -14
  7. package/src/App.tsx +375 -129
  8. package/src/App.walk.test.tsx +14 -15
  9. package/src/App.workbench.test.tsx +390 -0
  10. package/src/__snapshots__/view-plan.regression.test.ts.snap +1 -1
  11. package/src/annotation-palette.ts +61 -0
  12. package/src/app-screens.tsx +176 -45
  13. package/src/app-view-model.ts +7 -65
  14. package/src/components/AnnotationCard.tsx +1 -1
  15. package/src/components/AnnotationCards.stories.tsx +87 -0
  16. package/src/components/AnnotationCards.tsx +303 -0
  17. package/src/components/AppHeader.stories.tsx +50 -0
  18. package/src/components/AppHeader.tsx +83 -0
  19. package/src/components/AppShell.stories.tsx +47 -42
  20. package/src/components/AppShell.tsx +212 -100
  21. package/src/components/AppShell.zoom.test.tsx +94 -0
  22. package/src/components/ChangesColumn.tsx +49 -26
  23. package/src/components/DiffSheet.scroll.test.tsx +38 -55
  24. package/src/components/DiffSheet.split.test.tsx +140 -0
  25. package/src/components/DiffSheet.stories.tsx +55 -53
  26. package/src/components/DiffSheet.syntax.test.tsx +24 -7
  27. package/src/components/DiffSheet.tsx +607 -276
  28. package/src/components/DiscussionMarkerRail.stories.tsx +54 -0
  29. package/src/components/DiscussionMarkerRail.tsx +181 -0
  30. package/src/components/EditorGrid.stories.tsx +59 -0
  31. package/src/components/EditorGrid.tabs.test.tsx +105 -0
  32. package/src/components/EditorGrid.tsx +338 -0
  33. package/src/components/EditorGrid.zoom.test.tsx +68 -0
  34. package/src/components/FileContentsView.stories.tsx +16 -0
  35. package/src/components/FileContentsView.test.tsx +29 -0
  36. package/src/components/FileContentsView.tsx +78 -0
  37. package/src/components/InboxList.tsx +159 -30
  38. package/src/components/PaneGrid.stories.tsx +84 -0
  39. package/src/components/PaneGrid.tsx +128 -0
  40. package/src/components/PanelColumn.stories.tsx +25 -0
  41. package/src/components/PanelColumn.tsx +95 -0
  42. package/src/components/ProjectTreeView.stories.tsx +25 -0
  43. package/src/components/ProjectTreeView.test.tsx +32 -0
  44. package/src/components/ProjectTreeView.tsx +82 -0
  45. package/src/components/PromptDialog.tsx +8 -1
  46. package/src/components/RootOverlay.stories.tsx +44 -0
  47. package/src/components/RootOverlay.tsx +35 -0
  48. package/src/components/SettingsDialog.tsx +2 -1
  49. package/src/components/SurfaceScrollbar.stories.tsx +34 -0
  50. package/src/components/SurfaceScrollbar.test.tsx +111 -0
  51. package/src/components/SurfaceScrollbar.tsx +93 -0
  52. package/src/components/ThreadFooter.stories.tsx +2 -2
  53. package/src/components/ThreadFooter.test.tsx +31 -0
  54. package/src/components/ThreadFooter.tsx +12 -7
  55. package/src/components/ThreadView.tsx +62 -1440
  56. package/src/components/ThreadsSidebar.tsx +14 -1
  57. package/src/components/Tooltip.stories.tsx +26 -0
  58. package/src/components/Tooltip.tsx +72 -0
  59. package/src/components/WelcomeSurface.stories.tsx +12 -0
  60. package/src/components/WelcomeSurface.tsx +52 -0
  61. package/src/components/__snapshots__/stories.test.tsx.snap +509 -559
  62. package/src/components/diff-sheet-layout.test.ts +125 -55
  63. package/src/components/diff-sheet-layout.ts +3 -103
  64. package/src/components/editor-grid.test.ts +86 -0
  65. package/src/components/editor-grid.ts +138 -0
  66. package/src/components/fade-title.test.ts +49 -0
  67. package/src/components/fade-title.ts +61 -0
  68. package/src/components/file-tree.test.ts +15 -0
  69. package/src/components/file-tree.ts +59 -4
  70. package/src/components/pages/Workspace.stories.tsx +2 -2
  71. package/src/components/primitives/IconButton.tsx +21 -2
  72. package/src/components/primitives/Tree.tsx +13 -4
  73. package/src/components/primitives/icons.ts +27 -4
  74. package/src/components/session-tree.test.ts +20 -0
  75. package/src/components/session-tree.ts +16 -2
  76. package/src/components/stories-app.tsx +2 -2
  77. package/src/config.test.ts +20 -49
  78. package/src/config.ts +26 -28
  79. package/src/diff-fold.test.ts +78 -0
  80. package/src/diff-fold.ts +126 -0
  81. package/src/diff-syntax.test.ts +16 -0
  82. package/src/diff-syntax.ts +6 -3
  83. package/src/discussions.ts +79 -0
  84. package/src/intent-dispatch.test.ts +31 -11
  85. package/src/intent-dispatch.ts +93 -48
  86. package/src/key-bindings.test.ts +1 -1
  87. package/src/keymap.test.ts +3 -25
  88. package/src/keymap.ts +17 -18
  89. package/src/mark-runs.ts +120 -0
  90. package/src/pty.ts +10 -2
  91. package/src/run.ts +17 -7
  92. package/src/serve.test.ts +4 -8
  93. package/src/session-controller.curate.test.ts +75 -0
  94. package/src/session-controller.share.test.ts +1 -0
  95. package/src/session-controller.tree.test.ts +26 -0
  96. package/src/session-controller.ts +153 -29
  97. package/src/slash-palette.ts +83 -0
  98. package/src/split-diff.test.ts +110 -0
  99. package/src/split-diff.ts +149 -0
  100. package/src/test-support.test.ts +3 -3
  101. package/src/test-support.ts +39 -8
  102. package/src/thread-chords.test.ts +25 -9
  103. package/src/thread-chords.ts +33 -8
  104. package/src/use-annotation-surface.tsx +837 -0
  105. package/src/use-changes-workbench.ts +151 -0
  106. package/src/use-frame-measure.ts +38 -0
  107. package/src/use-settings-dialog.tsx +14 -24
  108. package/src/use-terminal-virtualizer.ts +233 -0
  109. package/src/view-diff.ts +114 -0
  110. package/src/components/ReviewPanel.stories.tsx +0 -100
  111. package/src/components/ReviewPanel.tsx +0 -103
  112. package/src/components/ReviewRail.stories.tsx +0 -232
  113. package/src/components/ReviewRail.tsx +0 -327
  114. package/src/review-panel.test.ts +0 -113
  115. package/src/review-panel.ts +0 -83
@@ -0,0 +1,338 @@
1
+ // Renders the Changes pane's editor grid: recursive branches split into rows/columns, each leaf an
2
+ // editor group with its own tab strip and header controls. The caller supplies renderTab so the grid
3
+ // stays layout-only - a tab becomes a diff, a file's contents, or the aggregate Changes view upstream.
4
+
5
+ import React, { useRef, useState } from "react";
6
+ import type { BoxRenderable } from "@opentui/core";
7
+ import { DARK, type Theme } from "../theme";
8
+ import { useFrameMeasure } from "../use-frame-measure";
9
+ import { IconButton } from "./primitives/IconButton";
10
+ import { NERD, HEADER_UNDERLINE_CHARS } from "./primitives/icons";
11
+ import type { EditorGroup, EditorNode, EditorTab, SplitDirection } from "./editor-grid";
12
+
13
+ export interface EditorGridProps {
14
+ tree: EditorNode;
15
+ focusedGroupId: string | null;
16
+ onFocusGroup: (groupId: string) => void;
17
+ onActivateTab: (groupId: string, tabId: string) => void;
18
+ onCloseTab: (groupId: string, tabId: string) => void;
19
+ onSplit: (groupId: string, direction: SplitDirection) => void;
20
+ onZoom: () => void;
21
+ zoomed: boolean;
22
+ /** The body of a group's active tab; `groupFocused` tells a keyboard-owning body whether it has the keys. */
23
+ renderTab: (tab: EditorTab, groupFocused: boolean) => React.ReactNode;
24
+ /** Comments per file path, shown as a dot-and-count badge on a file tab. */
25
+ commentCounts?: ReadonlyMap<string, number>;
26
+ theme?: Theme;
27
+ }
28
+
29
+ const SPLIT_ITEMS: ReadonlyArray<{ label: string; direction: SplitDirection; arrow: string }> = [
30
+ { label: "Split Left", direction: "left", arrow: "←" },
31
+ { label: "Split Right", direction: "right", arrow: "→" },
32
+ { label: "Split Up", direction: "up", arrow: "↑" },
33
+ { label: "Split Down", direction: "down", arrow: "↓" },
34
+ ];
35
+
36
+ function EditorTabButton({
37
+ tab,
38
+ active,
39
+ commentCount,
40
+ onSelect,
41
+ onClose,
42
+ tokens,
43
+ }: {
44
+ tab: EditorTab;
45
+ active: boolean;
46
+ /** Comments on this tab's file, shown as a dot-and-count badge; 0 shows nothing. */
47
+ commentCount: number;
48
+ onSelect: () => void;
49
+ onClose: () => void;
50
+ tokens: Theme;
51
+ }): React.ReactNode {
52
+ const [hovered, setHovered] = useState(false);
53
+
54
+ return (
55
+ <box
56
+ id={tab.id}
57
+ onMouseUp={onSelect}
58
+ onMouseOver={() => setHovered(true)}
59
+ onMouseOut={() => setHovered(false)}
60
+ style={{
61
+ flexDirection: "row",
62
+ // a tab keeps the width of its name; the strip scrolls when the row overflows
63
+ flexShrink: 0,
64
+ paddingLeft: 1,
65
+ paddingRight: 1,
66
+ backgroundColor: active ? tokens.background : undefined,
67
+ borderStyle: "single",
68
+ border: ["right"],
69
+ borderColor: tokens.border,
70
+ }}
71
+ >
72
+ <text fg={active ? tokens.textMuted : tokens.textDim} style={{ wrapMode: "none" }}>
73
+ {tab.label}
74
+ </text>
75
+ {commentCount > 0 ? (
76
+ <text
77
+ fg={tokens.textDim}
78
+ style={{ flexShrink: 0, wrapMode: "none" }}
79
+ >{` ● ${commentCount}`}</text>
80
+ ) : null}
81
+ <box
82
+ onMouseUp={(event) => {
83
+ event.stopPropagation();
84
+ onClose();
85
+ }}
86
+ style={{ paddingLeft: 1 }}
87
+ >
88
+ <text fg={tokens.textDim}>{hovered ? NERD.close : " "}</text>
89
+ </box>
90
+ </box>
91
+ );
92
+ }
93
+
94
+ /** Cells a tab occupies: its padding, the label, the close cell and its padding, the right rule. */
95
+ function tabCellWidth(tab: EditorTab): number {
96
+ return tab.label.length + 5;
97
+ }
98
+
99
+ /** The run of tabs `[first, end)` the strip shows. */
100
+ export interface TabWindow {
101
+ first: number;
102
+ end: number;
103
+ }
104
+
105
+ /** The run of tabs that fits the strip and contains the active tab; markers take a cell each side. */
106
+ export function visibleTabWindow(
107
+ tabs: EditorTab[],
108
+ firstVisible: number,
109
+ activeIndex: number,
110
+ stripWidth: number,
111
+ ): TabWindow {
112
+ // unmeasured strip: show everything, the header clips until the width is known
113
+ if (stripWidth <= 0) return { first: 0, end: tabs.length };
114
+ const fitEnd = (first: number): number => {
115
+ let used = first > 0 ? 1 : 0;
116
+ let end = first;
117
+
118
+ while (end < tabs.length) {
119
+ const remaining = end + 1 < tabs.length ? 1 : 0;
120
+
121
+ if (used + tabCellWidth(tabs[end]!) + remaining > stripWidth) break;
122
+ used += tabCellWidth(tabs[end]!);
123
+ end++;
124
+ }
125
+
126
+ // always show at least the tab the window starts on, even when it is wider than the strip
127
+ return Math.max(end, Math.min(first + 1, tabs.length));
128
+ };
129
+ let first = Math.min(Math.max(0, firstVisible), Math.max(0, activeIndex));
130
+ let end = fitEnd(first);
131
+
132
+ // the active tab past the right edge pulls the window along until it fits
133
+ while (activeIndex >= end && first < activeIndex) {
134
+ first++;
135
+ end = fitEnd(first);
136
+ }
137
+
138
+ return { first, end };
139
+ }
140
+
141
+ function SplitMenu({
142
+ onPick,
143
+ tokens,
144
+ }: {
145
+ onPick: (direction: SplitDirection) => void;
146
+ tokens: Theme;
147
+ }): React.ReactNode {
148
+ return (
149
+ <box
150
+ style={{
151
+ flexDirection: "column",
152
+ flexShrink: 0,
153
+ borderStyle: "single",
154
+ borderColor: tokens.border,
155
+ // the panel token is transparent in the branded theme, so the menu reads terminal-through
156
+ backgroundColor: tokens.panel,
157
+ }}
158
+ >
159
+ {SPLIT_ITEMS.map((item) => (
160
+ <box
161
+ key={item.direction}
162
+ onMouseUp={() => onPick(item.direction)}
163
+ style={{ flexDirection: "row", paddingLeft: 1, paddingRight: 1 }}
164
+ >
165
+ <text fg={tokens.text}>{item.label}</text>
166
+ <box style={{ flexGrow: 1 }} />
167
+ <text fg={tokens.textDim}>{` ${item.arrow}`}</text>
168
+ </box>
169
+ ))}
170
+ </box>
171
+ );
172
+ }
173
+
174
+ function EditorGroupPane({
175
+ group,
176
+ props,
177
+ }: {
178
+ group: EditorGroup;
179
+ props: EditorGridProps;
180
+ }): React.ReactNode {
181
+ const tokens = props.theme ?? DARK;
182
+ const [menuOpen, setMenuOpen] = useState(false);
183
+ const active = group.tabs.find((tab) => tab.id === group.activeTabId) ?? group.tabs[0];
184
+ const isFile = active?.kind === "file";
185
+ // The strip windows its tabs rather than scrolling a nested scrollbox (which stalls the
186
+ // renderer inside this header): tabs keep the width of their names, only the run that fits
187
+ // renders, and the window follows the active tab so a tab opened past the edge is never hidden.
188
+ const stripRef = useRef<BoxRenderable | null>(null);
189
+ const stripWidth = useFrameMeasure(
190
+ () => stripRef.current?.width ?? 0,
191
+ (left, right) => left === right,
192
+ 0,
193
+ );
194
+ const [firstVisible, setFirstVisible] = useState(0);
195
+ const activeIndex = Math.max(
196
+ 0,
197
+ group.tabs.findIndex((tab) => tab.id === active?.id),
198
+ );
199
+ // derived every render: the manual offset is a floor the active tab can pull the window past
200
+ const tabWindow = visibleTabWindow(group.tabs, firstVisible, activeIndex, stripWidth);
201
+
202
+ return (
203
+ <box
204
+ onMouseDown={() => props.onFocusGroup(group.id)}
205
+ style={{ flexDirection: "column", flexGrow: 1, flexBasis: 0, minWidth: 0, minHeight: 0 }}
206
+ >
207
+ <box
208
+ customBorderChars={HEADER_UNDERLINE_CHARS}
209
+ style={{
210
+ flexDirection: "row",
211
+ // pin the controls to the right edge so their column never shifts with the container
212
+ // width parity; a lone flexGrow tab strip would drag them a cell on zoom
213
+ justifyContent: "space-between",
214
+ height: 2,
215
+ // match the pane headers: the panel token is transparent in the branded theme, so the
216
+ // header reads as terminal-through rather than a raised box
217
+ backgroundColor: tokens.panel,
218
+ borderStyle: "single",
219
+ border: ["bottom"],
220
+ borderColor: tokens.border,
221
+ }}
222
+ >
223
+ {/* the tab strip windows its tabs: the run that fits renders in full, edge markers step
224
+ the window, and the controls stay pinned on the right */}
225
+ <box
226
+ ref={stripRef}
227
+ style={{ flexDirection: "row", flexGrow: 1, flexShrink: 1, flexBasis: 0, minWidth: 0 }}
228
+ >
229
+ {tabWindow.first > 0 ? (
230
+ <box onMouseUp={() => setFirstVisible(tabWindow.first - 1)} style={{ flexShrink: 0 }}>
231
+ <text fg={tokens.textDim}>{"‹"}</text>
232
+ </box>
233
+ ) : null}
234
+ {group.tabs.slice(tabWindow.first, tabWindow.end).map((tab) => (
235
+ <EditorTabButton
236
+ key={tab.id}
237
+ tab={tab}
238
+ active={tab.id === active?.id}
239
+ commentCount={tab.path ? (props.commentCounts?.get(tab.path) ?? 0) : 0}
240
+ onSelect={() => props.onActivateTab(group.id, tab.id)}
241
+ onClose={() => props.onCloseTab(group.id, tab.id)}
242
+ tokens={tokens}
243
+ />
244
+ ))}
245
+ {tabWindow.end < group.tabs.length ? (
246
+ <box onMouseUp={() => setFirstVisible(tabWindow.first + 1)} style={{ flexShrink: 0 }}>
247
+ <text fg={tokens.textDim}>{"›"}</text>
248
+ </box>
249
+ ) : null}
250
+ </box>
251
+ <box style={{ flexDirection: "row", flexShrink: 0, paddingLeft: 1, paddingRight: 1 }}>
252
+ <IconButton glyph="search" onPress={() => {}} marginRight={2} theme={tokens} />
253
+ <IconButton
254
+ glyph={NERD.zoom}
255
+ active={props.zoomed}
256
+ onPress={props.onZoom}
257
+ tip={props.zoomed ? "Zoom Out" : "Zoom In"}
258
+ marginRight={isFile ? 2 : 0}
259
+ theme={tokens}
260
+ />
261
+ {isFile ? (
262
+ <IconButton
263
+ glyph="split"
264
+ active={menuOpen}
265
+ onPress={() => setMenuOpen((open) => !open)}
266
+ theme={tokens}
267
+ />
268
+ ) : null}
269
+ </box>
270
+ </box>
271
+ {menuOpen ? (
272
+ <SplitMenu
273
+ onPick={(direction) => {
274
+ setMenuOpen(false);
275
+ props.onSplit(group.id, direction);
276
+ }}
277
+ tokens={tokens}
278
+ />
279
+ ) : null}
280
+ <box style={{ flexGrow: 1, flexDirection: "column" }}>
281
+ {active ? props.renderTab(active, props.focusedGroupId === group.id) : null}
282
+ </box>
283
+ </box>
284
+ );
285
+ }
286
+
287
+ function GridNode({ node, props }: { node: EditorNode; props: EditorGridProps }): React.ReactNode {
288
+ if (node.type === "group") {
289
+ return <EditorGroupPane group={node} props={props} />;
290
+ }
291
+ const tokens = props.theme ?? DARK;
292
+ const horizontal = node.orientation === "horizontal";
293
+ return (
294
+ <box
295
+ style={{
296
+ flexDirection: horizontal ? "row" : "column",
297
+ flexGrow: 1,
298
+ flexBasis: 0,
299
+ minWidth: 0,
300
+ minHeight: 0,
301
+ }}
302
+ >
303
+ {node.children.map((child, index) => (
304
+ <React.Fragment key={child.id}>
305
+ {index > 0 ? (
306
+ <box
307
+ style={{
308
+ borderStyle: "single",
309
+ border: [horizontal ? "left" : "top"],
310
+ borderColor: tokens.border,
311
+ }}
312
+ />
313
+ ) : null}
314
+ <box
315
+ style={{
316
+ flexDirection: "column",
317
+ flexGrow: 1,
318
+ flexBasis: 0,
319
+ minWidth: 0,
320
+ minHeight: 0,
321
+ }}
322
+ >
323
+ <GridNode node={child} props={props} />
324
+ </box>
325
+ </React.Fragment>
326
+ ))}
327
+ </box>
328
+ );
329
+ }
330
+
331
+ /** The Changes pane body: the editor grid of groups and splits, each rendering its active tab. */
332
+ export function EditorGrid(props: EditorGridProps): React.ReactNode {
333
+ return (
334
+ <box style={{ flexDirection: "column", flexGrow: 1, minWidth: 0, minHeight: 0 }}>
335
+ <GridNode node={props.tree} props={props} />
336
+ </box>
337
+ );
338
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Regression: the header controls (search, zoom, split) stay pinned to the right
3
+ * edge whatever the container width parity. A lone flexGrow tab strip used to
4
+ * underfill by a cell at even widths, dragging the zoom icon a column left when
5
+ * zoom widened the pane; space-between pins the controls so the column is stable.
6
+ */
7
+
8
+ import { describe, expect, test } from "bun:test";
9
+ import { testRender } from "@opentui/react/test-utils";
10
+ import React from "react";
11
+ import { EditorGrid } from "./EditorGrid";
12
+ import { changesTab, fileTab, makeGroup } from "./editor-grid";
13
+ import { allowEventLoopUpdates } from "../test-support";
14
+
15
+ const grid = makeGroup([changesTab(), fileTab("App.tsx", "src/App.tsx", "diff")]);
16
+
17
+ async function zoomIconInsetFromRight(width: number, zoomed: boolean): Promise<number> {
18
+ const setup = await testRender(
19
+ <EditorGrid
20
+ tree={grid}
21
+ focusedGroupId={grid.id}
22
+ onFocusGroup={() => {}}
23
+ onActivateTab={() => {}}
24
+ onCloseTab={() => {}}
25
+ onSplit={() => {}}
26
+ onZoom={() => {}}
27
+ zoomed={zoomed}
28
+ renderTab={() => <text>body</text>}
29
+ />,
30
+ { width, height: 6 },
31
+ );
32
+
33
+ allowEventLoopUpdates();
34
+ await setup.waitForVisualIdle();
35
+ const line = setup
36
+ .captureCharFrame()
37
+ .split("\n")
38
+ .find((row) => row.includes("⛶"));
39
+ const inset = line ? width - line.indexOf("⛶") : -1;
40
+
41
+ setup.renderer.destroy();
42
+
43
+ return inset;
44
+ }
45
+
46
+ describe("zoom icon position", () => {
47
+ test("stays a fixed inset from the right edge across width parities", async () => {
48
+ // Act - even and odd widths flip the flexGrow rounding the old layout was sensitive to
49
+ const insets = await Promise.all([
50
+ zoomIconInsetFromRight(100, false),
51
+ zoomIconInsetFromRight(101, false),
52
+ zoomIconInsetFromRight(102, false),
53
+ ]);
54
+
55
+ // Assert - one inset for all widths: the icon is pinned right, not drifting with parity
56
+ expect(new Set(insets).size).toBe(1);
57
+ expect(insets[0]).toBeGreaterThan(0);
58
+ });
59
+
60
+ test("does not shift when zoom becomes active", async () => {
61
+ // Act
62
+ const inactive = await zoomIconInsetFromRight(100, false);
63
+ const active = await zoomIconInsetFromRight(100, true);
64
+
65
+ // Assert - active only recolours the glyph; it must not move
66
+ expect(active).toBe(inactive);
67
+ });
68
+ });
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ import { DARK } from "../theme";
3
+ import type { Story, StoryMeta } from "./story";
4
+ import { FileContentsView } from "./FileContentsView";
5
+
6
+ export const meta: StoryMeta = { title: "Surfaces/FileContentsView" };
7
+
8
+ const SAMPLE = `export function add(a: number, b: number): number {\n return a + b;\n}\n`;
9
+
10
+ export const Loaded: Story = {
11
+ render: () => (
12
+ <FileContentsView path="src/add.ts" loadContents={() => Promise.resolve(SAMPLE)} theme={DARK} />
13
+ ),
14
+ expectedColors: [DARK.textDim],
15
+ size: { width: 60, height: 12 },
16
+ };
@@ -0,0 +1,29 @@
1
+ import { expect, test } from "bun:test";
2
+ import React from "react";
3
+ import { testRender } from "@opentui/react/test-utils";
4
+ import { FileContentsView } from "./FileContentsView";
5
+ import { waitForText } from "../test-support";
6
+ import { DARK } from "../theme";
7
+
8
+ test("renders the loaded file contents", async () => {
9
+ const setup = await testRender(
10
+ <FileContentsView
11
+ path="src/add.ts"
12
+ loadContents={() => Promise.resolve("const answer = 42;\n")}
13
+ theme={DARK}
14
+ />,
15
+ { width: 60, height: 10 },
16
+ );
17
+
18
+ await waitForText(setup, "const answer = 42;");
19
+ });
20
+
21
+ test("shows a not-found hint when the file cannot be read", async () => {
22
+ const setup = await testRender(
23
+ <FileContentsView path="missing.ts" loadContents={() => Promise.resolve(null)} theme={DARK} />,
24
+ { width: 60, height: 10 },
25
+ );
26
+
27
+ await waitForText(setup, "could not read missing.ts");
28
+ expect(setup.captureCharFrame()).toContain("could not read missing.ts");
29
+ });
@@ -0,0 +1,78 @@
1
+ // A Changes file tab in contents mode: a workspace file rendered read-only with line numbers.
2
+ // Loads on mount and whenever the path changes; a null read shows a "could not read" hint.
3
+
4
+ import React, { useEffect, useRef, useState } from "react";
5
+ import type { Theme } from "../theme";
6
+ import { useComponentTheme } from "./theme-context";
7
+
8
+ export interface FileContentsViewProps {
9
+ path: string;
10
+ loadContents: (path: string) => Promise<string | null>;
11
+ theme?: Theme;
12
+ }
13
+
14
+ /** A finished read for a specific path; `lines` is null when the file could not be read. */
15
+ interface FileLoad {
16
+ path: string;
17
+ lines: string[] | null;
18
+ }
19
+
20
+ export function FileContentsView({
21
+ path,
22
+ loadContents,
23
+ theme,
24
+ }: FileContentsViewProps): React.ReactNode {
25
+ const tokens = useComponentTheme(theme);
26
+ const [loaded, setLoaded] = useState<FileLoad | null>(null);
27
+ const loadRef = useRef(loadContents);
28
+ useEffect(() => {
29
+ loadRef.current = loadContents;
30
+ });
31
+
32
+ useEffect(() => {
33
+ let alive = true;
34
+ void loadRef.current(path).then(
35
+ (contents) => {
36
+ if (alive) setLoaded({ path, lines: contents === null ? null : contents.split("\n") });
37
+ },
38
+ () => {
39
+ if (alive) setLoaded({ path, lines: null });
40
+ },
41
+ );
42
+
43
+ return () => {
44
+ alive = false;
45
+ };
46
+ }, [path]);
47
+
48
+ if (loaded === null || loaded.path !== path) {
49
+ return (
50
+ <box style={{ flexGrow: 1, paddingLeft: 1, paddingTop: 1 }}>
51
+ <text fg={tokens.textDim}>loading...</text>
52
+ </box>
53
+ );
54
+ }
55
+ if (loaded.lines === null) {
56
+ return (
57
+ <box style={{ flexGrow: 1, paddingLeft: 1, paddingTop: 1 }}>
58
+ <text fg={tokens.textDim}>{`could not read ${path}`}</text>
59
+ </box>
60
+ );
61
+ }
62
+ const lines = loaded.lines;
63
+
64
+ return (
65
+ <scrollbox style={{ flexGrow: 1 }} focused={false}>
66
+ <box style={{ flexDirection: "column", paddingTop: 1 }}>
67
+ {lines.map((line, index) => (
68
+ <box key={index} style={{ flexDirection: "row", paddingLeft: 1 }}>
69
+ <box style={{ width: 5 }}>
70
+ <text fg={tokens.textDim}>{String(index + 1).padStart(4)}</text>
71
+ </box>
72
+ <text fg={tokens.text}>{line.length > 0 ? line : " "}</text>
73
+ </box>
74
+ ))}
75
+ </box>
76
+ </scrollbox>
77
+ );
78
+ }