Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/app/render.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import ReactDOM from "react-dom";6import { createRoot } from "react-dom/client";78import {9redux,10Redux,11useAsyncEffect,12useTypedRedux,13} from "@cocalc/frontend/app-framework";14import {15getLocale,16LOCALIZATIONS,17OTHER_SETTINGS_LOCALE_KEY,18} from "@cocalc/frontend/i18n";19import { QueryParams } from "@cocalc/frontend/misc/query-params";20import { AppContext, useAppContextProvider } from "./context";21import { Localize, useLocalizationCtx } from "./localize";2223// App uses the context provided by Redux (for the locale, etc.) and Localize.24function App({ children }) {25const appState = useAppContextProvider();26const { setLocale } = useLocalizationCtx();27const other_settings = useTypedRedux("account", "other_settings");2829// setting via ?lang=[locale] takes precedece over account settings30// additionally ?lang_temp=[locale] temporarily changes it, used by these impersonation admin links31useAsyncEffect(async () => {32const lang_set = QueryParams.get("lang");33// lang_temp sets the language *temporarily*, i.e. without changing the account settings and it is sticky34// this is useful for impersonation – https://github.com/sagemathinc/cocalc/issues/778235const lang_temp = QueryParams.get("lang_temp");36const temp = lang_temp != null;37const lang = temp ? lang_temp : lang_set;38if (lang != null) {39if (lang in LOCALIZATIONS) {40console.warn(41`URL query parameter 'lang=${lang}' – overriding user configuration ${42temp ? "temporary" : "permanent"43}.`,44);45if (!temp) {46const store = redux.getStore("account");47// we have to ensure the account store is available, because this code runs very early48await store.async_wait({49until: () => store.get_account_id() != null,50});51redux52.getActions("account")53.set_other_settings(OTHER_SETTINGS_LOCALE_KEY, lang);54}55setLocale(lang);56} else {57console.warn(58`URL query parameter '${JSON.stringify({59lang_set,60lang_temp,61})}' provided, but not a valid locale.`,62`Known values: ${Object.keys(LOCALIZATIONS)}`,63);64}65if (!temp) {66// removing the parameter, otherwise this conflicts with further changes of account settings67QueryParams.remove("lang");68}69} else {70setLocale(getLocale(other_settings));71}72}, [getLocale(other_settings)]);7374return <AppContext.Provider value={appState}>{children}</AppContext.Provider>;75}7677function Root({ Page }) {78return (79<Redux>80<Localize>81<App>82<Page />83</App>84</Localize>85</Redux>86);87}8889export async function render(): Promise<void> {90finishedLoading(); // comment this out to leave the loading/startup banner visible so you can use the Chrome dev tools with it.91const container = document.getElementById("cocalc-webapp-container");92const root = createRoot(container!);93const { Page } = await import("./page");94root.render(<Root Page={Page} />);95}9697export async function xxx_render(): Promise<void> {98finishedLoading(); // comment this out to leave the loading/startup banner visible99const { Page } = await import("./page");100ReactDOM.render(101<Root Page={Page} />,102document.getElementById("cocalc-webapp-container"),103);104}105106// When loading is done, remove any visible artifacts.107// This doesn't remove anything added to the head.108function finishedLoading() {109const load = document.getElementById("cocalc-load-container");110if (load != null) {111load.innerHTML = "";112load.remove();113}114}115116117