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/account/init.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { deep_copy } from "@cocalc/util/misc";6import { SCHEMA } from "@cocalc/util/schema";7import { webapp_client } from "../webapp-client";8import { AccountActions } from "./actions";9import { AccountStore } from "./store";10import { AccountTable } from "./table";11import { init_dark_mode } from "./dark-mode";12import { reset_password_key } from "../client/password-reset";13import { hasRememberMe } from "@cocalc/frontend/misc/remember-me";14import { appBasePath } from "@cocalc/frontend/customize/app-base-path";1516export function init(redux) {17// Register account store18// Use the database defaults for all account info until this gets set after they login19const init = deep_copy(SCHEMA.accounts.user_query?.get?.fields) ?? {};20// ... except for show_global_info2 (null or a timestamp)21// REGISTER and STRATEGIES are injected in app.html via the /customize endpoint -- do not delete them!22init.token = global["REGISTER"];23init.strategies = global["STRATEGIES"];24init.other_settings.show_global_info2 = "loading"; // indicates there is no data yet25init.editor_settings.physical_keyboard = "NO_DATA"; // indicator that there is no data26init.user_type = hasRememberMe(appBasePath) ? "signing_in" : "public"; // default27const store = redux.createStore("account", AccountStore, init);28const actions = redux.createActions("account", AccountActions);2930actions._init(store);31init_dark_mode(store);3233redux.createTable("account", AccountTable);34redux.getTable("account")._table.on("error", (tableError) => {35actions.setState({ tableError });36});37redux.getTable("account")._table.on("clear-error", () => {38actions.setState({ tableError: undefined });39});4041// Password reset42actions.setState({ reset_key: reset_password_key() });4344// Login status45webapp_client.on("signed_in", function (mesg) {46if (mesg?.api_key) {47// wait for sign in to finish and cookie to get set, then redirect48setTimeout(() => {49window.location.href = `https://authenticated?api_key=${mesg.api_key}`;50}, 2000);51}52redux.getActions("account").set_user_type("signed_in");53});5455webapp_client.on("signed_out", () =>56redux.getActions("account").set_user_type("public")57);5859webapp_client.on("remember_me_failed", () =>60redux.getActions("account").set_user_type("public")61);6263// Autosave interval64let _autosave_interval: NodeJS.Timeout | undefined = undefined;65const init_autosave = function (autosave) {66if (_autosave_interval) {67// This function can safely be called again to *adjust* the68// autosave interval, in case user changes the settings.69clearInterval(_autosave_interval);70_autosave_interval = undefined;71}7273// Use the most recent autosave value.74if (autosave) {75const save_all_files = function () {76if (webapp_client.is_connected()) {77redux.getActions("projects")?.save_all_files();78}79};80_autosave_interval = setInterval(save_all_files, autosave * 1000);81}82};8384let _last_autosave_interval_s = undefined;85store.on("change", function () {86const interval_s = store.get("autosave");87if (interval_s !== _last_autosave_interval_s) {88_last_autosave_interval_s = interval_s;89init_autosave(interval_s);90}91});9293// Standby timeout94let last_set_standby_timeout_m = undefined;95store.on("change", function () {96// NOTE: we call this on any change to account settings, which is maybe too extreme.97const x = store.getIn(["other_settings", "standby_timeout_m"]);98if (last_set_standby_timeout_m !== x) {99last_set_standby_timeout_m = x;100webapp_client.idle_client.set_standby_timeout_m(x);101}102});103}104105106