@bevel-software/platform-core-frontend 0.22.0 → 0.23.0
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.
- package/package.json +2 -2
- package/src/core/CoreAppShell.tsx +2 -0
- package/src/core/registry.ts +83 -0
- package/src/lib/api.ts +49 -0
- package/src/modules/access/api.ts +6 -4
- package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +89 -1
- package/src/modules/admin/components/UserAccountsPage.tsx +93 -5
- package/src/modules/auth/components/LoginScreen.tsx +3 -0
- package/src/modules/auth/services/account.api.ts +26 -3
- package/src/modules/change-requests/components/BranchFilePreview.tsx +7 -1
- package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +89 -10
- package/src/modules/git/__tests__/HistoryVersionPreview.test.tsx +479 -0
- package/src/modules/git/components/FileHistoryPanel.tsx +52 -65
- package/src/modules/git/components/HistoryVersionPreview.tsx +410 -0
- package/src/modules/layout/__tests__/ReadOnlyBanner.test.tsx +157 -0
- package/src/modules/layout/components/ReadOnlyBanner.tsx +96 -0
- package/src/modules/setup/__tests__/setup.repository-change.test.tsx +296 -0
- package/src/modules/setup/__tests__/setup.repository.test.tsx +114 -24
- package/src/modules/setup/__tests__/setup.test.tsx +5 -3
- package/src/modules/setup/components/SetupScreen.tsx +187 -46
- package/src/modules/setup/services/setup.api.ts +78 -3
- package/src/modules/workspace/components/FileExplorer.tsx +140 -12
- package/src/modules/workspace/components/FileViewer.tsx +11 -1
- package/src/modules/workspace/components/TreeActionConfirm.tsx +5 -7
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +603 -29
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +23 -6
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +41 -8
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/ImageRenderer.tsx +39 -16
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +27 -7
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/RetryReadButton.tsx +37 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +20 -5
- package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +186 -7
- package/src/modules/workspace/components/renderers/__tests__/readPane.test.tsx +59 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +80 -20
- package/src/modules/workspace/components/renderers/index.ts +6 -1
- package/src/modules/workspace/components/renderers/readPane.ts +22 -0
- package/src/modules/workspace/components/renderers/rendererWorkspace.ts +50 -9
- package/src/modules/workspace/components/renderers/useReadRetry.ts +27 -0
- package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +79 -0
- package/src/modules/workspace/routing/kb-routes.ts +25 -10
- package/src/modules/workspace/services/workspace.api.ts +22 -1
- package/src/shared/markdown/__tests__/hrefs.test.ts +111 -0
- package/src/shared/markdown/hrefs.ts +80 -4
|
@@ -3,8 +3,8 @@ import { History, AlertTriangle, Loader2 } from 'lucide-react';
|
|
|
3
3
|
import type { CommitAttribution, FileDiffPayload } from '@bevel-software/platform-shared';
|
|
4
4
|
import { useGit } from '../state/git.context';
|
|
5
5
|
import { formatRelativeTime } from '../../../lib/utils';
|
|
6
|
-
import { UnifiedDiffView } from './UnifiedDiffView';
|
|
7
6
|
import { MarkdownDiffViewer } from '../../review/components/MarkdownDiffViewer';
|
|
7
|
+
import { HistoryVersionPreview } from './HistoryVersionPreview';
|
|
8
8
|
import { friendlyGitError } from '../services/error-messages';
|
|
9
9
|
import { useEventBus, canonicalizeWorkspaceId } from '../../workflow/state/event-bus.context';
|
|
10
10
|
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
@@ -32,7 +32,6 @@ export function FileHistoryPanel({ filePath }: Props) {
|
|
|
32
32
|
const git = useGit();
|
|
33
33
|
const [commits, setCommits] = useState<CommitAttribution[] | null>(null);
|
|
34
34
|
const [selected, setSelected] = useState<CommitAttribution | null>(null);
|
|
35
|
-
const [diff, setDiff] = useState<string | null>(null);
|
|
36
35
|
// Markdown files render through the same viewer as "Review agent changes":
|
|
37
36
|
// full before/after contents diffed client-side into a rendered-markdown
|
|
38
37
|
// red/green view. `diff` (the raw patch) stays the path for everything else.
|
|
@@ -45,14 +44,13 @@ export function FileHistoryPanel({ filePath }: Props) {
|
|
|
45
44
|
// Pull the stable callbacks out so the effect's deps don't include the whole
|
|
46
45
|
// `git` object — that object is a useMemo result that changes on every status
|
|
47
46
|
// poll, which would otherwise wipe local state (selection, loading) every 30s.
|
|
48
|
-
const { fetchFileHistory,
|
|
47
|
+
const { fetchFileHistory, fetchFileAtChange } = git;
|
|
49
48
|
|
|
50
49
|
useEffect(() => {
|
|
51
50
|
let cancelled = false;
|
|
52
51
|
setLoading(true);
|
|
53
52
|
setError(null);
|
|
54
53
|
setSelected(null);
|
|
55
|
-
setDiff(null);
|
|
56
54
|
setMdPayload(null);
|
|
57
55
|
setCommits(null);
|
|
58
56
|
latestDiffRequestRef.current = null;
|
|
@@ -115,35 +113,24 @@ export function FileHistoryPanel({ filePath }: Props) {
|
|
|
115
113
|
// Clear any banner left over from a previous failed diff so the UI
|
|
116
114
|
// reflects only the currently-selected save's state.
|
|
117
115
|
setError(null);
|
|
118
|
-
setDiff(null);
|
|
119
116
|
setMdPayload(null);
|
|
120
|
-
setDiffLoading(true);
|
|
121
117
|
latestDiffRequestRef.current = commit.sha;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
})
|
|
139
|
-
.finally(() => {
|
|
140
|
-
if (latestDiffRequestRef.current === commit.sha) setDiffLoading(false);
|
|
118
|
+
// Everything that is NOT markdown is loaded by `HistoryVersionPreview`,
|
|
119
|
+
// which needs the save and nothing else: it fetches the patch (and, for
|
|
120
|
+
// a text-fed viewer, the save's text) for itself, keyed on the sha.
|
|
121
|
+
// Only the markdown path still loads here, unchanged.
|
|
122
|
+
if (!isMarkdownPath(filePath)) return;
|
|
123
|
+
setDiffLoading(true);
|
|
124
|
+
fetchFileAtChange(filePath, commit.sha)
|
|
125
|
+
.then(({ baseline, current }) => {
|
|
126
|
+
if (latestDiffRequestRef.current !== commit.sha) return;
|
|
127
|
+
setMdPayload({
|
|
128
|
+
path: filePath,
|
|
129
|
+
kind: baseline === null ? 'added' : current === null ? 'deleted' : 'modified',
|
|
130
|
+
baseline,
|
|
131
|
+
current,
|
|
132
|
+
isBinary: false,
|
|
141
133
|
});
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
fetchFileDiff(filePath, commit.sha)
|
|
145
|
-
.then((d) => {
|
|
146
|
-
if (latestDiffRequestRef.current === commit.sha) setDiff(d);
|
|
147
134
|
})
|
|
148
135
|
.catch((err) => {
|
|
149
136
|
if (latestDiffRequestRef.current === commit.sha) {
|
|
@@ -154,7 +141,7 @@ export function FileHistoryPanel({ filePath }: Props) {
|
|
|
154
141
|
if (latestDiffRequestRef.current === commit.sha) setDiffLoading(false);
|
|
155
142
|
});
|
|
156
143
|
},
|
|
157
|
-
[filePath,
|
|
144
|
+
[filePath, fetchFileAtChange],
|
|
158
145
|
);
|
|
159
146
|
|
|
160
147
|
return (
|
|
@@ -245,41 +232,41 @@ export function FileHistoryPanel({ filePath }: Props) {
|
|
|
245
232
|
{shortSha(selected.sha)}
|
|
246
233
|
</span>
|
|
247
234
|
</div>
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
mdPayload.baseline === null && mdPayload.current === null ? (
|
|
257
|
-
// The commit exists in the file's log but the file is
|
|
258
|
-
// absent on both sides (e.g. a pure rename elsewhere in
|
|
259
|
-
// the commit) — mirror the raw view's empty state.
|
|
260
|
-
<div className="px-3 py-3 text-xs text-ink-muted">
|
|
261
|
-
No file changes in this save.
|
|
235
|
+
{/* Markdown keeps the rendered before/after diff it has
|
|
236
|
+
always had; every other format is the version pane. */}
|
|
237
|
+
{isMarkdownPath(filePath) ? (
|
|
238
|
+
<div className="flex-1 overflow-auto">
|
|
239
|
+
{diffLoading && (
|
|
240
|
+
<div className="flex items-center gap-2 px-3 py-3 text-xs text-ink-muted">
|
|
241
|
+
<Loader2 size={13} className="animate-spin" />
|
|
242
|
+
Loading changes…
|
|
262
243
|
</div>
|
|
263
|
-
)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
244
|
+
)}
|
|
245
|
+
{!diffLoading && mdPayload !== null && (
|
|
246
|
+
mdPayload.baseline === null && mdPayload.current === null ? (
|
|
247
|
+
// The commit exists in the file's log but the file is
|
|
248
|
+
// absent on both sides (e.g. a pure rename elsewhere in
|
|
249
|
+
// the commit) — mirror the raw view's empty state.
|
|
250
|
+
<div className="px-3 py-3 text-xs text-ink-muted">
|
|
251
|
+
No file changes in this save.
|
|
252
|
+
</div>
|
|
253
|
+
) : (
|
|
254
|
+
// No link resolvers: this is a diff of a PAST commit, and a
|
|
255
|
+
// relative link in it has no meaningful "current" document
|
|
256
|
+
// to resolve against — the target may have moved or stopped
|
|
257
|
+
// existing since. Links render inert.
|
|
258
|
+
//
|
|
259
|
+
// No image resolver either: the checked-out tree holds
|
|
260
|
+
// today's copy of a picture, not the one this commit had,
|
|
261
|
+
// so the viewer names each workspace image instead of
|
|
262
|
+
// fetching bytes that may not match (`?ref=` in TODOS.md).
|
|
263
|
+
<MarkdownDiffViewer payload={mdPayload} />
|
|
264
|
+
)
|
|
265
|
+
)}
|
|
266
|
+
</div>
|
|
267
|
+
) : (
|
|
268
|
+
<HistoryVersionPreview filePath={filePath} commit={selected} />
|
|
269
|
+
)}
|
|
283
270
|
</>
|
|
284
271
|
)}
|
|
285
272
|
</div>
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { AlertTriangle, Loader2 } from 'lucide-react';
|
|
3
|
+
import type { CommitAttribution } from '@bevel-software/platform-shared';
|
|
4
|
+
import { Button } from '../../../shared/components';
|
|
5
|
+
import { formatRelativeTime } from '../../../lib/utils';
|
|
6
|
+
import { useGit } from '../state/git.context';
|
|
7
|
+
import { friendlyGitError } from '../services/error-messages';
|
|
8
|
+
import { UnifiedDiffView } from './UnifiedDiffView';
|
|
9
|
+
import { useFileAccess } from '../../access/hooks/useFileAccess';
|
|
10
|
+
import { FilePaneCard } from '../../workspace/components/FilePaneCard';
|
|
11
|
+
import {
|
|
12
|
+
CanDownloadContext,
|
|
13
|
+
DownloadFileButton,
|
|
14
|
+
} from '../../workspace/components/renderers/DownloadFileButton';
|
|
15
|
+
import { RetryReadButton } from '../../workspace/components/renderers/RetryReadButton';
|
|
16
|
+
import { READ_PANE } from '../../workspace/components/renderers/readPane';
|
|
17
|
+
import {
|
|
18
|
+
RendererWorkspaceContext,
|
|
19
|
+
getFileRenderer,
|
|
20
|
+
getRendererLayout,
|
|
21
|
+
hasFileViewer,
|
|
22
|
+
isBinaryFile,
|
|
23
|
+
type RendererFileRef,
|
|
24
|
+
} from '../../workspace/components/renderers';
|
|
25
|
+
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One past save of a file, shown the way the FILE PAGE shows that file.
|
|
29
|
+
*
|
|
30
|
+
* Version history used to have exactly one special case: a `.md` file got a
|
|
31
|
+
* rendered diff, and everything else got `git show`'s patch as coloured text.
|
|
32
|
+
* For an HTML page that meant its source rather than the page; for a PNG or a
|
|
33
|
+
* PDF it meant the single line "Binary files a/… and b/… differ", which tells
|
|
34
|
+
* a reader nothing at all about the version they selected. Every one of those
|
|
35
|
+
* formats already has a viewer on the file page, and a viewer only needs a
|
|
36
|
+
* workspace, a path and — now — a save; so this mounts the same viewer, bound
|
|
37
|
+
* to the selected save, and the reader looks at the actual document.
|
|
38
|
+
*
|
|
39
|
+
* Read-only throughout: a past version has no save path, no lock and no edit
|
|
40
|
+
* mode. Getting an old version back is a download and an upload (Restore is
|
|
41
|
+
* deliberately out of scope, hx-history-file-preview decision 6), which is why
|
|
42
|
+
* "Download this version" is on every save rather than only where there is no
|
|
43
|
+
* preview.
|
|
44
|
+
*
|
|
45
|
+
* Three bodies, decided per format (see {@link previewKind}), and a header
|
|
46
|
+
* that is the same for all three.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/** A read-only save handler for a pane that has no save. */
|
|
50
|
+
const NO_SAVE = (): Promise<void> =>
|
|
51
|
+
Promise.reject(new Error('A past version is read-only.'));
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The formats whose viewer is fed the file's TEXT rather than its bytes.
|
|
55
|
+
*
|
|
56
|
+
* `HtmlRenderer` and `CsvRenderer` render the `content` prop — they never
|
|
57
|
+
* fetch the file — so binding them to a save means fetching that save's text
|
|
58
|
+
* through the history endpoint the markdown view already uses, rather than
|
|
59
|
+
* through `?ref=` on the bytes route.
|
|
60
|
+
*/
|
|
61
|
+
const TEXT_FED_EXTENSIONS = new Set(['.html', '.htm', '.csv']);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The formats whose pane offers "Source changes".
|
|
65
|
+
*
|
|
66
|
+
* The three text-fed ones plus `.svg`: all four are files a technical reader
|
|
67
|
+
* may well want to read as source, and for all four the line diff the panel
|
|
68
|
+
* has always shown is genuinely informative. It is not offered for a pdf, an
|
|
69
|
+
* image or an Office document, where the patch is one line about bytes.
|
|
70
|
+
*/
|
|
71
|
+
const SOURCE_TOGGLE_EXTENSIONS = new Set(['.html', '.htm', '.csv', '.svg']);
|
|
72
|
+
|
|
73
|
+
/** The save's time in full, for the header's tooltip — the commit list's own shape. */
|
|
74
|
+
function formatAbsoluteTime(iso: string): string {
|
|
75
|
+
if (!iso) return '';
|
|
76
|
+
const d = new Date(iso);
|
|
77
|
+
if (Number.isNaN(d.getTime())) return iso;
|
|
78
|
+
return d.toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function extensionOf(filePath: string): string {
|
|
82
|
+
return filePath.slice(filePath.lastIndexOf('.')).toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Tool definitions are excluded from the viewer path on purpose: `.tool`'s
|
|
87
|
+
* renderer is an editing FORM over a JSON file, not a rendering of the file,
|
|
88
|
+
* and its history has always been — and stays — the line diff.
|
|
89
|
+
*/
|
|
90
|
+
function isToolPath(filePath: string): boolean {
|
|
91
|
+
return extensionOf(filePath) === '.tool';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** How this save is shown. */
|
|
95
|
+
type PreviewKind =
|
|
96
|
+
/** The file page's viewer, bound to the save. */
|
|
97
|
+
| 'viewer'
|
|
98
|
+
/** Nothing renders these bytes: a note and the download. */
|
|
99
|
+
| 'no-preview'
|
|
100
|
+
/** Text with no viewer (a `.txt`, a `.json`, a tool): the line diff, as before. */
|
|
101
|
+
| 'source';
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Which of the three bodies this file gets.
|
|
105
|
+
*
|
|
106
|
+
* Derived from the renderer registry rather than kept as a fourth list of
|
|
107
|
+
* extensions, so a viewer added on the file page reaches Version history the
|
|
108
|
+
* same day. The binary question needs the patch as well as the extension:
|
|
109
|
+
* `isBinaryFile` knows the formats the app handles, and git's own "Binary
|
|
110
|
+
* files … differ" marker covers everything else — a `.bin`, a `.exe`, an
|
|
111
|
+
* unknown container — which is what makes "and any other binary" true rather
|
|
112
|
+
* than a list somebody has to keep up to date.
|
|
113
|
+
*/
|
|
114
|
+
function previewKind(filePath: string, patch: string): PreviewKind {
|
|
115
|
+
// The save did not touch this path at all — the file is absent, or unchanged,
|
|
116
|
+
// on both sides (a pure rename elsewhere in the commit, say). There is no
|
|
117
|
+
// version here to show, and a viewer bound to a ref with nothing at it would
|
|
118
|
+
// answer with its read error; `source` is what the panel has always shown
|
|
119
|
+
// for this, and `UnifiedDiffView`'s empty state says it in words.
|
|
120
|
+
if (patch.trim() === '') return 'source';
|
|
121
|
+
if (hasFileViewer(filePath) && !isToolPath(filePath)) return 'viewer';
|
|
122
|
+
if (isBinaryFile(filePath) || /^Binary files .* differ$/m.test(patch)) return 'no-preview';
|
|
123
|
+
return 'source';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* What one selected save looks like once the server has answered, TAGGED with
|
|
128
|
+
* the request it is an answer for.
|
|
129
|
+
*
|
|
130
|
+
* The tag is what replaces a reset inside the effect: switching saves does not
|
|
131
|
+
* clear anything, it simply stops the previous save's answer from being
|
|
132
|
+
* recognised. Clearing in the effect body would cost a cascading render on
|
|
133
|
+
* every selection and, for one render, would show the OLD save's document
|
|
134
|
+
* under the NEW save's header — the same pattern, and the same reason, as the
|
|
135
|
+
* change-request pane's `BranchFilePreview`.
|
|
136
|
+
*/
|
|
137
|
+
interface Answer {
|
|
138
|
+
/** File, save and attempt — everything the load is a function of. */
|
|
139
|
+
key: string;
|
|
140
|
+
loaded: Loaded | null;
|
|
141
|
+
error: string | null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Everything one selected save needs, once the server has answered. */
|
|
145
|
+
interface Loaded {
|
|
146
|
+
/** `git show` for this save — the "Source changes" body, and the delete signal. */
|
|
147
|
+
patch: string;
|
|
148
|
+
/**
|
|
149
|
+
* The save DELETED the file. git spells this in the patch header the same
|
|
150
|
+
* way for a binary and for text (`deleted file mode 100644`), which makes it
|
|
151
|
+
* the one signal that works for every format without a second read of bytes
|
|
152
|
+
* nobody is going to look at.
|
|
153
|
+
*/
|
|
154
|
+
deleted: boolean;
|
|
155
|
+
/** The save's text, for a text-fed viewer only; `null` otherwise. */
|
|
156
|
+
text: string | null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface HistoryVersionPreviewProps {
|
|
160
|
+
/** Workspace-relative path, e.g. `knowledge-base/Docs/Q3-report.pdf`. */
|
|
161
|
+
filePath: string;
|
|
162
|
+
/** The save being shown. */
|
|
163
|
+
commit: CommitAttribution;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function HistoryVersionPreview({ filePath, commit }: HistoryVersionPreviewProps) {
|
|
167
|
+
const { fetchFileDiff, fetchFileAtChange, status } = useGit();
|
|
168
|
+
const { workspaceId } = useWorkspace();
|
|
169
|
+
const ext = extensionOf(filePath);
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Bumped by the pane's own "Try again". A failed load whose file and save
|
|
173
|
+
* are unchanged has nothing else to re-trigger the effect, and a version
|
|
174
|
+
* pane must never be left blank — so the retry is part of the error state
|
|
175
|
+
* rather than something a reader has to find elsewhere.
|
|
176
|
+
*/
|
|
177
|
+
const [attempt, setAttempt] = useState(0);
|
|
178
|
+
const key = `${filePath}::${commit.sha}::${attempt}`;
|
|
179
|
+
const [answer, setAnswer] = useState<Answer | null>(null);
|
|
180
|
+
const { loaded, error } = answer?.key === key ? answer : { loaded: null, error: null };
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The save whose SOURCE view is open, rather than a bare boolean: a boolean
|
|
184
|
+
* would need clearing when the selection changes, and this simply stops
|
|
185
|
+
* matching. Selecting another save therefore lands on Preview.
|
|
186
|
+
*/
|
|
187
|
+
const [sourceForSha, setSourceForSha] = useState<string | null>(null);
|
|
188
|
+
const showSource = sourceForSha === commit.sha;
|
|
189
|
+
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
let cancelled = false;
|
|
192
|
+
(async () => {
|
|
193
|
+
try {
|
|
194
|
+
// The patch is fetched for every format, not only the ones that show
|
|
195
|
+
// it: it is also how "this save deleted the file" and "these bytes are
|
|
196
|
+
// binary" are known, and for a binary git prints one line rather than
|
|
197
|
+
// the content, so it costs nothing worth saving.
|
|
198
|
+
const patch = await fetchFileDiff(filePath, commit.sha);
|
|
199
|
+
if (cancelled) return;
|
|
200
|
+
const deleted = /^deleted file mode /m.test(patch);
|
|
201
|
+
let text: string | null = null;
|
|
202
|
+
if (TEXT_FED_EXTENSIONS.has(extensionOf(filePath))) {
|
|
203
|
+
const sides = await fetchFileAtChange(filePath, commit.sha);
|
|
204
|
+
if (cancelled) return;
|
|
205
|
+
// A deleting save shows the version just BEFORE it — there is
|
|
206
|
+
// nothing at the path after it.
|
|
207
|
+
text = (deleted ? sides.baseline : sides.current) ?? '';
|
|
208
|
+
}
|
|
209
|
+
if (cancelled) return;
|
|
210
|
+
setAnswer({ key, loaded: { patch, deleted, text }, error: null });
|
|
211
|
+
} catch (err) {
|
|
212
|
+
if (!cancelled) setAnswer({ key, loaded: null, error: friendlyGitError(err) });
|
|
213
|
+
}
|
|
214
|
+
})();
|
|
215
|
+
return () => {
|
|
216
|
+
cancelled = true;
|
|
217
|
+
};
|
|
218
|
+
}, [key, filePath, commit.sha, fetchFileDiff, fetchFileAtChange]);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The same download verdict the file page resolves for this file, on today's
|
|
222
|
+
* tree — which is the whole access rule for a past save: anyone who may read
|
|
223
|
+
* (and download) the file now may read (and download) any of its saves, and
|
|
224
|
+
* the rules as they were at that save play no part.
|
|
225
|
+
*/
|
|
226
|
+
const access = useFileAccess(filePath, status?.branch ?? null);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Which side of the save "this version" is: the save's own, or — for the save
|
|
230
|
+
* that DELETED the file — the one before it, which is the only side with any
|
|
231
|
+
* bytes.
|
|
232
|
+
*
|
|
233
|
+
* `after` before the patch has answered, deliberately, and NOT a `null` the
|
|
234
|
+
* ref waits for. Nothing reads a provisional side (the body renders its
|
|
235
|
+
* loading state until `loaded` is non-null, so no viewer is mounted, and the
|
|
236
|
+
* header's download is `pending` until then), but a `null` ref would mean
|
|
237
|
+
* `rawFileUrl` omitted `ref=` and served TODAY's bytes under a past save's
|
|
238
|
+
* name — silently wrong, which is the one answer this whole pane exists to
|
|
239
|
+
* rule out. A provisional side can only ever be wrong LOUDLY: the after side
|
|
240
|
+
* of a deleting save is a 404.
|
|
241
|
+
*/
|
|
242
|
+
const side: 'after' | 'before' = loaded?.deleted ? 'before' : 'after';
|
|
243
|
+
/**
|
|
244
|
+
* What binds every viewer in this pane to the selected save. Memoized so a
|
|
245
|
+
* status poll re-rendering the panel cannot hand the renderers a new object
|
|
246
|
+
* and restart their reads.
|
|
247
|
+
*/
|
|
248
|
+
const fileRef = useMemo<RendererFileRef>(
|
|
249
|
+
() => ({ ref: commit.sha, side }),
|
|
250
|
+
[commit.sha, side],
|
|
251
|
+
);
|
|
252
|
+
const rendererContext = useMemo(
|
|
253
|
+
() => ({ workspaceId, fileRef }),
|
|
254
|
+
[workspaceId, fileRef],
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const retry = useCallback(() => setAttempt((n) => n + 1), []);
|
|
258
|
+
|
|
259
|
+
const who = commit.authorName || commit.authorEmail || 'unknown';
|
|
260
|
+
const fileName = filePath.slice(filePath.lastIndexOf('/') + 1);
|
|
261
|
+
const kind = loaded === null ? null : previewKind(filePath, loaded.patch);
|
|
262
|
+
const canToggleSource = kind === 'viewer' && SOURCE_TOGGLE_EXTENSIONS.has(ext);
|
|
263
|
+
/**
|
|
264
|
+
* The save did not touch this path: nothing to preview AND nothing to
|
|
265
|
+
* download. The empty patch is the whole signal — see {@link previewKind} —
|
|
266
|
+
* and the panel's own answer for it has always been the one sentence and no
|
|
267
|
+
* controls, which is what a save with no version of the file can honestly
|
|
268
|
+
* offer. `logForFile` lists commits that touched the path, so this is the
|
|
269
|
+
* save that turns out not to have (a rename elsewhere in the commit).
|
|
270
|
+
*/
|
|
271
|
+
const untouched = loaded !== null && loaded.patch.trim() === '';
|
|
272
|
+
|
|
273
|
+
const header = (
|
|
274
|
+
<div className="flex flex-wrap items-center gap-2 border-b border-line px-3 py-2 shrink-0">
|
|
275
|
+
<span
|
|
276
|
+
className="flex-1 truncate text-xs text-ink-muted"
|
|
277
|
+
title={formatAbsoluteTime(commit.committedAt)}
|
|
278
|
+
>
|
|
279
|
+
As saved {formatRelativeTime(commit.committedAt)} by {who}
|
|
280
|
+
</span>
|
|
281
|
+
{canToggleSource && (
|
|
282
|
+
<div className="flex items-center gap-1">
|
|
283
|
+
<Button
|
|
284
|
+
variant={showSource ? 'outline' : 'primary'}
|
|
285
|
+
size="tiny"
|
|
286
|
+
onClick={() => setSourceForSha(null)}
|
|
287
|
+
>
|
|
288
|
+
Preview
|
|
289
|
+
</Button>
|
|
290
|
+
<Button
|
|
291
|
+
variant={showSource ? 'primary' : 'outline'}
|
|
292
|
+
size="tiny"
|
|
293
|
+
onClick={() => setSourceForSha(commit.sha)}
|
|
294
|
+
>
|
|
295
|
+
Source changes
|
|
296
|
+
</Button>
|
|
297
|
+
</div>
|
|
298
|
+
)}
|
|
299
|
+
{/* Inside the providers, so it downloads THIS save's bytes and is
|
|
300
|
+
disabled with its reason for a reader who may not download — and
|
|
301
|
+
idle until the patch has said which SIDE of the save "this version"
|
|
302
|
+
means, which for a deleting save is the one before it. Absent
|
|
303
|
+
altogether for a save with no version of this file to hand over. */}
|
|
304
|
+
{!untouched && (
|
|
305
|
+
<DownloadFileButton
|
|
306
|
+
filePath={filePath}
|
|
307
|
+
size="tiny"
|
|
308
|
+
label="Download this version"
|
|
309
|
+
pending={loaded === null}
|
|
310
|
+
/>
|
|
311
|
+
)}
|
|
312
|
+
</div>
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
let body;
|
|
316
|
+
if (error !== null) {
|
|
317
|
+
body = (
|
|
318
|
+
<div className="flex flex-1 flex-col items-center justify-center gap-3 px-6 py-6 text-center">
|
|
319
|
+
<p role="alert" className="flex items-start gap-2 text-xs text-danger">
|
|
320
|
+
<AlertTriangle size={13} className="mt-0.5 shrink-0" />
|
|
321
|
+
<span>{error}</span>
|
|
322
|
+
</p>
|
|
323
|
+
<RetryReadButton onRetry={retry} />
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
} else if (loaded === null) {
|
|
327
|
+
body = (
|
|
328
|
+
<div className="flex items-center gap-2 px-3 py-3 text-xs text-ink-muted">
|
|
329
|
+
<Loader2 size={13} className="animate-spin" />
|
|
330
|
+
Loading this version…
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
} else if (kind === 'source' || showSource) {
|
|
334
|
+
body = (
|
|
335
|
+
<div className="flex-1 overflow-auto">
|
|
336
|
+
<UnifiedDiffView diff={loaded.patch} emptyMessage="No file changes in this save." />
|
|
337
|
+
</div>
|
|
338
|
+
);
|
|
339
|
+
} else if (kind === 'no-preview') {
|
|
340
|
+
body = (
|
|
341
|
+
<div className="flex flex-1 items-center justify-center px-6 py-6 text-center text-xs text-ink-muted">
|
|
342
|
+
No preview for this format.
|
|
343
|
+
</div>
|
|
344
|
+
);
|
|
345
|
+
} else {
|
|
346
|
+
const Renderer = getFileRenderer(filePath);
|
|
347
|
+
const viewer = (
|
|
348
|
+
/* Not a component created during render: `getFileRenderer` returns one
|
|
349
|
+
of a fixed set of module-level components (or a lazy wrapper built
|
|
350
|
+
once at module load), keyed by the path's extension — the same lookup,
|
|
351
|
+
with the same identity, `FileViewer` mounts. */
|
|
352
|
+
/* eslint-disable-next-line react-hooks/static-components */
|
|
353
|
+
<Renderer
|
|
354
|
+
// A byte-reading viewer (pdf, image, docx, deck, workbook, message)
|
|
355
|
+
// ignores these and reads the save through `?ref=`; a text-fed one
|
|
356
|
+
// (html, csv) renders exactly this string, which is the save's own
|
|
357
|
+
// text. `savedContent` matches `content` so the renderer is never
|
|
358
|
+
// dirty — there is nothing here to save.
|
|
359
|
+
content={loaded.text ?? ''}
|
|
360
|
+
savedContent={loaded.text ?? ''}
|
|
361
|
+
filePath={filePath}
|
|
362
|
+
onSave={NO_SAVE}
|
|
363
|
+
readOnly
|
|
364
|
+
/>
|
|
365
|
+
);
|
|
366
|
+
body =
|
|
367
|
+
getRendererLayout(filePath) === 'prose' ? (
|
|
368
|
+
// `READ_PANE`: the viewer's own "Try again" unmounts itself, and this
|
|
369
|
+
// div is what stays mounted around it for focus to land on. See
|
|
370
|
+
// `RetryReadButton`.
|
|
371
|
+
<div {...READ_PANE} className="flex-1 overflow-auto p-3">
|
|
372
|
+
<FilePaneCard file={fileName}>{viewer}</FilePaneCard>
|
|
373
|
+
</div>
|
|
374
|
+
) : (
|
|
375
|
+
// A viewport (pdf, image, workbook, sandboxed page): it owns its own
|
|
376
|
+
// scrolling and needs a DEFINITE height to do it — an `h-full` child
|
|
377
|
+
// of an auto-height column collapses to 0px. The floor keeps a short
|
|
378
|
+
// panel from squeezing a PDF into a sliver.
|
|
379
|
+
<div
|
|
380
|
+
{...READ_PANE}
|
|
381
|
+
data-testid="history-preview-viewport"
|
|
382
|
+
className="min-h-[24rem] flex-1 p-3"
|
|
383
|
+
>
|
|
384
|
+
{viewer}
|
|
385
|
+
</div>
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return (
|
|
390
|
+
<RendererWorkspaceContext.Provider value={rendererContext}>
|
|
391
|
+
<CanDownloadContext.Provider value={access.canDownload}>
|
|
392
|
+
{/* A read region for the whole pane, so the pane's own "Try again" —
|
|
393
|
+
which unmounts itself exactly as a viewer's does — has somewhere to
|
|
394
|
+
hand focus. A viewer's own body marks a nearer one below. */}
|
|
395
|
+
<div {...READ_PANE} className="flex min-h-0 flex-1 flex-col">
|
|
396
|
+
{header}
|
|
397
|
+
{loaded?.deleted && (
|
|
398
|
+
// The note, then the version just before the save beneath it: a
|
|
399
|
+
// reader who selected the save that removed a file wants to see
|
|
400
|
+
// what was removed (decision 4).
|
|
401
|
+
<p className="border-b border-line px-3 py-2 text-xs text-ink shrink-0">
|
|
402
|
+
This save deleted the file.
|
|
403
|
+
</p>
|
|
404
|
+
)}
|
|
405
|
+
{body}
|
|
406
|
+
</div>
|
|
407
|
+
</CanDownloadContext.Provider>
|
|
408
|
+
</RendererWorkspaceContext.Provider>
|
|
409
|
+
);
|
|
410
|
+
}
|