@bevel-software/platform-core-frontend 0.11.2 → 0.12.1
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/THIRD-PARTY-NOTICES.md +670 -395
- package/package.json +6 -2
- package/src/lib/__tests__/api.test.ts +95 -0
- package/src/lib/api.ts +30 -2
- package/src/lib/clipboard.ts +25 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +47 -0
- package/src/modules/access/hooks/useFileAccess.ts +23 -5
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +181 -0
- package/src/modules/git/components/GitSyncFailedBanner.tsx +106 -0
- package/src/modules/library/utils/clipboard.ts +4 -20
- package/src/modules/secrets-vault/components/SecretsPage.tsx +313 -313
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +82 -82
- package/src/modules/workspace/components/FileExplorer.tsx +6 -14
- package/src/modules/workspace/components/FileViewer.tsx +20 -4
- package/src/modules/workspace/components/KbPageHeader.tsx +1 -1
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +3 -1
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +17 -0
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +15 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -2
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +88 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +309 -0
- package/src/modules/workspace/components/renderers/LegacyOfficeRenderer.tsx +60 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +261 -33
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +184 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +195 -45
- package/src/modules/workspace/components/renderers/__tests__/DownloadFileButton.test.tsx +48 -0
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.test.tsx +176 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +214 -76
- package/src/modules/workspace/components/renderers/__tests__/PptxRenderer.test.tsx +125 -0
- package/src/modules/workspace/components/renderers/__tests__/XlsxRenderer.test.tsx +245 -0
- package/src/modules/workspace/components/renderers/__tests__/emailBody.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/emailMessage.test.ts +404 -0
- package/src/modules/workspace/components/renderers/__tests__/pptxOutline.test.ts +448 -0
- package/src/modules/workspace/components/renderers/__tests__/readBodyCapped.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/rendererLayout.test.ts +73 -7
- package/src/modules/workspace/components/renderers/__tests__/rendererSelection.test.tsx +107 -0
- package/src/modules/workspace/components/renderers/downloadFile.ts +33 -0
- package/src/modules/workspace/components/renderers/emailBody.ts +168 -0
- package/src/modules/workspace/components/renderers/emailMessage.ts +223 -0
- package/src/modules/workspace/components/renderers/emlMessage.ts +91 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +9 -2
- package/src/modules/workspace/components/renderers/index.ts +82 -6
- package/src/modules/workspace/components/renderers/lazyRenderer.tsx +9 -2
- package/src/modules/workspace/components/renderers/msgMessage.ts +219 -0
- package/src/modules/workspace/components/renderers/pptxOutline.ts +317 -0
- package/src/modules/workspace/components/renderers/readBodyCapped.ts +97 -0
- package/src/modules/workspace/components/renderers/xmlReading.ts +0 -0
- package/src/url-suffix-imports.d.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bevel-software/platform-core-frontend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "Open-source core UI of the Bevel platform (raw TS/TSX source): workspace explorer/viewer, branches & change requests, Library, secrets, tools, access control.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -28,9 +28,13 @@
|
|
|
28
28
|
"@tailwindcss/typography": "^0.5.19",
|
|
29
29
|
"@utcp/sdk": "^1.1.1",
|
|
30
30
|
"clsx": "^2.1.1",
|
|
31
|
+
"htmlparser2": "^12.0.0",
|
|
32
|
+
"jszip": "^3.10.1",
|
|
31
33
|
"lucide-react": "^1.7.0",
|
|
32
34
|
"mammoth": "^1.12.0",
|
|
33
35
|
"mermaid": "^11.16.1",
|
|
36
|
+
"pdfjs-dist": "^5.4.624",
|
|
37
|
+
"postal-mime": "^3.0.0",
|
|
34
38
|
"react-markdown": "^10.1.0",
|
|
35
39
|
"react-resizable-panels": "^4.10.0",
|
|
36
40
|
"rehype-raw": "^7.0.0",
|
|
@@ -41,7 +45,7 @@
|
|
|
41
45
|
"tailwind-merge": "^3.5.0",
|
|
42
46
|
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz",
|
|
43
47
|
"yaml": "^2.9.0",
|
|
44
|
-
"@bevel-software/platform-shared": "0.
|
|
48
|
+
"@bevel-software/platform-shared": "0.12.1"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
47
51
|
"@testing-library/jest-dom": "^6.9.1",
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { authFetch, API_UNREACHABLE_EVENT } from '../api';
|
|
4
|
+
|
|
5
|
+
/** Counts API_UNREACHABLE_EVENT dispatches for the span of one test. */
|
|
6
|
+
function watchUnreachable(): { seen: () => number; stop: () => void } {
|
|
7
|
+
let count = 0;
|
|
8
|
+
const onEvent = (): void => {
|
|
9
|
+
count += 1;
|
|
10
|
+
};
|
|
11
|
+
window.addEventListener(API_UNREACHABLE_EVENT, onEvent);
|
|
12
|
+
return {
|
|
13
|
+
seen: () => count,
|
|
14
|
+
stop: () => window.removeEventListener(API_UNREACHABLE_EVENT, onEvent),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The rejection `fetch` produces when its signal is aborted. */
|
|
19
|
+
function abortError(): Error {
|
|
20
|
+
return new DOMException('The operation was aborted.', 'AbortError') as unknown as Error;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe('authFetch transport-failure signalling', () => {
|
|
24
|
+
let watcher: ReturnType<typeof watchUnreachable>;
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
vi.restoreAllMocks();
|
|
28
|
+
watcher = watchUnreachable();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
watcher.stop();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('does NOT signal unreachable when the caller aborted its own request', async () => {
|
|
36
|
+
// The binary viewers abort in flight on unmount and on every file switch.
|
|
37
|
+
// Treating that as backend downtime probes /api/health and can raise the
|
|
38
|
+
// maintenance overlay during ordinary navigation.
|
|
39
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce(abortError());
|
|
40
|
+
|
|
41
|
+
await expect(authFetch('/api/workspace/file')).rejects.toMatchObject({ name: 'AbortError' });
|
|
42
|
+
expect(watcher.seen()).toBe(0);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('does NOT signal unreachable when the abort carried a custom reason', async () => {
|
|
46
|
+
// `abort(reason)` rejects the fetch with THAT value rather than an
|
|
47
|
+
// AbortError, so the rejection alone cannot identify the abort.
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
controller.abort('viewer closed');
|
|
50
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce('viewer closed');
|
|
51
|
+
|
|
52
|
+
await expect(
|
|
53
|
+
authFetch('/api/workspace/file', { signal: controller.signal }),
|
|
54
|
+
).rejects.toBe('viewer closed');
|
|
55
|
+
expect(watcher.seen()).toBe(0);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('does NOT signal unreachable when the aborted signal rode on a Request', async () => {
|
|
59
|
+
const controller = new AbortController();
|
|
60
|
+
controller.abort('viewer closed');
|
|
61
|
+
const request = { signal: controller.signal } as unknown as Request;
|
|
62
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce('viewer closed');
|
|
63
|
+
|
|
64
|
+
await expect(authFetch(request)).rejects.toBe('viewer closed');
|
|
65
|
+
expect(watcher.seen()).toBe(0);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('still signals unreachable when init overrides a Request that was already aborted', async () => {
|
|
69
|
+
// `fetch` uses init's signal and ignores the Request's when both are
|
|
70
|
+
// given, so a stale aborted signal on the Request must not mask a real
|
|
71
|
+
// transport failure on the live one.
|
|
72
|
+
const stale = new AbortController();
|
|
73
|
+
stale.abort();
|
|
74
|
+
const live = new AbortController();
|
|
75
|
+
const request = { signal: stale.signal } as unknown as Request;
|
|
76
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
|
77
|
+
|
|
78
|
+
await expect(authFetch(request, { signal: live.signal })).rejects.toThrow('Failed to fetch');
|
|
79
|
+
expect(watcher.seen()).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('still signals unreachable when the connection genuinely fails', async () => {
|
|
83
|
+
vi.spyOn(globalThis, 'fetch').mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
|
84
|
+
|
|
85
|
+
await expect(authFetch('/api/workspace/file')).rejects.toThrow('Failed to fetch');
|
|
86
|
+
expect(watcher.seen()).toBe(1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('still signals unreachable when the proxy answers for a dead upstream', async () => {
|
|
90
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce({ status: 503 } as Response);
|
|
91
|
+
|
|
92
|
+
await authFetch('/api/workspace/file');
|
|
93
|
+
expect(watcher.seen()).toBe(1);
|
|
94
|
+
});
|
|
95
|
+
});
|
package/src/lib/api.ts
CHANGED
|
@@ -6,7 +6,8 @@ const TOKEN_KEY = 'bevel_token';
|
|
|
6
6
|
* the reverse proxy answered for a dead upstream (502/503/504). The
|
|
7
7
|
* MaintenanceOverlay listens for this, confirms via /api/health, and takes
|
|
8
8
|
* over the screen during a redeploy. Application-level errors (4xx, 500)
|
|
9
|
-
* are NOT signalled — those mean the backend is up and responding.
|
|
9
|
+
* are NOT signalled — those mean the backend is up and responding. Neither is
|
|
10
|
+
* an aborted request, which says nothing about the backend at all.
|
|
10
11
|
*/
|
|
11
12
|
export const API_UNREACHABLE_EVENT = 'bevel:api-unreachable';
|
|
12
13
|
|
|
@@ -16,6 +17,29 @@ function signalUnreachable(): void {
|
|
|
16
17
|
window.dispatchEvent(new Event(API_UNREACHABLE_EVENT));
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Whether a rejection is a caller aborting its own request rather than a
|
|
22
|
+
* transport failure.
|
|
23
|
+
*
|
|
24
|
+
* The signal is asked first because it is the only reliable witness:
|
|
25
|
+
* `abort(reason)` rejects the fetch with THAT reason, which can be any value
|
|
26
|
+
* and carries no marking of its own. Only the signal the fetch actually ran on
|
|
27
|
+
* is asked — `init`'s wins where it has one, and a `Request` input's is used
|
|
28
|
+
* only otherwise — so a stale signal on a Request that `init` overrode cannot
|
|
29
|
+
* mask a real transport failure on the live one.
|
|
30
|
+
*
|
|
31
|
+
* The rejection is consulted as a fallback, for an abort whose signal never
|
|
32
|
+
* reached us; there it is matched by `name` rather than `instanceof
|
|
33
|
+
* DOMException`, which does not hold across realms.
|
|
34
|
+
*/
|
|
35
|
+
function wasAborted(input: RequestInfo | URL, init: RequestInit | undefined, err: unknown): boolean {
|
|
36
|
+
const requestSignal =
|
|
37
|
+
typeof input === 'object' && 'signal' in input ? (input as Request).signal : undefined;
|
|
38
|
+
const signal = init?.signal ?? requestSignal;
|
|
39
|
+
if (signal?.aborted) return true;
|
|
40
|
+
return typeof err === 'object' && err !== null && (err as { name?: unknown }).name === 'AbortError';
|
|
41
|
+
}
|
|
42
|
+
|
|
19
43
|
export function getToken(): string | null {
|
|
20
44
|
return localStorage.getItem(TOKEN_KEY);
|
|
21
45
|
}
|
|
@@ -41,7 +65,11 @@ export async function authFetch(input: RequestInfo | URL, init?: RequestInit): P
|
|
|
41
65
|
try {
|
|
42
66
|
response = await fetch(input, { ...init, headers });
|
|
43
67
|
} catch (err) {
|
|
44
|
-
|
|
68
|
+
// An abort is the caller's own doing, not the backend going away: the
|
|
69
|
+
// binary viewers abort in flight on unmount and on every file switch.
|
|
70
|
+
// Signalling here would probe /api/health on ordinary navigation and
|
|
71
|
+
// could raise the maintenance overlay when a probe transiently fails.
|
|
72
|
+
if (!wasAborted(input, init, err)) signalUnreachable();
|
|
45
73
|
throw err;
|
|
46
74
|
}
|
|
47
75
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copy text, and say whether it landed.
|
|
3
|
+
*
|
|
4
|
+
* `navigator.clipboard` is undefined outside a secure context (plain-HTTP
|
|
5
|
+
* staging, some embedded webviews), and `writeText` rejects when the document
|
|
6
|
+
* isn't focused. Both are ordinary conditions, not exceptions — a copy button
|
|
7
|
+
* hands over something the user can also select by hand, so a failure has a
|
|
8
|
+
* real answer and must never surface as success.
|
|
9
|
+
*
|
|
10
|
+
* Lives in `lib/` because more than one module needs it: the Library's copy
|
|
11
|
+
* buttons and the email viewer's link list.
|
|
12
|
+
*/
|
|
13
|
+
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
14
|
+
// `navigator` itself is undefined off the browser (SSR, a test runner with
|
|
15
|
+
// no DOM): reading through it would THROW rather than answer false, which is
|
|
16
|
+
// the one thing this function promises not to do.
|
|
17
|
+
const clipboard = typeof navigator === 'undefined' ? undefined : navigator.clipboard;
|
|
18
|
+
if (!clipboard?.writeText) return false;
|
|
19
|
+
try {
|
|
20
|
+
await clipboard.writeText(text);
|
|
21
|
+
return true;
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -218,6 +218,53 @@ describe('useFileAccess', () => {
|
|
|
218
218
|
expect(apiMock.fetchFileAccess).not.toHaveBeenCalled();
|
|
219
219
|
});
|
|
220
220
|
|
|
221
|
+
// Switching files must not leak the PREVIOUS file's verdicts into the new
|
|
222
|
+
// lookup's in-flight window: both canWrite and canDownload reset to null
|
|
223
|
+
// (optimistic) the moment the new request starts.
|
|
224
|
+
it('resets canWrite/canDownload to null while a new file lookup is in flight', async () => {
|
|
225
|
+
apiMock.fetchFileAccess.mockResolvedValueOnce({
|
|
226
|
+
canWrite: false,
|
|
227
|
+
canDownload: false,
|
|
228
|
+
eligible: { roles: ['Admin'], users: [] },
|
|
229
|
+
owners: { roles: [], users: [] },
|
|
230
|
+
});
|
|
231
|
+
// The second file's request stays pending until we resolve it.
|
|
232
|
+
let resolveSecond!: (v: unknown) => void;
|
|
233
|
+
apiMock.fetchFileAccess.mockImplementationOnce(
|
|
234
|
+
() =>
|
|
235
|
+
new Promise((resolve) => {
|
|
236
|
+
resolveSecond = resolve;
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
const { result, rerender } = renderHook(
|
|
241
|
+
({ path }: { path: string }) => useFileAccess(path, PROTECTED),
|
|
242
|
+
{
|
|
243
|
+
wrapper: withWorkspace(makeWorkspace()),
|
|
244
|
+
initialProps: { path: 'knowledge-base/Knowledge/First.md' },
|
|
245
|
+
},
|
|
246
|
+
);
|
|
247
|
+
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
248
|
+
expect(result.current.canWrite).toBe(false);
|
|
249
|
+
expect(result.current.canDownload).toBe(false);
|
|
250
|
+
|
|
251
|
+
rerender({ path: 'knowledge-base/Knowledge/Second.md' });
|
|
252
|
+
// In flight for the SECOND file: the first file's hard "no" must not show.
|
|
253
|
+
expect(result.current.loading).toBe(true);
|
|
254
|
+
expect(result.current.canWrite).toBeNull();
|
|
255
|
+
expect(result.current.canDownload).toBeNull();
|
|
256
|
+
|
|
257
|
+
resolveSecond({
|
|
258
|
+
canWrite: true,
|
|
259
|
+
canDownload: true,
|
|
260
|
+
eligible: { roles: [], users: [] },
|
|
261
|
+
owners: { roles: [], users: [] },
|
|
262
|
+
});
|
|
263
|
+
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
264
|
+
expect(result.current.canWrite).toBe(true);
|
|
265
|
+
expect(result.current.canDownload).toBe(true);
|
|
266
|
+
});
|
|
267
|
+
|
|
221
268
|
// Branch still loading: hold canWrite=null so the caller stays optimistic
|
|
222
269
|
// (editor editable) until we know which gate applies. Don't fire the API
|
|
223
270
|
// yet because we'd be guessing.
|
|
@@ -11,6 +11,8 @@ export interface FileAccessState {
|
|
|
11
11
|
* — the backend is the authoritative gate at commit time anyway.
|
|
12
12
|
*/
|
|
13
13
|
canWrite: boolean | null;
|
|
14
|
+
/** Same contract as `canWrite`, for the per-path `download:` verb. */
|
|
15
|
+
canDownload: boolean | null;
|
|
14
16
|
eligible: AccessEligible;
|
|
15
17
|
/**
|
|
16
18
|
* The node's owners — who to contact about it. Populated only when the
|
|
@@ -62,6 +64,7 @@ export function useFileAccess(
|
|
|
62
64
|
const { workspaceId, kbDirName } = useWorkspace();
|
|
63
65
|
const [state, setState] = useState<FileAccessState>({
|
|
64
66
|
canWrite: null,
|
|
67
|
+
canDownload: null,
|
|
65
68
|
eligible: EMPTY_ELIGIBLE,
|
|
66
69
|
owners: EMPTY_ELIGIBLE,
|
|
67
70
|
loading: false,
|
|
@@ -70,7 +73,7 @@ export function useFileAccess(
|
|
|
70
73
|
|
|
71
74
|
useEffect(() => {
|
|
72
75
|
if (!workspacePath || !kbDirName || !workspaceId) {
|
|
73
|
-
setState({ canWrite: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
76
|
+
setState({ canWrite: null, canDownload: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
74
77
|
return;
|
|
75
78
|
}
|
|
76
79
|
// Files outside the KB repo dir are the user's own workspace — never
|
|
@@ -78,7 +81,12 @@ export function useFileAccess(
|
|
|
78
81
|
// access tree can't lock users out of their own scratch files.
|
|
79
82
|
const prefix = `${kbDirName}/`;
|
|
80
83
|
if (!workspacePath.startsWith(prefix)) {
|
|
81
|
-
|
|
84
|
+
// `canDownload: null` — NOT true. The write short-circuits are this
|
|
85
|
+
// hook's own policy, and they match the backend's write gates; the raw
|
|
86
|
+
// endpoint gates EVERY download regardless of branch or path, so
|
|
87
|
+
// claiming permission here would promise something it can still refuse.
|
|
88
|
+
// Null keeps the button clickable and the backend authoritative.
|
|
89
|
+
setState({ canWrite: true, canDownload: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
82
90
|
return;
|
|
83
91
|
}
|
|
84
92
|
// Drafts are free-for-all (mirrors the backend's branch-protected gates
|
|
@@ -87,16 +95,24 @@ export function useFileAccess(
|
|
|
87
95
|
// draft. `branch === null` is the bootstrap window before status loads;
|
|
88
96
|
// return canWrite=null so callers stay optimistic until we know.
|
|
89
97
|
if (branch === null) {
|
|
90
|
-
setState({ canWrite: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
98
|
+
setState({ canWrite: null, canDownload: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
91
99
|
return;
|
|
92
100
|
}
|
|
93
101
|
if (!isProtectedBranch(branch)) {
|
|
94
|
-
|
|
102
|
+
// `canDownload: null` — NOT true. The write short-circuits are this
|
|
103
|
+
// hook's own policy, and they match the backend's write gates; the raw
|
|
104
|
+
// endpoint gates EVERY download regardless of branch or path, so
|
|
105
|
+
// claiming permission here would promise something it can still refuse.
|
|
106
|
+
// Null keeps the button clickable and the backend authoritative.
|
|
107
|
+
setState({ canWrite: true, canDownload: null, eligible: EMPTY_ELIGIBLE, owners: EMPTY_ELIGIBLE, loading: false, error: null });
|
|
95
108
|
return;
|
|
96
109
|
}
|
|
97
110
|
|
|
98
111
|
let cancelled = false;
|
|
99
|
-
|
|
112
|
+
// Reset the verdicts to null (= optimistic/unknown) for the NEW lookup —
|
|
113
|
+
// carrying the previous file's canWrite/canDownload across a file switch
|
|
114
|
+
// would briefly show the old file's permissions on the new file.
|
|
115
|
+
setState((s) => ({ ...s, canWrite: null, canDownload: null, loading: true, error: null }));
|
|
100
116
|
// Strip kbDirName/ so the backend resolver receives the repo-relative
|
|
101
117
|
// path it expects (`Knowledge/Foo.md`, not `knowledge-base/…`).
|
|
102
118
|
const repoRelative = workspacePath.slice(prefix.length);
|
|
@@ -105,6 +121,7 @@ export function useFileAccess(
|
|
|
105
121
|
if (cancelled) return;
|
|
106
122
|
setState({
|
|
107
123
|
canWrite: res.canWrite,
|
|
124
|
+
canDownload: res.canDownload,
|
|
108
125
|
eligible: res.eligible,
|
|
109
126
|
owners: res.owners,
|
|
110
127
|
loading: false,
|
|
@@ -119,6 +136,7 @@ export function useFileAccess(
|
|
|
119
136
|
// truly don't have access.
|
|
120
137
|
setState({
|
|
121
138
|
canWrite: true,
|
|
139
|
+
canDownload: true,
|
|
122
140
|
eligible: EMPTY_ELIGIBLE,
|
|
123
141
|
owners: EMPTY_ELIGIBLE,
|
|
124
142
|
loading: false,
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { act, render, screen } from '@testing-library/react';
|
|
3
|
+
import { GitSyncFailedBanner } from '../components/GitSyncFailedBanner';
|
|
4
|
+
import {
|
|
5
|
+
EventBusContext,
|
|
6
|
+
type EventBusContextValue,
|
|
7
|
+
} from '../../workflow/state/event-bus.context';
|
|
8
|
+
import {
|
|
9
|
+
WorkspaceContext,
|
|
10
|
+
type WorkspaceContextValue,
|
|
11
|
+
} from '../../workspace/state/workspace.context';
|
|
12
|
+
import { makeWorkspaceFixture } from '../../workspace/__tests__/testFixtures';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A minimal stand-in for the SSE bus: keeps the handlers the component
|
|
16
|
+
* registers and lets a test push events through them, so these assertions
|
|
17
|
+
* drive the banner exactly the way the real backend does — by event, not by
|
|
18
|
+
* poking state.
|
|
19
|
+
*/
|
|
20
|
+
function makeBus() {
|
|
21
|
+
// Handlers are stored untyped: the test emits plain event-shaped objects and
|
|
22
|
+
// the component's own subscription types are what's under test, not the bus.
|
|
23
|
+
const handlers = new Map<string, Set<(e: unknown) => void>>();
|
|
24
|
+
const ctx = {
|
|
25
|
+
subscribe(kind: string, handler: (e: unknown) => void) {
|
|
26
|
+
const set = handlers.get(kind) ?? new Set<(e: unknown) => void>();
|
|
27
|
+
set.add(handler);
|
|
28
|
+
handlers.set(kind, set);
|
|
29
|
+
return () => set.delete(handler);
|
|
30
|
+
},
|
|
31
|
+
} as unknown as EventBusContextValue;
|
|
32
|
+
return {
|
|
33
|
+
ctx,
|
|
34
|
+
emit(event: { kind: string; workspaceId: string; branch: string; reason?: string }) {
|
|
35
|
+
act(() => {
|
|
36
|
+
for (const h of handlers.get(event.kind) ?? []) h(event);
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function renderBanner(workspace: Partial<WorkspaceContextValue> = {}) {
|
|
43
|
+
const bus = makeBus();
|
|
44
|
+
const view = render(
|
|
45
|
+
<EventBusContext.Provider value={bus.ctx}>
|
|
46
|
+
<WorkspaceContext.Provider value={makeWorkspaceFixture(workspace)}>
|
|
47
|
+
<GitSyncFailedBanner />
|
|
48
|
+
</WorkspaceContext.Provider>
|
|
49
|
+
</EventBusContext.Provider>,
|
|
50
|
+
);
|
|
51
|
+
const rerender = (next: Partial<WorkspaceContextValue>) =>
|
|
52
|
+
view.rerender(
|
|
53
|
+
<EventBusContext.Provider value={bus.ctx}>
|
|
54
|
+
<WorkspaceContext.Provider value={makeWorkspaceFixture(next)}>
|
|
55
|
+
<GitSyncFailedBanner />
|
|
56
|
+
</WorkspaceContext.Provider>
|
|
57
|
+
</EventBusContext.Provider>,
|
|
58
|
+
);
|
|
59
|
+
return { ...bus, rerender };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const FAILED = {
|
|
63
|
+
kind: 'git-sync-failed',
|
|
64
|
+
workspaceId: 'ws-1',
|
|
65
|
+
branch: 'feat/x',
|
|
66
|
+
reason: 'git push failed: fatal: Authentication failed',
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
describe('GitSyncFailedBanner', () => {
|
|
70
|
+
it('renders nothing until a failure arrives — the healthy case is invisible', () => {
|
|
71
|
+
renderBanner();
|
|
72
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('tells the author their work is safe and points an admin at the logs', () => {
|
|
76
|
+
const bus = renderBanner();
|
|
77
|
+
bus.emit(FAILED);
|
|
78
|
+
|
|
79
|
+
const alert = screen.getByRole('alert');
|
|
80
|
+
// The two things this banner exists to say: the save is not lost, and
|
|
81
|
+
// whoever can act should look at the server logs. Neither is negotiable —
|
|
82
|
+
// an author reading "sync failed" with no reassurance re-types their work.
|
|
83
|
+
expect(alert.textContent).toContain('saved here');
|
|
84
|
+
expect(alert.textContent).toContain('server logs');
|
|
85
|
+
expect(alert.textContent).toContain('feat/x');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('shows the sanitised git reason as the detail line', () => {
|
|
89
|
+
const bus = renderBanner();
|
|
90
|
+
bus.emit(FAILED);
|
|
91
|
+
expect(screen.getByRole('alert').textContent).toContain('Authentication failed');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('clears when the backend reports the push recovered', () => {
|
|
95
|
+
const bus = renderBanner();
|
|
96
|
+
bus.emit(FAILED);
|
|
97
|
+
expect(screen.queryByRole('alert')).not.toBeNull();
|
|
98
|
+
|
|
99
|
+
bus.emit({ kind: 'git-sync-recovered', workspaceId: 'ws-1', branch: 'feat/x' });
|
|
100
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('ignores events for another workspace', () => {
|
|
104
|
+
// Focus changes race with in-flight events; a failure on the branch the
|
|
105
|
+
// user just left must not paint a banner over the one they are on.
|
|
106
|
+
const bus = renderBanner();
|
|
107
|
+
bus.emit({ ...FAILED, workspaceId: 'ws-2', branch: 'feat/other' });
|
|
108
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('does not let another workspace recovering clear this one’s banner', () => {
|
|
112
|
+
const bus = renderBanner();
|
|
113
|
+
bus.emit(FAILED);
|
|
114
|
+
bus.emit({ kind: 'git-sync-recovered', workspaceId: 'ws-2', branch: 'feat/other' });
|
|
115
|
+
expect(screen.queryByRole('alert')).not.toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('matches a slashed feature branch across the encode/decode split', () => {
|
|
119
|
+
// Local `workspace.id` is URL-encoded; the SSE event id is URL-decoded.
|
|
120
|
+
// A raw `===` drops every event on a `<user>/<branch>` workspace — which
|
|
121
|
+
// is every feature branch — so the banner would never show, silently
|
|
122
|
+
// recreating the invisible-failure bug this whole feature fixes.
|
|
123
|
+
const bus = renderBanner({ workspaceId: 'razvan-radulescu%2Ffeat' });
|
|
124
|
+
bus.emit({
|
|
125
|
+
kind: 'git-sync-failed',
|
|
126
|
+
workspaceId: 'razvan-radulescu/feat', // decoded, as the backend emits it
|
|
127
|
+
branch: 'razvan-radulescu/feat',
|
|
128
|
+
reason: 'git push failed: fatal: Authentication failed',
|
|
129
|
+
});
|
|
130
|
+
expect(screen.getByRole('alert').textContent).toContain('Authentication failed');
|
|
131
|
+
|
|
132
|
+
bus.emit({
|
|
133
|
+
kind: 'git-sync-recovered',
|
|
134
|
+
workspaceId: 'razvan-radulescu/feat',
|
|
135
|
+
branch: 'razvan-radulescu/feat',
|
|
136
|
+
});
|
|
137
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('keeps each branch’s failure separately — returning to a broken branch still shows it', () => {
|
|
141
|
+
// A single retained record let branch B's failure overwrite branch A's;
|
|
142
|
+
// switching back to A then showed nothing despite A being unresolved.
|
|
143
|
+
const bus = renderBanner({ workspaceId: 'ws-1' });
|
|
144
|
+
bus.emit(FAILED); // ws-1 fails
|
|
145
|
+
|
|
146
|
+
act(() => bus.rerender({ workspaceId: 'ws-2' }));
|
|
147
|
+
bus.emit({ ...FAILED, workspaceId: 'ws-2', branch: 'feat/y', reason: 'ws-2 broke too' });
|
|
148
|
+
expect(screen.getByRole('alert').textContent).toContain('ws-2 broke too');
|
|
149
|
+
|
|
150
|
+
act(() => bus.rerender({ workspaceId: 'ws-1' }));
|
|
151
|
+
expect(screen.getByRole('alert').textContent).toContain('feat/x');
|
|
152
|
+
|
|
153
|
+
// ws-1 recovering clears only ws-1's record.
|
|
154
|
+
bus.emit({ kind: 'git-sync-recovered', workspaceId: 'ws-1', branch: 'feat/x' });
|
|
155
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
156
|
+
act(() => bus.rerender({ workspaceId: 'ws-2' }));
|
|
157
|
+
expect(screen.getByRole('alert').textContent).toContain('ws-2 broke too');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('clears a stale banner when the focused workspace changes', () => {
|
|
161
|
+
// FileViewer is not keyed by workspace, so this component survives a branch
|
|
162
|
+
// switch. A failure from the branch just left must not paint over the new
|
|
163
|
+
// one — and no recovered event would ever arrive for it.
|
|
164
|
+
const bus = renderBanner({ workspaceId: 'ws-1' });
|
|
165
|
+
bus.emit(FAILED);
|
|
166
|
+
expect(screen.queryByRole('alert')).not.toBeNull();
|
|
167
|
+
|
|
168
|
+
act(() => bus.rerender({ workspaceId: 'ws-2' }));
|
|
169
|
+
expect(screen.queryByRole('alert')).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('stays up across repeated failures, showing the latest reason', () => {
|
|
173
|
+
const bus = renderBanner();
|
|
174
|
+
bus.emit(FAILED);
|
|
175
|
+
bus.emit({ ...FAILED, reason: 'git push failed: could not read Username' });
|
|
176
|
+
|
|
177
|
+
const alert = screen.getByRole('alert');
|
|
178
|
+
expect(alert.textContent).toContain('could not read Username');
|
|
179
|
+
expect(screen.getAllByRole('alert')).toHaveLength(1);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { CloudOff } from 'lucide-react';
|
|
3
|
+
import { useEventBusSubscription } from '../../workflow/hooks/useEventBusSubscription';
|
|
4
|
+
import { canonicalizeWorkspaceId } from '../../workflow/state/event-bus.context';
|
|
5
|
+
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Shown when a commit landed locally but could not be pushed to the remote.
|
|
9
|
+
*
|
|
10
|
+
* The save itself is fine and is NOT rolled back — the commit is on the
|
|
11
|
+
* server's disk and the retry paths keep running — so this is a warning, not
|
|
12
|
+
* an error state, and nothing here blocks the user. It exists because the
|
|
13
|
+
* failure is otherwise completely invisible: pushes happen behind saves, the
|
|
14
|
+
* one path that does surface an error only reaches a still-mounted editor, and
|
|
15
|
+
* a deployment with a broken git credential therefore looks perfectly healthy
|
|
16
|
+
* while every commit stacks up locally. One self-hosted install ran two days
|
|
17
|
+
* and 136 commits that way.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately not addressed to the author: every cause of a failing push —
|
|
20
|
+
* an expired token, a revoked repo grant, an unreachable host — is an operator
|
|
21
|
+
* concern, and there is nothing the person writing a page can do about it. So
|
|
22
|
+
* the copy tells them their work is safe and points whoever can act at the
|
|
23
|
+
* logs, rather than explaining git.
|
|
24
|
+
*
|
|
25
|
+
* Clears on `git-sync-recovered`, which the backend emits when a push
|
|
26
|
+
* succeeds after failing. No dismiss control, same as `PullNeededBanner`: the
|
|
27
|
+
* condition is real until the server says otherwise, and the next failing save
|
|
28
|
+
* would re-raise it anyway.
|
|
29
|
+
*/
|
|
30
|
+
export function GitSyncFailedBanner() {
|
|
31
|
+
const workspace = useWorkspace();
|
|
32
|
+
const workspaceId = workspace.workspaceId;
|
|
33
|
+
// Local state (`workspace.id`) is URL-encoded (`user%2Ffeat`); event ids
|
|
34
|
+
// arrive URL-decoded (`user/feat`, Express auto-decodes `req.params.id`).
|
|
35
|
+
// Comparing them raw drops every event on a slashed feature branch — the
|
|
36
|
+
// exact silent-failure symptom this banner exists to surface — so both
|
|
37
|
+
// sides are canonicalised, the same way every other SSE consumer does.
|
|
38
|
+
const canonId = workspaceId ? canonicalizeWorkspaceId(workspaceId) : null;
|
|
39
|
+
// One record PER workspace, keyed by canonical id. FileViewer is mounted at
|
|
40
|
+
// a catch-all route and is NOT keyed by workspace, so this component
|
|
41
|
+
// survives every branch switch: a single record would let branch B's
|
|
42
|
+
// failure overwrite branch A's, hiding A's unresolved state on return. The
|
|
43
|
+
// render gate below shows only the focused workspace's record.
|
|
44
|
+
//
|
|
45
|
+
// Known limit, accepted deliberately: SSE events are focus-scoped, so a
|
|
46
|
+
// recovery that happens while the user is on another branch never arrives —
|
|
47
|
+
// a retained record can be stale on return until that branch's next push
|
|
48
|
+
// settles it (failures re-emit on every attempt, recoveries on the next
|
|
49
|
+
// transition). The alternative — clearing on switch — hides real, still-
|
|
50
|
+
// broken state, which is the exact failure mode this PR exists to fix.
|
|
51
|
+
const [failures, setFailures] = useState<ReadonlyMap<string, { branch: string; reason: string }>>(
|
|
52
|
+
new Map(),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
useEventBusSubscription(
|
|
56
|
+
'git-sync-failed',
|
|
57
|
+
useCallback(
|
|
58
|
+
(e) => {
|
|
59
|
+
// The bus already filters to the focused workspace, but a focus change
|
|
60
|
+
// races with in-flight events — checking here keeps another branch's
|
|
61
|
+
// failure from being recorded under this one's key.
|
|
62
|
+
if (!canonId || canonicalizeWorkspaceId(e.workspaceId) !== canonId) return;
|
|
63
|
+
setFailures((prev) =>
|
|
64
|
+
new Map(prev).set(canonId, { branch: e.branch, reason: e.reason }),
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
[canonId],
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
useEventBusSubscription(
|
|
72
|
+
'git-sync-recovered',
|
|
73
|
+
useCallback(
|
|
74
|
+
(e) => {
|
|
75
|
+
if (!canonId || canonicalizeWorkspaceId(e.workspaceId) !== canonId) return;
|
|
76
|
+
setFailures((prev) => {
|
|
77
|
+
if (!prev.has(canonId)) return prev;
|
|
78
|
+
const next = new Map(prev);
|
|
79
|
+
next.delete(canonId);
|
|
80
|
+
return next;
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
[canonId],
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const failure = canonId ? failures.get(canonId) : undefined;
|
|
88
|
+
if (!failure) return null;
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div
|
|
92
|
+
role="alert"
|
|
93
|
+
className="px-3 py-1.5 bg-sunken border-b border-line text-xs text-ink shrink-0"
|
|
94
|
+
>
|
|
95
|
+
<div className="flex items-center gap-2">
|
|
96
|
+
<CloudOff size={13} className="shrink-0" />
|
|
97
|
+
<span className="flex-1">
|
|
98
|
+
Changes on <span className="font-mono font-semibold">{failure.branch}</span> aren’t
|
|
99
|
+
reaching the remote repository. Your work is saved here — an administrator should check
|
|
100
|
+
the server logs.
|
|
101
|
+
</span>
|
|
102
|
+
</div>
|
|
103
|
+
<div className="mt-1 ml-5 text-ink-muted break-words">{failure.reason}</div>
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -1,23 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* staging, some embedded webviews), and `writeText` rejects when the document
|
|
6
|
-
* isn't focused. Both are ordinary conditions here, not exceptions — the
|
|
7
|
-
* Library's copy buttons hand over an agent prompt the user can also just
|
|
8
|
-
* select, so a failure has a real answer ("select the text instead") and must
|
|
9
|
-
* never surface as a success toast.
|
|
10
|
-
*/
|
|
11
|
-
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
12
|
-
const clipboard = navigator.clipboard;
|
|
13
|
-
if (!clipboard?.writeText) return false;
|
|
14
|
-
try {
|
|
15
|
-
await clipboard.writeText(text);
|
|
16
|
-
return true;
|
|
17
|
-
} catch {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
1
|
+
// The implementation moved to `lib/clipboard.ts` when the email viewer needed
|
|
2
|
+
// it too; re-exported here so the Library keeps importing its copy helper and
|
|
3
|
+
// its toast strings from one place.
|
|
4
|
+
export { copyToClipboard } from '../../../lib/clipboard';
|
|
21
5
|
|
|
22
6
|
/** What a copy button tells the user, either way. */
|
|
23
7
|
export const COPIED_TOAST = 'Prompt copied.';
|