Files
ThreadNet-Web/src/vector/init.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

135 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-03-25 13:53:28 +00:00
/*
2024-09-06 15:44:31 +01:00
Copyright 2018-2024 New Vector Ltd.
2020-03-25 13:53:28 +00:00
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
2024-09-06 15:44:31 +01:00
Copyright 2017 Vector Creations Ltd
Copyright 2015, 2016 OpenMarket Ltd
2020-03-25 13:53:28 +00:00
2024-09-06 15:44:31 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
2020-03-25 13:53:28 +00:00
*/
2020-04-05 00:05:59 +01:00
import * as ReactDOM from "react-dom";
2020-04-08 19:47:52 +01:00
import * as React from "react";
import * as languageHandler from "matrix-react-sdk/src/languageHandler";
2020-03-25 13:55:25 +00:00
import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore";
import PlatformPeg from "matrix-react-sdk/src/PlatformPeg";
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
2021-06-30 13:28:31 +01:00
import { setTheme } from "matrix-react-sdk/src/theme";
2021-10-15 16:56:22 +02:00
import { logger } from "matrix-js-sdk/src/logger";
import { ModuleRunner } from "matrix-react-sdk/src/modules/ModuleRunner";
import MatrixChat from "matrix-react-sdk/src/components/structures/MatrixChat";
2021-10-15 16:56:22 +02:00
import ElectronPlatform from "./platform/ElectronPlatform";
import PWAPlatform from "./platform/PWAPlatform";
import WebPlatform from "./platform/WebPlatform";
import { initRageshake, initRageshakeStore } from "./rageshakesetup";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - this path is created at runtime and therefore won't exist at typecheck time
import { INSTALLED_MODULES } from "../modules";
export const rageshakePromise = initRageshake();
2020-03-25 13:55:25 +00:00
export function preparePlatform(): void {
2021-01-13 15:39:18 +00:00
if (window.electron) {
2021-10-15 16:59:13 +02:00
logger.log("Using Electron platform");
PlatformPeg.set(new ElectronPlatform());
} else if (window.matchMedia("(display-mode: standalone)").matches) {
2021-10-15 16:59:13 +02:00
logger.log("Using PWA platform");
PlatformPeg.set(new PWAPlatform());
} else {
2021-10-15 16:59:13 +02:00
logger.log("Using Web platform");
PlatformPeg.set(new WebPlatform());
}
}
export function setupLogStorage(): Promise<void> {
if (SdkConfig.get().bug_report_endpoint_url) {
return initRageshakeStore();
}
2021-10-15 17:00:43 +02:00
logger.warn("No bug report endpoint set - logs will not be persisted");
return Promise.resolve();
}
export async function loadConfig(): Promise<void> {
2020-04-08 19:35:16 +01:00
// XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure
// granular settings are loaded correctly and to avoid duplicating the override logic for the theme.
//
// Note: this isn't called twice for some wrappers, like the Jitsi wrapper.
const platformConfig = await PlatformPeg.get()?.getConfig();
if (platformConfig) {
SdkConfig.put(platformConfig);
} else {
2023-04-26 11:12:57 +01:00
SdkConfig.reset();
}
}
export async function loadLanguage(): Promise<void> {
2020-03-25 13:55:25 +00:00
const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/ true);
let langs: string[] = [];
2020-03-25 13:55:25 +00:00
if (!prefLang) {
languageHandler.getLanguagesFromBrowser().forEach((l) => {
langs.push(...languageHandler.getNormalizedLanguageKeys(l));
});
} else {
langs = [prefLang];
}
try {
await languageHandler.setLanguage(langs);
document.documentElement.setAttribute("lang", languageHandler.getCurrentLanguage());
} catch (e) {
2021-10-15 16:56:22 +02:00
logger.error("Unable to set language", e);
2020-03-25 13:55:25 +00:00
}
}
2020-04-04 17:21:59 +01:00
export async function loadTheme(): Promise<void> {
2024-06-12 17:17:29 +01:00
return setTheme();
2020-04-08 16:17:46 +01:00
}
export async function loadApp(fragParams: {}): Promise<void> {
2020-04-04 23:42:19 +01:00
// load app.js async so that its code is not executed immediately and we can catch any exceptions
2020-04-04 17:21:59 +01:00
const module = await import(
/* webpackChunkName: "element-web-app" */
2020-04-04 17:21:59 +01:00
/* webpackPreload: true */
"./app"
);
function setWindowMatrixChat(matrixChat: MatrixChat): void {
window.matrixChat = matrixChat;
}
ReactDOM.render(await module.loadApp(fragParams, setWindowMatrixChat), document.getElementById("matrixchat"));
2020-04-04 17:21:59 +01:00
}
2020-04-08 19:47:52 +01:00
export async function showError(title: string, messages?: string[]): Promise<void> {
const { ErrorView } = await import(
/* webpackChunkName: "error-view" */
"../async-components/structures/ErrorView"
);
2020-04-08 20:03:45 +01:00
window.matrixChat = ReactDOM.render(
<ErrorView title={title} messages={messages} />,
2020-04-08 19:47:52 +01:00
document.getElementById("matrixchat"),
);
}
export async function showIncompatibleBrowser(onAccept: () => void): Promise<void> {
const { UnsupportedBrowserView } = await import(
/* webpackChunkName: "error-view" */
"../async-components/structures/ErrorView"
);
window.matrixChat = ReactDOM.render(
<UnsupportedBrowserView onAccept={onAccept} />,
document.getElementById("matrixchat"),
);
}
export async function loadModules(): Promise<void> {
for (const InstalledModule of INSTALLED_MODULES) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - we know the constructor exists even if TypeScript can't be convinced of that
ModuleRunner.instance.registerModule((api) => new InstalledModule(api));
}
}
export { _t } from "../languageHandler";
export { extractErrorMessageFromError } from "matrix-react-sdk/src/components/views/dialogs/ErrorDialog";