File size: 2,663 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as api from "@/Api";
import { deleteAutoSave, stopAutoSaveTimer } from "@/Store/AutoSave";
import { unregisterWarnSaveOnClose } from "@/Core/SaveOnClose/BeforeUnload";
import { PopupVariant } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums";
import { closeElectron } from "../Electron/ElectronUtils";
import { getUrlParam } from "../UrlParams";
import { isTest } from "../GlobalVars";
/**
 * Reloads the page to the specified URL, but only if not in a test environment.
 *
 * @param {string | URL} url The URL to navigate to.
 */
export function reloadPage(url: string | URL): void {
    if (!isTest) {
        window.location.href = url.toString();
    }
}
/**
 * Constructs the base URL for reloading, optionally retaining the 'focus' parameter.
 *
 * @returns {string} The URL to navigate to.
 */
function _getReloadUrl(): string {
    const focusParamValue = getUrlParam("focus");
    let newUrl = window.location.origin + window.location.pathname;
    if (focusParamValue !== null) {
        newUrl += `?focus=${focusParamValue}`;
    }
    return newUrl;
}

/**
 * Closes down the app by hiding all elements with class hide-on-app, clearing
 * the autosave, unregistering the warn save on close event, reloading the page
 * if showMsg is true,and showing a message to the user.
 *
 * @param {string}  preMsg   Text to prepend to the message that will be shown
 *                           to the user.
 * @param {boolean} showMsg  Whether to show a message to the user.
 * @returns {Promise<void>}  A promise that resolves when the app is closed
 *     down.
 */
export async function closeDownApp(preMsg = "", showMsg = true): Promise<void> {
    // Get all elements with class hide-on-app-closed
    const elementsToHide = document.querySelectorAll(".hide-on-app-closed");
    elementsToHide.forEach((element: any) => {
        element.style.display = "none"; // Hide each element
    });
    // Clear the autosave
    stopAutoSaveTimer();
    await deleteAutoSave();
    unregisterWarnSaveOnClose();
    // Below will close the app if running in electron. So subsequent messages
    // is never shown.
    closeElectron();
    const reloadUrl = _getReloadUrl();
    if (showMsg) {
        api.messages.popupMessage(
            "Session Ended",
            preMsg + "You may now close/reload this tab/window.",
            PopupVariant.Info,
            () => {
                // Reload the page, preserving only the focus parameter if it exists.
                reloadPage(reloadUrl);
            }
        );
    } else {
        // Reload the page, preserving only the focus parameter if it exists.
        reloadPage(reloadUrl);
    }
}