@bridgebrew/shell 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/browser/approvals.tsx +373 -0
- package/browser/browser.tsx +2020 -0
- package/browser/css.d.ts +2 -0
- package/browser/errors.tsx +552 -0
- package/browser/keep-focus.ts +65 -0
- package/browser/look.css +118 -0
- package/browser/look.ts +218 -0
- package/browser/mobile.css +305 -0
- package/browser/mobile.tsx +1528 -0
- package/browser/node-actions.ts +29 -0
- package/browser/notifications.ts +1264 -0
- package/browser/page-slot.ts +48 -0
- package/browser/restart-screen.tsx +156 -0
- package/browser/shell-state.ts +1800 -0
- package/browser/shell.d.ts +924 -0
- package/browser/shortcuts.ts +528 -0
- package/browser/theme-controls.tsx +379 -0
- package/browser/transition.ts +140 -0
- package/browser/tsconfig.json +15 -0
- package/browser/view-scopes.ts +75 -0
- package/browser/wallpaper-ink.ts +167 -0
- package/browser/window.css +9 -0
- package/package.json +89 -4
- package/tsconfig.json +5 -0
- package/README.md +0 -3
package/browser/css.d.ts
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
// The Errors modal (E4): everything failing in this Bridge now, in full, from `/_bridge/status` (./notifications.ts
|
|
2
|
+
// `report`), what a plugin reports needs attention included, warnings too. A failure toast's Details opens it, scrolled to
|
|
3
|
+
// that item (`shell.showErrors`); Open Health leads to Admin's Health (research-047), where the rail's status dot goes.
|
|
4
|
+
// Each item is a card: its title and workspaces, the raw error in a monospace block, its way out, the dependents waiting
|
|
5
|
+
// on it, a definition list of its other status fields, then its fix (run as the toasts' is, `runFix`; Open the setting
|
|
6
|
+
// opens it), a Copy, and a Logs disclosure that fetches that item's places' error and warning logs. `Copy all` writes
|
|
7
|
+
// `reportText` of every item.
|
|
8
|
+
import * as React from 'react';
|
|
9
|
+
import {
|
|
10
|
+
Button,
|
|
11
|
+
Dialog,
|
|
12
|
+
DialogBody,
|
|
13
|
+
DialogContent,
|
|
14
|
+
DialogDescription,
|
|
15
|
+
DialogFooter,
|
|
16
|
+
DialogHeader,
|
|
17
|
+
DialogTitle,
|
|
18
|
+
useToast,
|
|
19
|
+
type ToastInput,
|
|
20
|
+
copyText,
|
|
21
|
+
isRecord,
|
|
22
|
+
refusalOf,
|
|
23
|
+
gestureOf,
|
|
24
|
+
type FollowEvent,
|
|
25
|
+
} from '@bridge/ui';
|
|
26
|
+
import type { Context } from 'cordis';
|
|
27
|
+
import type {} from './shell.d.ts';
|
|
28
|
+
import { copyOf, report, reportText, toastOf, type Fix, type ReportItem } from './notifications.ts';
|
|
29
|
+
|
|
30
|
+
/** A request to open the Errors modal, carrying the item key to scroll to (`undefined`: the top). `shell.showErrors`
|
|
31
|
+
* emits; the modal, mounted by ./browser.tsx, subscribes. */
|
|
32
|
+
export function errorsSignal() {
|
|
33
|
+
const listeners = new Set<(key: string | undefined) => void>();
|
|
34
|
+
return {
|
|
35
|
+
emit(key: string | undefined) {
|
|
36
|
+
for (const listener of listeners) listener(key);
|
|
37
|
+
},
|
|
38
|
+
subscribe(listener: (key: string | undefined) => void) {
|
|
39
|
+
listeners.add(listener);
|
|
40
|
+
return () => {
|
|
41
|
+
listeners.delete(listener);
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export type ErrorsSignal = ReturnType<typeof errorsSignal>;
|
|
47
|
+
|
|
48
|
+
/** A failure's way out as a button's label, by its fix (the toasts' too). */
|
|
49
|
+
export const fixLabels: Record<Fix['action'], string> = {
|
|
50
|
+
restart: 'Restart',
|
|
51
|
+
retry: 'Retry',
|
|
52
|
+
'restart-bridge': 'Restart when ready',
|
|
53
|
+
reload: 'Reload',
|
|
54
|
+
'reload-page': 'Reload page',
|
|
55
|
+
'reload-all': 'Reload all',
|
|
56
|
+
setting: 'Open the setting',
|
|
57
|
+
'plugin-action': 'Run',
|
|
58
|
+
};
|
|
59
|
+
/** A fix's button label: a plugin's action names its own; a setting with no field opens the plugin's settings page. */
|
|
60
|
+
export const fixLabel = (fix: Fix) =>
|
|
61
|
+
fix.action === 'plugin-action' || fix.action === 'retry'
|
|
62
|
+
? fix.label
|
|
63
|
+
: fix.action === 'setting' && fix.setting === ''
|
|
64
|
+
? 'Open its settings'
|
|
65
|
+
: fixLabels[fix.action];
|
|
66
|
+
|
|
67
|
+
/** The host route a fix's button runs (the Plugins section's, `bridge restart` / `bridge reload`'s); Reload page then
|
|
68
|
+
* reloads the page. Reload all is not one route: the targets are walked in turn. */
|
|
69
|
+
const fixUrl = (fix: Exclude<Fix, { action: 'reload-all' | 'setting' | 'plugin-action' }>) =>
|
|
70
|
+
fix.action === 'restart-bridge'
|
|
71
|
+
? '/_bridge/restart'
|
|
72
|
+
: `/_bridge/workspaces/${encodeURIComponent(fix.slug)}/${
|
|
73
|
+
fix.action === 'restart' || fix.action === 'retry'
|
|
74
|
+
? `restart${fix.action === 'retry' || fix.plugin === undefined ? '' : `?plugin=${encodeURIComponent(fix.plugin)}`}`
|
|
75
|
+
: `plugins/${encodeURIComponent(fix.plugin)}/reload`
|
|
76
|
+
}`;
|
|
77
|
+
|
|
78
|
+
/** Runs a failure's fix (a toast's button, or the Errors modal's card): the request(s), a toast for the start and any
|
|
79
|
+
* refusal. `Notifications`' old `run`, shared. */
|
|
80
|
+
export function runFix(fix: Fix, title: string, toast: (input: ToastInput) => string) {
|
|
81
|
+
// Open the setting is the page's own navigation (`Notifications`), no request.
|
|
82
|
+
if (fix.action === 'setting') return;
|
|
83
|
+
if (fix.action === 'plugin-action') {
|
|
84
|
+
// Through `ActionConfirm` when it is mounted (its question first), else at once.
|
|
85
|
+
if (actions.size) for (const listener of actions) listener(fix);
|
|
86
|
+
else void runPluginAction(fix, toast);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const label = fixLabels[fix.action];
|
|
90
|
+
// Bridge's restart says where it stands itself (the notice while it waits, then the restart screen).
|
|
91
|
+
if (fix.action !== 'restart-bridge')
|
|
92
|
+
toast({
|
|
93
|
+
variant: 'info',
|
|
94
|
+
title: `${label === 'Restart' ? 'Restarting' : label === 'Retry' ? 'Retrying' : 'Reloading'}: ${title}`,
|
|
95
|
+
});
|
|
96
|
+
const failure = (description: string) =>
|
|
97
|
+
toast({
|
|
98
|
+
...toastOf({
|
|
99
|
+
variant: 'error',
|
|
100
|
+
title: `${label} failed`,
|
|
101
|
+
description,
|
|
102
|
+
copy: copyOf(`${label} failed`, description),
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
const errorOf = async (res: Response) => {
|
|
106
|
+
const { error } = (await res
|
|
107
|
+
.json()
|
|
108
|
+
.catch(() => ({ error: { message: `HTTP ${res.status}` } }))) as {
|
|
109
|
+
error?: { message?: string; hint?: string };
|
|
110
|
+
};
|
|
111
|
+
return [error?.message, error?.hint].filter(Boolean).join(' ');
|
|
112
|
+
};
|
|
113
|
+
// Reload all: each workspace's root and reload-failed dependents in turn, the workspaces in parallel; the first
|
|
114
|
+
// refusal (or lost connection) in a workspace stops it and toasts why.
|
|
115
|
+
if (fix.action === 'reload-all') {
|
|
116
|
+
void Promise.all(
|
|
117
|
+
fix.targets.map(async ({ slug, plugins }) => {
|
|
118
|
+
for (const plugin of plugins) {
|
|
119
|
+
try {
|
|
120
|
+
const res = await fetch(
|
|
121
|
+
`/_bridge/workspaces/${encodeURIComponent(slug)}/plugins/${encodeURIComponent(plugin)}/reload`,
|
|
122
|
+
{ method: 'POST' },
|
|
123
|
+
);
|
|
124
|
+
if (!res.ok) {
|
|
125
|
+
failure(await errorOf(res));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
} catch (cause) {
|
|
129
|
+
failure(cause instanceof Error ? cause.message : String(cause));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
void fetch(fixUrl(fix), { method: 'POST' }).then(
|
|
138
|
+
async (res) => {
|
|
139
|
+
if (res.ok) {
|
|
140
|
+
if (fix.action === 'reload-page') location.reload();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
failure(await errorOf(res));
|
|
144
|
+
},
|
|
145
|
+
(cause) => failure(cause instanceof Error ? cause.message : String(cause)),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type PluginActionFix = Extract<Fix, { action: 'plugin-action' }>;
|
|
150
|
+
/** The mounted `ActionConfirm`s, `runFix`'s way to a plugin's action (one, the shell's). */
|
|
151
|
+
const actions = new Set<(fix: PluginActionFix) => void>();
|
|
152
|
+
|
|
153
|
+
/** A plugin's settings action run from a toast, a card or Health (`runFix`): its `confirm` asked first in a dialog
|
|
154
|
+
* (Take over: the other Bridge stops writing), the buttons on the right; one without `confirm` runs at once. */
|
|
155
|
+
export function ActionConfirm() {
|
|
156
|
+
const { toast } = useToast();
|
|
157
|
+
const [pending, setPending] = React.useState<PluginActionFix | undefined>(undefined);
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
const listener = (fix: PluginActionFix) => {
|
|
160
|
+
if (fix.confirm === undefined) void runPluginAction(fix, toast);
|
|
161
|
+
else setPending(fix);
|
|
162
|
+
};
|
|
163
|
+
actions.add(listener);
|
|
164
|
+
return () => void actions.delete(listener);
|
|
165
|
+
}, [toast]);
|
|
166
|
+
return (
|
|
167
|
+
<Dialog open={pending !== undefined} onOpenChange={(open) => !open && setPending(undefined)}>
|
|
168
|
+
<DialogContent>
|
|
169
|
+
<DialogHeader>
|
|
170
|
+
<DialogTitle>{pending?.label}</DialogTitle>
|
|
171
|
+
</DialogHeader>
|
|
172
|
+
<DialogBody className="pb-4">
|
|
173
|
+
<DialogDescription>{pending?.confirm}</DialogDescription>
|
|
174
|
+
</DialogBody>
|
|
175
|
+
<DialogFooter className="justify-end gap-2">
|
|
176
|
+
<Button variant="outline" onClick={() => setPending(undefined)}>
|
|
177
|
+
Cancel
|
|
178
|
+
</Button>
|
|
179
|
+
<Button
|
|
180
|
+
onClick={() => {
|
|
181
|
+
if (pending) void runPluginAction(pending, toast);
|
|
182
|
+
setPending(undefined);
|
|
183
|
+
}}
|
|
184
|
+
>
|
|
185
|
+
{pending?.label}
|
|
186
|
+
</Button>
|
|
187
|
+
</DialogFooter>
|
|
188
|
+
</DialogContent>
|
|
189
|
+
</Dialog>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** A plugin's settings action from a toast (research-047): its route found by `id` among the `x-bridge-action` entries of
|
|
194
|
+
* the plugin's settings (`/_bridge/workspaces/<slug>/nodes`), posted the saved value as Settings posts it, its answer
|
|
195
|
+
* toasted. The status events that follow dismiss the attention's toast once it is solved. */
|
|
196
|
+
async function runPluginAction(fix: PluginActionFix, toast: (input: ToastInput) => string) {
|
|
197
|
+
const failure = (description: string, hint?: string) => {
|
|
198
|
+
const text = hint === undefined ? description : `${description} ${hint}`;
|
|
199
|
+
toast(
|
|
200
|
+
toastOf({
|
|
201
|
+
variant: 'error',
|
|
202
|
+
title: `${fix.label} failed`,
|
|
203
|
+
description: text,
|
|
204
|
+
copy: copyOf(`${fix.label} failed`, text),
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
try {
|
|
209
|
+
const tree: unknown = await fetch(`/_bridge/workspaces/${encodeURIComponent(fix.slug)}/nodes`, {
|
|
210
|
+
cache: 'no-store',
|
|
211
|
+
}).then((res) => res.json());
|
|
212
|
+
const found = isRecord(tree) ? actionIn(tree, fix.plugin, fix.id) : undefined;
|
|
213
|
+
if (!found) {
|
|
214
|
+
failure(`${fix.plugin} has no action ${JSON.stringify(fix.id)} in its settings.`);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const res = await fetch(
|
|
218
|
+
`/${encodeURIComponent(fix.slug)}/api/${encodeURIComponent(fix.plugin)}${found.path}`,
|
|
219
|
+
{
|
|
220
|
+
method: 'POST',
|
|
221
|
+
headers: { 'content-type': 'application/json', 'x-bridge-node': 'root' },
|
|
222
|
+
body: JSON.stringify(found.entry ?? null),
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
const body: unknown = await res.json().catch(() => undefined);
|
|
226
|
+
if (res.ok && isRecord(body) && body.ok === true) {
|
|
227
|
+
toast({
|
|
228
|
+
variant: 'success',
|
|
229
|
+
title: typeof body.message === 'string' ? body.message : `${fix.label}: done`,
|
|
230
|
+
});
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!res.ok) {
|
|
234
|
+
const { message, hint } = refusalOf(body, res.status);
|
|
235
|
+
failure(message, hint);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (isRecord(body) && typeof body.error === 'string')
|
|
239
|
+
failure(body.error, typeof body.hint === 'string' ? body.hint : undefined);
|
|
240
|
+
else failure('The answer was not understood.');
|
|
241
|
+
} catch (cause) {
|
|
242
|
+
failure(cause instanceof Error ? cause.message : String(cause));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The route of plugin `plugin`'s settings action `id` in a nodes tree, and the saved value of its setting (root level). */
|
|
247
|
+
const actionIn = (tree: Record<string, unknown>, plugin: string, id: string) => {
|
|
248
|
+
const schema = isRecord(tree.schemas) ? tree.schemas[plugin] : undefined;
|
|
249
|
+
const properties = isRecord(schema) && isRecord(schema.properties) ? schema.properties : {};
|
|
250
|
+
for (const [field, property] of Object.entries(properties)) {
|
|
251
|
+
if (!isRecord(property)) continue;
|
|
252
|
+
const declared = property['x-bridge-action'];
|
|
253
|
+
const actions: unknown[] = Array.isArray(declared) ? declared : [declared];
|
|
254
|
+
const action = actions.find((each) => isRecord(each) && each.id === id);
|
|
255
|
+
if (!isRecord(action) || typeof action.path !== 'string') continue;
|
|
256
|
+
const root = isRecord(tree.resolved) ? tree.resolved.root : undefined;
|
|
257
|
+
const settings = isRecord(root) && isRecord(root.settings) ? root.settings[plugin] : undefined;
|
|
258
|
+
const values = isRecord(settings) && isRecord(settings.values) ? settings.values : {};
|
|
259
|
+
const value = values[field];
|
|
260
|
+
return { path: action.path, entry: isRecord(value) ? value.value : undefined };
|
|
261
|
+
}
|
|
262
|
+
return undefined;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/** Writes `text` with `copyText`, saying `Copied`, or `Copy failed` when the write fails, for 1.5 s. */
|
|
266
|
+
function CopyButton({
|
|
267
|
+
text,
|
|
268
|
+
label = 'Copy',
|
|
269
|
+
...props
|
|
270
|
+
}: { text: string; label?: string } & Omit<
|
|
271
|
+
React.ComponentProps<typeof Button>,
|
|
272
|
+
'onClick' | 'children'
|
|
273
|
+
>) {
|
|
274
|
+
const [said, setSaid] = React.useState<string | undefined>();
|
|
275
|
+
const timerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
276
|
+
React.useEffect(() => () => clearTimeout(timerRef.current), []);
|
|
277
|
+
const say = (words: string) => {
|
|
278
|
+
setSaid(words);
|
|
279
|
+
clearTimeout(timerRef.current);
|
|
280
|
+
timerRef.current = setTimeout(() => setSaid(undefined), 1500);
|
|
281
|
+
};
|
|
282
|
+
const write = () => {
|
|
283
|
+
void copyText(text).then(
|
|
284
|
+
() => say('Copied'),
|
|
285
|
+
() => say('Copy failed'),
|
|
286
|
+
);
|
|
287
|
+
};
|
|
288
|
+
return (
|
|
289
|
+
<Button size="sm" onClick={write} {...props}>
|
|
290
|
+
{said ?? label}
|
|
291
|
+
</Button>
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The error and warning logs of each of `item`'s places that has a plugin, kept as `time + message` lines (newest last). */
|
|
296
|
+
async function placesLogs(item: ReportItem): Promise<{ lines: string[]; errors: string[] }> {
|
|
297
|
+
const places = item.places.filter(
|
|
298
|
+
(place): place is { slug: string; title: string; plugin: string } => place.plugin !== undefined,
|
|
299
|
+
);
|
|
300
|
+
const results = await Promise.all(
|
|
301
|
+
places.map(async (place) => {
|
|
302
|
+
try {
|
|
303
|
+
const res = await fetch(
|
|
304
|
+
`/_bridge/logs?workspace=${encodeURIComponent(place.slug)}&plugin=${encodeURIComponent(
|
|
305
|
+
place.plugin,
|
|
306
|
+
)}&n=200`,
|
|
307
|
+
{ cache: 'no-store' },
|
|
308
|
+
);
|
|
309
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
310
|
+
const { records = [] } = (await res.json()) as {
|
|
311
|
+
records?: { time: string; level: string; message: string }[];
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
ok: true as const,
|
|
315
|
+
lines: records
|
|
316
|
+
.filter((record) => record.level === 'error' || record.level === 'warn')
|
|
317
|
+
.slice(-5)
|
|
318
|
+
.map((record) => `${record.time} ${record.message}`),
|
|
319
|
+
};
|
|
320
|
+
} catch (cause) {
|
|
321
|
+
return {
|
|
322
|
+
ok: false as const,
|
|
323
|
+
error: `${place.slug}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}),
|
|
327
|
+
);
|
|
328
|
+
return {
|
|
329
|
+
lines: results.flatMap((result) => (result.ok ? result.lines : [])),
|
|
330
|
+
errors: results.flatMap((result) => (result.ok ? [] : [result.error])),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** One failure's card: its title and places, the raw error, its way out, its waiting dependents and status fields, then
|
|
335
|
+
* its fix, Copy and Logs. */
|
|
336
|
+
function ErrorCard({
|
|
337
|
+
item,
|
|
338
|
+
onFix,
|
|
339
|
+
}: {
|
|
340
|
+
item: ReportItem;
|
|
341
|
+
onFix: (fix: Fix, title: string, event: React.MouseEvent) => void;
|
|
342
|
+
}) {
|
|
343
|
+
const [logs, setLogs] = React.useState<string[] | undefined>(undefined);
|
|
344
|
+
const [logError, setLogError] = React.useState<string | undefined>(undefined);
|
|
345
|
+
const hasLogs = item.places.some((place) => place.plugin !== undefined);
|
|
346
|
+
const copy =
|
|
347
|
+
logs !== undefined && logs.length > 0
|
|
348
|
+
? `${reportText([item])}\n\n${logs.join('\n')}`
|
|
349
|
+
: reportText([item]);
|
|
350
|
+
// Its places' error logs, fetched once the card shows (the item's key changes with what it holds).
|
|
351
|
+
React.useEffect(() => {
|
|
352
|
+
if (!hasLogs) return;
|
|
353
|
+
let gone = false;
|
|
354
|
+
void placesLogs(item).then(({ lines, errors }) => {
|
|
355
|
+
if (gone) return;
|
|
356
|
+
setLogs(lines);
|
|
357
|
+
setLogError(errors.length > 0 ? errors.join('\n') : undefined);
|
|
358
|
+
});
|
|
359
|
+
return () => {
|
|
360
|
+
gone = true;
|
|
361
|
+
};
|
|
362
|
+
}, [item.key, hasLogs]);
|
|
363
|
+
const places = item.places.map((place) => place.title).join(', ');
|
|
364
|
+
return (
|
|
365
|
+
<article
|
|
366
|
+
data-error-item={item.key}
|
|
367
|
+
className="rounded-default border border-border p-3 transition-shadow"
|
|
368
|
+
>
|
|
369
|
+
<h3 className="m-0 flex items-center gap-2 text-sm font-medium">
|
|
370
|
+
<span
|
|
371
|
+
aria-hidden="true"
|
|
372
|
+
className={`size-2 shrink-0 rounded-full ${item.kind === 'cause' || item.severity === 'error' ? 'bg-destructive' : 'bg-warning'}`}
|
|
373
|
+
/>
|
|
374
|
+
{item.title}
|
|
375
|
+
</h3>
|
|
376
|
+
<p className="m-0 mt-1 text-xs text-secondary">{places}</p>
|
|
377
|
+
<pre className="m-0 mt-2 max-h-48 overflow-auto rounded-default border border-input bg-hover p-2 font-mono text-xs whitespace-pre-wrap">
|
|
378
|
+
{item.text}
|
|
379
|
+
</pre>
|
|
380
|
+
{item.hint !== undefined && <p className="m-0 mt-2 text-xs text-secondary">{item.hint}</p>}
|
|
381
|
+
{item.waiting.length > 0 && (
|
|
382
|
+
<p className="m-0 mt-1 text-xs text-secondary">
|
|
383
|
+
Waiting on it: {[...new Set(item.waiting.map(({ id }) => id))].join(', ')}
|
|
384
|
+
</p>
|
|
385
|
+
)}
|
|
386
|
+
{item.details.length > 0 && (
|
|
387
|
+
<dl className="m-0 mt-2 grid grid-cols-[auto_1fr] items-baseline gap-x-3 gap-y-0.5 text-xs">
|
|
388
|
+
{item.details.map(({ label, value }) => (
|
|
389
|
+
<React.Fragment key={label}>
|
|
390
|
+
<dt className="text-muted">{label}</dt>
|
|
391
|
+
<dd className="m-0 font-mono break-words whitespace-pre-wrap">{value}</dd>
|
|
392
|
+
</React.Fragment>
|
|
393
|
+
))}
|
|
394
|
+
</dl>
|
|
395
|
+
)}
|
|
396
|
+
<div className="mt-3 flex flex-wrap items-center gap-2">
|
|
397
|
+
{item.fix !== undefined && (
|
|
398
|
+
<Button
|
|
399
|
+
size="sm"
|
|
400
|
+
variant="outline"
|
|
401
|
+
onClick={(event) => onFix(item.fix!, item.title, event)}
|
|
402
|
+
// A setting's fix is a link to it: a middle click opens it beside too (research-048).
|
|
403
|
+
onAuxClick={(event) => {
|
|
404
|
+
if (event.button !== 1 || item.fix?.action !== 'setting') return;
|
|
405
|
+
event.preventDefault();
|
|
406
|
+
onFix(item.fix, item.title, event);
|
|
407
|
+
}}
|
|
408
|
+
>
|
|
409
|
+
{fixLabel(item.fix)}
|
|
410
|
+
</Button>
|
|
411
|
+
)}
|
|
412
|
+
<CopyButton text={copy} />
|
|
413
|
+
</div>
|
|
414
|
+
{hasLogs && (
|
|
415
|
+
<section className="mt-3">
|
|
416
|
+
<h4 className="m-0 mb-1 text-xs font-medium text-secondary">Logs</h4>
|
|
417
|
+
<pre className="m-0 max-h-72 overflow-auto rounded-default border border-input bg-hover p-2 font-mono text-xs whitespace-pre-wrap">
|
|
418
|
+
{logs === undefined && logError === undefined
|
|
419
|
+
? 'Loading…'
|
|
420
|
+
: logError === undefined && logs?.length === 0
|
|
421
|
+
? 'No error or warning in the logs.'
|
|
422
|
+
: [...(logError !== undefined ? [logError] : []), ...(logs ?? [])].join('\n')}
|
|
423
|
+
</pre>
|
|
424
|
+
</section>
|
|
425
|
+
)}
|
|
426
|
+
</article>
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** The Errors modal: fetched when it opens and again on each host status event while it is open (one fetch in flight). */
|
|
431
|
+
export function ErrorsModal({
|
|
432
|
+
hostStatus,
|
|
433
|
+
errors,
|
|
434
|
+
openSetting,
|
|
435
|
+
showHealth,
|
|
436
|
+
}: {
|
|
437
|
+
hostStatus: Context['hostStatus'];
|
|
438
|
+
errors: ErrorsSignal;
|
|
439
|
+
/** Open the setting, an attention's fix (the shell's `openSetting`). */
|
|
440
|
+
openSetting: (slug: string, plugin: string, setting: string, event?: FollowEvent) => void;
|
|
441
|
+
/** Admin's Health on its open problems (research-047): every episode, past ones too. */
|
|
442
|
+
showHealth: (event?: FollowEvent) => void;
|
|
443
|
+
}) {
|
|
444
|
+
const { toast } = useToast();
|
|
445
|
+
const [open, setOpen] = React.useState(false);
|
|
446
|
+
const [key, setKey] = React.useState<string | undefined>(undefined);
|
|
447
|
+
const [items, setItems] = React.useState<ReportItem[] | undefined>(undefined);
|
|
448
|
+
const [error, setError] = React.useState<string | undefined>(undefined);
|
|
449
|
+
const busy = React.useRef(false);
|
|
450
|
+
const queued = React.useRef(false);
|
|
451
|
+
const bodyRef = React.useRef<HTMLDivElement>(null);
|
|
452
|
+
|
|
453
|
+
React.useEffect(
|
|
454
|
+
() =>
|
|
455
|
+
errors.subscribe((next) => {
|
|
456
|
+
setKey(next);
|
|
457
|
+
setOpen(true);
|
|
458
|
+
}),
|
|
459
|
+
[errors],
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
const load = React.useCallback(() => {
|
|
463
|
+
if (busy.current) {
|
|
464
|
+
queued.current = true;
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
busy.current = true;
|
|
468
|
+
void fetch('/_bridge/status', { cache: 'no-store' })
|
|
469
|
+
.then((res) => (res.ok ? res.json() : Promise.reject(new Error(`HTTP ${res.status}`))))
|
|
470
|
+
.then((json: { workspaces: Parameters<typeof report>[0] }) => {
|
|
471
|
+
setItems(report(json.workspaces));
|
|
472
|
+
setError(undefined);
|
|
473
|
+
})
|
|
474
|
+
.catch((cause: unknown) => setError(cause instanceof Error ? cause.message : String(cause)))
|
|
475
|
+
.finally(() => {
|
|
476
|
+
busy.current = false;
|
|
477
|
+
if (queued.current) {
|
|
478
|
+
queued.current = false;
|
|
479
|
+
load();
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}, []);
|
|
483
|
+
|
|
484
|
+
React.useEffect(() => {
|
|
485
|
+
if (!open) return;
|
|
486
|
+
load();
|
|
487
|
+
return hostStatus.subscribe(() => load());
|
|
488
|
+
}, [open, hostStatus, load]);
|
|
489
|
+
|
|
490
|
+
// Opening with a key: scroll that item into view and outline it briefly.
|
|
491
|
+
React.useEffect(() => {
|
|
492
|
+
if (!open || key === undefined || items === undefined) return;
|
|
493
|
+
const element = bodyRef.current?.querySelector(`[data-error-item="${CSS.escape(key)}"]`);
|
|
494
|
+
if (!(element instanceof HTMLElement)) return;
|
|
495
|
+
element.scrollIntoView({ block: 'nearest' });
|
|
496
|
+
element.classList.add('ring-2', 'ring-warning');
|
|
497
|
+
const timer = setTimeout(() => element.classList.remove('ring-2', 'ring-warning'), 1500);
|
|
498
|
+
return () => clearTimeout(timer);
|
|
499
|
+
}, [open, key, items]);
|
|
500
|
+
|
|
501
|
+
const all = items ?? [];
|
|
502
|
+
/** Health by Bridge's click rule (research-048): a click or a middle click, ⇧ nothing. */
|
|
503
|
+
const openHealth = (event: React.MouseEvent) => {
|
|
504
|
+
if (gestureOf(event) === undefined) return;
|
|
505
|
+
event.preventDefault();
|
|
506
|
+
setOpen(false);
|
|
507
|
+
showHealth(event);
|
|
508
|
+
};
|
|
509
|
+
return (
|
|
510
|
+
<Dialog open={open} onOpenChange={setOpen}>
|
|
511
|
+
<DialogContent className="w-[92vw] max-w-[57.6rem]">
|
|
512
|
+
<DialogHeader>
|
|
513
|
+
<DialogTitle>Errors</DialogTitle>
|
|
514
|
+
<Button
|
|
515
|
+
size="sm"
|
|
516
|
+
variant="outline"
|
|
517
|
+
onClick={(event) => openHealth(event)}
|
|
518
|
+
onAuxClick={(event) => event.button === 1 && openHealth(event)}
|
|
519
|
+
>
|
|
520
|
+
Open Health
|
|
521
|
+
</Button>
|
|
522
|
+
<CopyButton text={reportText(all)} label="Copy all" />
|
|
523
|
+
</DialogHeader>
|
|
524
|
+
<DialogBody className="flex max-h-[84vh] flex-col gap-3 overflow-y-auto pb-3">
|
|
525
|
+
<DialogDescription>What is failing in this Bridge now.</DialogDescription>
|
|
526
|
+
{error !== undefined ? (
|
|
527
|
+
<p className="m-0 text-sm text-destructive">{error}</p>
|
|
528
|
+
) : items === undefined ? (
|
|
529
|
+
<p className="m-0 text-sm text-secondary">Loading…</p>
|
|
530
|
+
) : items.length === 0 ? (
|
|
531
|
+
<p className="m-0 text-sm text-secondary">Nothing is failing.</p>
|
|
532
|
+
) : (
|
|
533
|
+
<div ref={bodyRef} className="flex flex-col gap-3">
|
|
534
|
+
{items.map((item) => (
|
|
535
|
+
<ErrorCard
|
|
536
|
+
key={item.key}
|
|
537
|
+
item={item}
|
|
538
|
+
onFix={(fix, title, event) => {
|
|
539
|
+
if (fix.action !== 'setting') return runFix(fix, title, toast);
|
|
540
|
+
if (gestureOf(event) === undefined) return;
|
|
541
|
+
setOpen(false);
|
|
542
|
+
openSetting(fix.slug, fix.plugin, fix.setting, event);
|
|
543
|
+
}}
|
|
544
|
+
/>
|
|
545
|
+
))}
|
|
546
|
+
</div>
|
|
547
|
+
)}
|
|
548
|
+
</DialogBody>
|
|
549
|
+
</DialogContent>
|
|
550
|
+
</Dialog>
|
|
551
|
+
);
|
|
552
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Research-069: an agent's page never takes the typing focus. While window `id`'s new view mounts (its first render, a
|
|
2
|
+
// field's autofocus, a card's focus once loaded), a focus moving into it goes back where it was, its selection too,
|
|
3
|
+
// until the user moves it themselves (a pointer down, Tab, a shortcut) or another page is shown.
|
|
4
|
+
|
|
5
|
+
let stop: (() => void) | undefined;
|
|
6
|
+
|
|
7
|
+
/** Keeps the focus and the selection of now while window `id` mounts its new view. */
|
|
8
|
+
export function keepFocus(id: string): void {
|
|
9
|
+
stop?.();
|
|
10
|
+
const before = document.activeElement;
|
|
11
|
+
const field =
|
|
12
|
+
before instanceof HTMLInputElement || before instanceof HTMLTextAreaElement
|
|
13
|
+
? {
|
|
14
|
+
start: before.selectionStart,
|
|
15
|
+
end: before.selectionEnd,
|
|
16
|
+
direction: before.selectionDirection,
|
|
17
|
+
}
|
|
18
|
+
: undefined;
|
|
19
|
+
const selection = document.getSelection();
|
|
20
|
+
const ranges = selection
|
|
21
|
+
? Array.from({ length: selection.rangeCount }, (_, at) => selection.getRangeAt(at).cloneRange())
|
|
22
|
+
: [];
|
|
23
|
+
const back = (event: FocusEvent) => {
|
|
24
|
+
const target = event.target;
|
|
25
|
+
if (!(target instanceof Element) || !target.closest(`[data-window="${CSS.escape(id)}"]`))
|
|
26
|
+
return;
|
|
27
|
+
// Rejected before a window hears it (its manager focuses the window a focus enters): neither focus moves.
|
|
28
|
+
event.stopImmediatePropagation();
|
|
29
|
+
if (before instanceof HTMLElement && before.isConnected && before !== document.body) {
|
|
30
|
+
before.focus({ preventScroll: true });
|
|
31
|
+
if (field && (before instanceof HTMLInputElement || before instanceof HTMLTextAreaElement))
|
|
32
|
+
try {
|
|
33
|
+
before.setSelectionRange(field.start, field.end, field.direction ?? undefined);
|
|
34
|
+
} catch {
|
|
35
|
+
// An input type without a selection (a number): its focus is back, enough.
|
|
36
|
+
}
|
|
37
|
+
} else if (target instanceof HTMLElement) target.blur();
|
|
38
|
+
if (!field && selection && ranges.length) {
|
|
39
|
+
selection.removeAllRanges();
|
|
40
|
+
for (const range of ranges) selection.addRange(range);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
// The user moves the focus: a pointer down, Tab, a shortcut (a window's, ⌥ or ⌘); typing on keeps it where it is.
|
|
44
|
+
const ended = (event: Event) => {
|
|
45
|
+
const key = event as KeyboardEvent;
|
|
46
|
+
if (
|
|
47
|
+
event.type === 'keydown' &&
|
|
48
|
+
key.key !== 'Tab' &&
|
|
49
|
+
!key.altKey &&
|
|
50
|
+
!key.metaKey &&
|
|
51
|
+
!key.ctrlKey
|
|
52
|
+
)
|
|
53
|
+
return;
|
|
54
|
+
stop?.();
|
|
55
|
+
};
|
|
56
|
+
document.addEventListener('focusin', back, true);
|
|
57
|
+
document.addEventListener('pointerdown', ended, true);
|
|
58
|
+
document.addEventListener('keydown', ended, true);
|
|
59
|
+
stop = () => {
|
|
60
|
+
document.removeEventListener('focusin', back, true);
|
|
61
|
+
document.removeEventListener('pointerdown', ended, true);
|
|
62
|
+
document.removeEventListener('keydown', ended, true);
|
|
63
|
+
stop = undefined;
|
|
64
|
+
};
|
|
65
|
+
}
|