@bevel-software/platform-core-frontend 0.21.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 +140 -0
- package/src/lib/api.ts +49 -0
- package/src/modules/access/__tests__/ManageAccessDialog.request.test.tsx +376 -0
- package/src/modules/access/api.ts +6 -4
- package/src/modules/access/components/AccessRequestControl.tsx +155 -0
- package/src/modules/{library → access}/components/AccessRequestsBanner.tsx +62 -24
- package/src/modules/access/components/ManageAccessDialog.tsx +81 -4
- package/src/modules/access/hooks/useAccessRequests.ts +183 -0
- package/src/modules/access/requests.api.ts +121 -0
- 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 +8 -0
- package/src/modules/auth/services/account.api.ts +26 -3
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.brings-itself-up-to-date.test.tsx +171 -38
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.fork-point.test.tsx +34 -19
- package/src/modules/change-requests/__tests__/ChangeRequestDialog.test.tsx +173 -6
- package/src/modules/change-requests/components/BranchFilePreview.tsx +7 -1
- package/src/modules/change-requests/components/ChangeRequestDialog.tsx +155 -98
- 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/library/__tests__/SkillPage.test.tsx +15 -0
- package/src/modules/library/__tests__/ToolPage.test.tsx +15 -1
- package/src/modules/library/__tests__/title-row-actions.ts +53 -1
- package/src/modules/library/components/PluginJoinRequests.tsx +2 -2
- package/src/modules/library/components/skill-page/SkillPage.tsx +2 -2
- package/src/modules/library/components/tool-page/ToolPage.tsx +11 -1
- package/src/modules/library/hooks/useJoinRequests.ts +26 -101
- package/src/modules/pr/services/pr-merge.api.ts +10 -2
- package/src/modules/settings/components/DeploymentPage.tsx +22 -0
- package/src/modules/settings/components/__tests__/DeploymentPage.test.tsx +72 -4
- package/src/modules/setup/__tests__/setup.first-run.test.tsx +276 -0
- package/src/modules/setup/__tests__/setup.github.test.tsx +416 -0
- package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +275 -0
- package/src/modules/setup/__tests__/setup.repository-change.test.tsx +296 -0
- package/src/modules/setup/__tests__/setup.repository.test.tsx +463 -0
- package/src/modules/setup/__tests__/setup.test.tsx +13 -17
- package/src/modules/setup/components/GitHubRepositoryPanel.tsx +369 -0
- package/src/modules/setup/components/SetupGate.tsx +1 -0
- package/src/modules/setup/components/SetupScreen.tsx +608 -41
- package/src/modules/setup/services/setup.api.ts +171 -3
- package/src/modules/setup/utils/kept-draft.ts +86 -0
- 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/components/SlotBoundary.tsx +51 -0
- package/src/shared/markdown/__tests__/hrefs.test.ts +111 -0
- package/src/shared/markdown/hrefs.ts +80 -4
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import { authFetch } from '../../../lib/api';
|
|
4
|
+
import { ReadOnlyBanner } from '../components/ReadOnlyBanner';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The read-only banner, driven through what the app really does: requests
|
|
8
|
+
* made with `authFetch` against a server that answers. Only `fetch` is
|
|
9
|
+
* replaced, so the path from a refused save to the banner is the real one.
|
|
10
|
+
*/
|
|
11
|
+
const READ_ONLY = 'This workspace has 5 people switched on and room for 3.';
|
|
12
|
+
|
|
13
|
+
/** What the server would answer right now. */
|
|
14
|
+
let writable = true;
|
|
15
|
+
/** Every request for the state, so a test can count the asking. */
|
|
16
|
+
let asked = 0;
|
|
17
|
+
|
|
18
|
+
const json = (status: number, body: unknown) =>
|
|
19
|
+
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
writable = true;
|
|
23
|
+
asked = 0;
|
|
24
|
+
vi.stubGlobal(
|
|
25
|
+
'fetch',
|
|
26
|
+
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
27
|
+
const url = String(input);
|
|
28
|
+
if (url === '/api/write-access') {
|
|
29
|
+
asked += 1;
|
|
30
|
+
return json(200, writable ? { writable: true } : { writable: false, message: READ_ONLY });
|
|
31
|
+
}
|
|
32
|
+
// The account routes stay open while the deployment is read-only.
|
|
33
|
+
if (url.startsWith('/api/admin/accounts')) return json(200, {});
|
|
34
|
+
if ((init?.method ?? 'GET') === 'GET') return json(200, {});
|
|
35
|
+
return writable ? json(200, {}) : json(403, { error: READ_ONLY, code: 'workspace_read_only' });
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
vi.unstubAllGlobals();
|
|
42
|
+
vi.useRealTimers();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('ReadOnlyBanner', () => {
|
|
46
|
+
it('says so, in the host’s words, on a deployment that is read-only when the app opens', async () => {
|
|
47
|
+
writable = false;
|
|
48
|
+
render(<ReadOnlyBanner />);
|
|
49
|
+
expect(await screen.findByRole('status')).toHaveTextContent(READ_ONLY);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('shows nothing on a writable deployment, and asks once, however long the tab stays open', async () => {
|
|
53
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
54
|
+
render(<ReadOnlyBanner />);
|
|
55
|
+
await waitFor(() => expect(asked).toBe(1));
|
|
56
|
+
await vi.advanceTimersByTimeAsync(10 * 60_000);
|
|
57
|
+
expect(asked).toBe(1);
|
|
58
|
+
expect(screen.queryByRole('status')).toBeNull();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('learns the deployment went read-only from the first change that is refused', async () => {
|
|
62
|
+
render(<ReadOnlyBanner />);
|
|
63
|
+
await waitFor(() => expect(asked).toBe(1));
|
|
64
|
+
expect(screen.queryByRole('status')).toBeNull();
|
|
65
|
+
|
|
66
|
+
writable = false;
|
|
67
|
+
const refused = await authFetch('/api/workspace/w1/file', { method: 'PUT', body: '{}' });
|
|
68
|
+
// The caller still has the answer to itself.
|
|
69
|
+
expect(await refused.json()).toMatchObject({ code: 'workspace_read_only' });
|
|
70
|
+
expect(await screen.findByRole('status')).toHaveTextContent(READ_ONLY);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('is not moved by a refusal that is about something else', async () => {
|
|
74
|
+
render(<ReadOnlyBanner />);
|
|
75
|
+
await waitFor(() => expect(asked).toBe(1));
|
|
76
|
+
vi.mocked(fetch).mockResolvedValueOnce(json(403, { error: 'You cannot edit this file.' }));
|
|
77
|
+
await authFetch('/api/workspace/w1/file', { method: 'PUT', body: '{}' });
|
|
78
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
79
|
+
expect(asked).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('goes away once an admin’s change ends the state', async () => {
|
|
83
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
84
|
+
writable = false;
|
|
85
|
+
render(<ReadOnlyBanner />);
|
|
86
|
+
await screen.findByRole('status');
|
|
87
|
+
// Some while later, so the check is made at once and not spaced.
|
|
88
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
89
|
+
|
|
90
|
+
// The admin switches an account off: the request succeeds, and the count is back under the plan.
|
|
91
|
+
writable = true;
|
|
92
|
+
await authFetch('/api/admin/accounts/u1/deactivate', { method: 'POST' });
|
|
93
|
+
await waitFor(() => expect(screen.queryByRole('status')).toBeNull());
|
|
94
|
+
expect(asked).toBe(2);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not ask again over changes that go through while nothing is wrong', async () => {
|
|
98
|
+
render(<ReadOnlyBanner />);
|
|
99
|
+
await waitFor(() => expect(asked).toBe(1));
|
|
100
|
+
await authFetch('/api/workspace/w1/file', { method: 'PUT', body: '{}' });
|
|
101
|
+
await authFetch('/api/workspace/w1/file', { method: 'PUT', body: '{}' });
|
|
102
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
103
|
+
expect(asked).toBe(1);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('asks again when the tab comes back into view, at most once a minute, and never as it leaves', async () => {
|
|
107
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
108
|
+
let visibility: DocumentVisibilityState = 'visible';
|
|
109
|
+
Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => visibility });
|
|
110
|
+
const turn = (to: DocumentVisibilityState) => {
|
|
111
|
+
visibility = to;
|
|
112
|
+
document.dispatchEvent(new Event('visibilitychange'));
|
|
113
|
+
};
|
|
114
|
+
try {
|
|
115
|
+
render(<ReadOnlyBanner />);
|
|
116
|
+
await waitFor(() => expect(asked).toBe(1));
|
|
117
|
+
|
|
118
|
+
// Back into view too soon after the last check.
|
|
119
|
+
turn('hidden');
|
|
120
|
+
turn('visible');
|
|
121
|
+
expect(asked).toBe(1);
|
|
122
|
+
|
|
123
|
+
await vi.advanceTimersByTimeAsync(61_000);
|
|
124
|
+
writable = false;
|
|
125
|
+
// Leaving the tab asks nothing, however long it has been.
|
|
126
|
+
turn('hidden');
|
|
127
|
+
expect(asked).toBe(1);
|
|
128
|
+
turn('visible');
|
|
129
|
+
expect(await screen.findByRole('status')).toHaveTextContent(READ_ONLY);
|
|
130
|
+
expect(asked).toBe(2);
|
|
131
|
+
} finally {
|
|
132
|
+
Reflect.deleteProperty(document, 'visibilityState');
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A permission lookup or a heartbeat is a POST that goes through, and the
|
|
138
|
+
* app makes them all the time. They must not turn into a check each.
|
|
139
|
+
*/
|
|
140
|
+
it('spaces the checks that requests going through set off, without losing the last one', async () => {
|
|
141
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
142
|
+
writable = false;
|
|
143
|
+
render(<ReadOnlyBanner />);
|
|
144
|
+
await screen.findByRole('status');
|
|
145
|
+
expect(asked).toBe(1);
|
|
146
|
+
|
|
147
|
+
for (let i = 0; i < 5; i += 1) await authFetch('/api/admin/accounts/lookup', { method: 'POST' });
|
|
148
|
+
expect(asked).toBe(1);
|
|
149
|
+
|
|
150
|
+
// The change that ends the state arrives inside the spacing, and is still asked about.
|
|
151
|
+
writable = true;
|
|
152
|
+
await authFetch('/api/admin/accounts/u1/deactivate', { method: 'POST' });
|
|
153
|
+
await vi.advanceTimersByTimeAsync(5_000);
|
|
154
|
+
await waitFor(() => expect(screen.queryByRole('status')).toBeNull());
|
|
155
|
+
expect(asked).toBe(2);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { Lock } from 'lucide-react';
|
|
3
|
+
import { authFetch, WRITE_ACCEPTED_EVENT, WRITE_REFUSED_EVENT } from '../../../lib/api';
|
|
4
|
+
import { useLatestRef } from '../../../shared/components/useLatestRef';
|
|
5
|
+
|
|
6
|
+
/** The least time between two checks made because the tab came back into view. */
|
|
7
|
+
const RETURN_CHECK_MS = 60_000;
|
|
8
|
+
|
|
9
|
+
/** The least time between two checks made because a request went through while the banner is up. */
|
|
10
|
+
const ACCEPTED_CHECK_MS = 5_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* App-wide notice that the deployment is read-only right now (`GET
|
|
14
|
+
* /api/write-access`): everyone can still sign in and read, but every change
|
|
15
|
+
* is refused until the cause is fixed — on a host that sells seats, more
|
|
16
|
+
* people are switched on than the plan allows. Said once, here, with the
|
|
17
|
+
* host's own words, instead of letting each save fail on its own.
|
|
18
|
+
*
|
|
19
|
+
* Renders nothing while the deployment is writable, which on a core
|
|
20
|
+
* deployment is always.
|
|
21
|
+
*
|
|
22
|
+
* ASKED ON ACTIVITY, NEVER ON A TIMER. Once when the app opens; when a
|
|
23
|
+
* change is refused as read-only, which is how an open tab learns the state
|
|
24
|
+
* began; when the tab comes back into view; and, while the banner is up,
|
|
25
|
+
* when a change goes through, which is how it learns the state ended (an
|
|
26
|
+
* admin switching accounts off). An idle tab on a deployment that can never
|
|
27
|
+
* be read-only asks once and never again.
|
|
28
|
+
*/
|
|
29
|
+
export function ReadOnlyBanner() {
|
|
30
|
+
const [message, setMessage] = useState<string | null>(null);
|
|
31
|
+
const showing = useLatestRef(message !== null);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
let cancelled = false;
|
|
35
|
+
// Only the latest check may decide: a slow answer to an earlier one must
|
|
36
|
+
// not overwrite what a newer one said.
|
|
37
|
+
let latest = 0;
|
|
38
|
+
let lastAsked = 0;
|
|
39
|
+
const check = () => {
|
|
40
|
+
const mine = ++latest;
|
|
41
|
+
lastAsked = Date.now();
|
|
42
|
+
authFetch('/api/write-access')
|
|
43
|
+
.then((res) => (res.ok ? res.json() : { writable: true }))
|
|
44
|
+
.then((body: { writable?: boolean; message?: string }) => {
|
|
45
|
+
if (cancelled || mine !== latest) return;
|
|
46
|
+
setMessage(body.writable === false ? (body.message ?? 'This workspace is read-only.') : null);
|
|
47
|
+
})
|
|
48
|
+
// An unanswered check says nothing about the workspace; keep what we knew.
|
|
49
|
+
.catch(() => undefined);
|
|
50
|
+
};
|
|
51
|
+
const onReturn = () => {
|
|
52
|
+
if (document.visibilityState === 'visible' && Date.now() - lastAsked >= RETURN_CHECK_MS) check();
|
|
53
|
+
};
|
|
54
|
+
// Requests that only ask something are POSTs too (a permission lookup,
|
|
55
|
+
// a heartbeat), and the app makes them constantly. So the checks they
|
|
56
|
+
// set off are spaced: one now if none was made lately, otherwise ONE
|
|
57
|
+
// later, at the end of the spacing, so the change that did end the state
|
|
58
|
+
// is never the one that went unasked about.
|
|
59
|
+
let pending: number | null = null;
|
|
60
|
+
const onAccepted = () => {
|
|
61
|
+
if (!showing.current || pending !== null) return;
|
|
62
|
+
const wait = ACCEPTED_CHECK_MS - (Date.now() - lastAsked);
|
|
63
|
+
if (wait <= 0) return check();
|
|
64
|
+
pending = window.setTimeout(() => {
|
|
65
|
+
pending = null;
|
|
66
|
+
if (showing.current) check();
|
|
67
|
+
}, wait);
|
|
68
|
+
};
|
|
69
|
+
check();
|
|
70
|
+
window.addEventListener(WRITE_REFUSED_EVENT, check);
|
|
71
|
+
window.addEventListener(WRITE_ACCEPTED_EVENT, onAccepted);
|
|
72
|
+
document.addEventListener('visibilitychange', onReturn);
|
|
73
|
+
return () => {
|
|
74
|
+
cancelled = true;
|
|
75
|
+
if (pending !== null) window.clearTimeout(pending);
|
|
76
|
+
window.removeEventListener(WRITE_REFUSED_EVENT, check);
|
|
77
|
+
window.removeEventListener(WRITE_ACCEPTED_EVENT, onAccepted);
|
|
78
|
+
document.removeEventListener('visibilitychange', onReturn);
|
|
79
|
+
};
|
|
80
|
+
}, [showing]);
|
|
81
|
+
// `showing` is a ref with a stable identity: the effect runs once.
|
|
82
|
+
|
|
83
|
+
if (message === null) return null;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div
|
|
87
|
+
role="status"
|
|
88
|
+
className="flex items-center gap-2 px-4 py-2 border-b border-line text-sm shrink-0 bg-wait-soft text-ink"
|
|
89
|
+
>
|
|
90
|
+
<Lock size={16} className="shrink-0" />
|
|
91
|
+
<span className="flex-1">
|
|
92
|
+
<span className="font-semibold">Read-only.</span> {message}
|
|
93
|
+
</span>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|