okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-guidata (4c4596746) into f-0213-int22a3d7068

main
2.4 KiB65 linesraw
1/**
2 * After a crash of the page's renderer (#5079). The desktop shell loads the page again at `/?recovery=<n>`; the page then starts with the
3 * sidebar previews closed, once, so the file that may have caused the crash is not opened again by itself. The chats are the host's and
4 * are all still there; only what the sidebar showed is deferred, and only until the person opens a preview themselves.
5 */
6
7/** 1..9 from a `recovery=<n>` query, 0 when there is none or it is not a number. */
8export function parseRecovery(search: string): number {
9    const value = new URLSearchParams(search).get('recovery');
10    if (value === null || !/^[1-9]$/.test(value))
11        return 0;
12    return Number(value);
13}
14
15/** The query without the recovery parameter (and with its `?` only when something is left). */
16export function withoutRecovery(search: string): string {
17    const params = new URLSearchParams(search);
18    params.delete('recovery');
19    const rest = params.toString();
20    return rest ? `?${rest}` : '';
21}
22
23let deferred = false;
24let deferredSession: string | null = null;
25
26/**
27 * Read the shell's recovery mark from the address, remove it (a later reload of the page is an ordinary start), and defer the previews.
28 * Called once, before the first render. Returns the recovery number, 0 for an ordinary start.
29 */
30export function startRecovery(): number {
31    let level = 0;
32    try {
33        level = parseRecovery(window.location.search);
34        if (level) {
35            const { pathname, search, hash } = window.location;
36            window.history.replaceState(window.history.state, '', pathname + withoutRecovery(search) + hash);
37        }
38    }
39    catch { /* no address to read: an ordinary start */ }
40    deferred = level > 0;
41    deferredSession = null;
42    return level;
43}
44
45/**
46 * True while the sidebar previews stay closed after a recovery: for the chat that was open when the renderer died, until the person
47 * opens a preview themselves or goes to another chat. The first chat asked about is that chat.
48 */
49export function previewsDeferred(sessionId?: string): boolean {
50    if (!deferred)
51        return false;
52    if (sessionId === undefined)
53        return true;
54    deferredSession ??= sessionId;
55    if (deferredSession !== sessionId) {
56        deferred = false;
57        return false;
58    }
59    return true;
60}
61
62/** The person opened a preview themselves: from now on previews open as usual. */
63export function endPreviewDeferral(): void {
64    deferred = false;
65}