@bridgebrew/shell 0.0.0-stage → 0.0.2
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 +2025 -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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// State that lasts as long as the page across reloads of the shell's own module (a reload imports a new instance of it):
|
|
2
|
+
// kept in `sessionStorage` under `key`, tied to this document by `performance.timeOrigin`, so a page reloaded for real
|
|
3
|
+
// starts afresh, as an in-memory value would. Not on the global object, which a plugin may not write (research-062).
|
|
4
|
+
// Storage blocked or full: the value lives in this module instance only (lost at the next shell reload).
|
|
5
|
+
|
|
6
|
+
/** A JSON value of the page under `key`: `read()` (`undefined` when none for this document), `write(value)`. */
|
|
7
|
+
export function pageSlot<T>(key: string) {
|
|
8
|
+
let memory: T | undefined;
|
|
9
|
+
const origin = typeof performance === 'undefined' ? 0 : performance.timeOrigin;
|
|
10
|
+
const storage = () => {
|
|
11
|
+
try {
|
|
12
|
+
return typeof sessionStorage === 'undefined' ? undefined : sessionStorage;
|
|
13
|
+
} catch {
|
|
14
|
+
return undefined; // access refused (storage blocked)
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
return {
|
|
18
|
+
read(): T | undefined {
|
|
19
|
+
const store = storage();
|
|
20
|
+
// No storage (blocked, none): the value in this module instance.
|
|
21
|
+
if (!store) return memory;
|
|
22
|
+
try {
|
|
23
|
+
const text = store.getItem(key);
|
|
24
|
+
if (!text) return undefined;
|
|
25
|
+
const saved = JSON.parse(text) as unknown;
|
|
26
|
+
if (
|
|
27
|
+
typeof saved === 'object' &&
|
|
28
|
+
saved !== null &&
|
|
29
|
+
'origin' in saved &&
|
|
30
|
+
saved.origin === origin &&
|
|
31
|
+
'value' in saved
|
|
32
|
+
)
|
|
33
|
+
return saved.value as T;
|
|
34
|
+
return undefined; // another document's (a page reloaded): starts afresh
|
|
35
|
+
} catch {
|
|
36
|
+
return memory; // unreadable: the value in memory
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
write(value: T) {
|
|
40
|
+
memory = value;
|
|
41
|
+
try {
|
|
42
|
+
storage()?.setItem(key, JSON.stringify({ origin, value }));
|
|
43
|
+
} catch {
|
|
44
|
+
// blocked or full: kept in memory only
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Bridge's restart, as the page lives it (research-056 §14): once the host says its restart is under way (`code.restarting:
|
|
2
|
+
// 'now'`, after Restart when ready waited for the sessions), a modal that cannot be closed says Bridge restarts, through the
|
|
3
|
+
// lost connection (the boot banner keeps quiet meanwhile: `data-bridge-restarting`), until the next host's first snapshot:
|
|
4
|
+
// then it goes and a toast says Bridge restarted, or the page reloads when its own code changed with it (the host's
|
|
5
|
+
// "updated"), the reloaded page saying it. Called off by the host (`restarting` gone, same host), it just goes: the host's notice says why. Still
|
|
6
|
+
// restarting after `lateMs`, it says where the restart's log is, with Reload page.
|
|
7
|
+
import * as React from 'react';
|
|
8
|
+
import {
|
|
9
|
+
Button,
|
|
10
|
+
Dialog,
|
|
11
|
+
DialogBody,
|
|
12
|
+
DialogContent,
|
|
13
|
+
DialogDescription,
|
|
14
|
+
DialogFooter,
|
|
15
|
+
DialogHeader,
|
|
16
|
+
DialogTitle,
|
|
17
|
+
Icon,
|
|
18
|
+
useToast,
|
|
19
|
+
} from '@bridge/ui';
|
|
20
|
+
import type { Context } from 'cordis';
|
|
21
|
+
import type { HostStatusEvent } from './notifications.ts';
|
|
22
|
+
|
|
23
|
+
export type RestartPhase = 'idle' | 'restarting' | 'late';
|
|
24
|
+
/** Set before the page reloads itself after Bridge's restart: the reloaded page says Bridge restarted. */
|
|
25
|
+
const reloadedKey = 'bridge-restarted';
|
|
26
|
+
|
|
27
|
+
/** The listener for `hostStatus.subscribe`: `phase` on each change, `back` once the next host answered. */
|
|
28
|
+
export function restartTracker(
|
|
29
|
+
phase: (next: RestartPhase) => void,
|
|
30
|
+
back: () => void,
|
|
31
|
+
lateMs = 90_000,
|
|
32
|
+
/** Runs `run` after `ms`, returns what `cancel` takes (a test passes its own). */
|
|
33
|
+
later: (run: () => void, ms: number) => unknown = (run, ms) => setTimeout(run, ms),
|
|
34
|
+
cancel: (timer: unknown) => void = (timer) => clearTimeout(timer as number),
|
|
35
|
+
) {
|
|
36
|
+
let current: RestartPhase = 'idle';
|
|
37
|
+
let timer: unknown;
|
|
38
|
+
const set = (next: RestartPhase) => {
|
|
39
|
+
if (next === current) return;
|
|
40
|
+
current = next;
|
|
41
|
+
if (next !== 'late') cancel(timer);
|
|
42
|
+
phase(next);
|
|
43
|
+
};
|
|
44
|
+
return (event: HostStatusEvent) => {
|
|
45
|
+
if (event.event !== 'snapshot' && event.event !== 'code') return;
|
|
46
|
+
const code = event.data.code;
|
|
47
|
+
const now = code !== undefined && !('error' in code) && code.restarting === 'now';
|
|
48
|
+
if (now) {
|
|
49
|
+
if (current !== 'idle') return;
|
|
50
|
+
set('restarting');
|
|
51
|
+
timer = later(() => set('late'), lateMs);
|
|
52
|
+
} else if (current !== 'idle') {
|
|
53
|
+
// A snapshot is a new connection: the next host when the restart was under way. A `code` event is the same host
|
|
54
|
+
// calling it off.
|
|
55
|
+
if (event.event === 'snapshot') back();
|
|
56
|
+
set('idle');
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function RestartScreen({ hostStatus }: { hostStatus: Context['hostStatus'] }) {
|
|
62
|
+
const { toast } = useToast();
|
|
63
|
+
const [phase, setPhase] = React.useState<RestartPhase>('idle');
|
|
64
|
+
/** Restarting, or back a moment ago: a page whose code changed then reloads by itself, and the boot banner keeps
|
|
65
|
+
* quiet (`data-bridge-restarting`: its own "Bridge restarted" comes once the stream is back, after the snapshot). */
|
|
66
|
+
const reloads = React.useRef(false);
|
|
67
|
+
React.useEffect(() => {
|
|
68
|
+
const root = document.documentElement;
|
|
69
|
+
// Reloaded by the restart below (its code changed with Bridge's): it still says Bridge restarted.
|
|
70
|
+
if (sessionStorage.getItem(reloadedKey) !== null) {
|
|
71
|
+
sessionStorage.removeItem(reloadedKey);
|
|
72
|
+
toast({ variant: 'success', title: 'Bridge restarted' });
|
|
73
|
+
}
|
|
74
|
+
let quiet: ReturnType<typeof setTimeout> | undefined;
|
|
75
|
+
let unsettle = () => {};
|
|
76
|
+
/** The next host answered (`back` runs before the phase goes idle). */
|
|
77
|
+
let returned = false;
|
|
78
|
+
const off = hostStatus.subscribe(
|
|
79
|
+
restartTracker(
|
|
80
|
+
(next) => {
|
|
81
|
+
setPhase(next);
|
|
82
|
+
if (next === 'idle') {
|
|
83
|
+
// Called off: at once; back: the moment the boot takes to tell it.
|
|
84
|
+
if (!returned) {
|
|
85
|
+
reloads.current = false;
|
|
86
|
+
delete root.dataset.bridgeRestarting;
|
|
87
|
+
}
|
|
88
|
+
returned = false;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
clearTimeout(quiet);
|
|
92
|
+
reloads.current = true;
|
|
93
|
+
root.dataset.bridgeRestarting = '';
|
|
94
|
+
},
|
|
95
|
+
() => {
|
|
96
|
+
returned = true;
|
|
97
|
+
toast({ variant: 'success', title: 'Bridge restarted' });
|
|
98
|
+
// The page checks its hash only while shown: a hidden tab keeps waiting for it until it is shown again.
|
|
99
|
+
const settle = () => {
|
|
100
|
+
if (document.hidden) return;
|
|
101
|
+
document.removeEventListener('visibilitychange', settle);
|
|
102
|
+
quiet = setTimeout(() => {
|
|
103
|
+
reloads.current = false;
|
|
104
|
+
delete root.dataset.bridgeRestarting;
|
|
105
|
+
}, 10_000);
|
|
106
|
+
};
|
|
107
|
+
document.addEventListener('visibilitychange', settle);
|
|
108
|
+
unsettle = () => document.removeEventListener('visibilitychange', settle);
|
|
109
|
+
settle();
|
|
110
|
+
},
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
const offUpdated = hostStatus.pageUpdated?.(() => {
|
|
114
|
+
if (!reloads.current) return;
|
|
115
|
+
sessionStorage.setItem(reloadedKey, '');
|
|
116
|
+
location.reload();
|
|
117
|
+
});
|
|
118
|
+
return () => {
|
|
119
|
+
off();
|
|
120
|
+
offUpdated?.();
|
|
121
|
+
clearTimeout(quiet);
|
|
122
|
+
unsettle();
|
|
123
|
+
};
|
|
124
|
+
}, [hostStatus, toast]);
|
|
125
|
+
return (
|
|
126
|
+
<Dialog open={phase !== 'idle'} onOpenChange={() => {}}>
|
|
127
|
+
{/* A quick fade (and a slight zoom) in and out: Base UI's starting and ending styles. */}
|
|
128
|
+
<DialogContent
|
|
129
|
+
aria-live="polite"
|
|
130
|
+
className="transition-[opacity,scale] duration-150 ease-out data-[ending-style]:scale-95 data-[ending-style]:opacity-0 data-[starting-style]:scale-95 data-[starting-style]:opacity-0 motion-reduce:transition-none"
|
|
131
|
+
backdropClassName="transition-opacity duration-150 ease-out data-[ending-style]:opacity-0 data-[starting-style]:opacity-0 motion-reduce:transition-none"
|
|
132
|
+
>
|
|
133
|
+
<DialogHeader>
|
|
134
|
+
<DialogTitle>Restarting Bridge</DialogTitle>
|
|
135
|
+
</DialogHeader>
|
|
136
|
+
<DialogBody className="flex items-start gap-3 pb-4">
|
|
137
|
+
<Icon
|
|
138
|
+
name="loader-circle"
|
|
139
|
+
size={20}
|
|
140
|
+
className="mt-0.5 shrink-0 animate-spin text-secondary motion-reduce:animate-none"
|
|
141
|
+
/>
|
|
142
|
+
<DialogDescription>
|
|
143
|
+
{phase === 'late'
|
|
144
|
+
? 'Bridge has not come back yet. What its restart said is in logs/restart.log, in its root folder.'
|
|
145
|
+
: 'Bridge and every workspace start again with the new code. This page comes back by itself.'}
|
|
146
|
+
</DialogDescription>
|
|
147
|
+
</DialogBody>
|
|
148
|
+
{phase === 'late' && (
|
|
149
|
+
<DialogFooter className="justify-end">
|
|
150
|
+
<Button onClick={() => location.reload()}>Reload page</Button>
|
|
151
|
+
</DialogFooter>
|
|
152
|
+
)}
|
|
153
|
+
</DialogContent>
|
|
154
|
+
</Dialog>
|
|
155
|
+
);
|
|
156
|
+
}
|