CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/account/init.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { deep_copy } from "@cocalc/util/misc";
7
import { SCHEMA } from "@cocalc/util/schema";
8
import { webapp_client } from "../webapp-client";
9
import { AccountActions } from "./actions";
10
import { AccountStore } from "./store";
11
import { AccountTable } from "./table";
12
import { init_dark_mode } from "./dark-mode";
13
import { reset_password_key } from "../client/password-reset";
14
import { hasRememberMe } from "@cocalc/frontend/misc/remember-me";
15
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
16
17
export function init(redux) {
18
// Register account store
19
// Use the database defaults for all account info until this gets set after they login
20
const init = deep_copy(SCHEMA.accounts.user_query?.get?.fields) ?? {};
21
// ... except for show_global_info2 (null or a timestamp)
22
// REGISTER and STRATEGIES are injected in app.html via the /customize endpoint -- do not delete them!
23
init.token = global["REGISTER"];
24
init.strategies = global["STRATEGIES"];
25
init.other_settings.show_global_info2 = "loading"; // indicates there is no data yet
26
init.editor_settings.physical_keyboard = "NO_DATA"; // indicator that there is no data
27
init.user_type = hasRememberMe(appBasePath) ? "signing_in" : "public"; // default
28
const store = redux.createStore("account", AccountStore, init);
29
const actions = redux.createActions("account", AccountActions);
30
31
actions._init(store);
32
init_dark_mode(store);
33
34
redux.createTable("account", AccountTable);
35
redux.getTable("account")._table.on("error", (tableError) => {
36
actions.setState({ tableError });
37
});
38
redux.getTable("account")._table.on("clear-error", () => {
39
actions.setState({ tableError: undefined });
40
});
41
42
// Password reset
43
actions.setState({ reset_key: reset_password_key() });
44
45
// Login status
46
webapp_client.on("signed_in", function (mesg) {
47
if (mesg?.api_key) {
48
// wait for sign in to finish and cookie to get set, then redirect
49
setTimeout(() => {
50
window.location.href = `https://authenticated?api_key=${mesg.api_key}`;
51
}, 2000);
52
}
53
redux.getActions("account").set_user_type("signed_in");
54
});
55
56
webapp_client.on("signed_out", () =>
57
redux.getActions("account").set_user_type("public")
58
);
59
60
webapp_client.on("remember_me_failed", () =>
61
redux.getActions("account").set_user_type("public")
62
);
63
64
// Autosave interval
65
let _autosave_interval: NodeJS.Timeout | undefined = undefined;
66
const init_autosave = function (autosave) {
67
if (_autosave_interval) {
68
// This function can safely be called again to *adjust* the
69
// autosave interval, in case user changes the settings.
70
clearInterval(_autosave_interval);
71
_autosave_interval = undefined;
72
}
73
74
// Use the most recent autosave value.
75
if (autosave) {
76
const save_all_files = function () {
77
if (webapp_client.is_connected()) {
78
redux.getActions("projects")?.save_all_files();
79
}
80
};
81
_autosave_interval = setInterval(save_all_files, autosave * 1000);
82
}
83
};
84
85
let _last_autosave_interval_s = undefined;
86
store.on("change", function () {
87
const interval_s = store.get("autosave");
88
if (interval_s !== _last_autosave_interval_s) {
89
_last_autosave_interval_s = interval_s;
90
init_autosave(interval_s);
91
}
92
});
93
94
// Standby timeout
95
let last_set_standby_timeout_m = undefined;
96
store.on("change", function () {
97
// NOTE: we call this on any change to account settings, which is maybe too extreme.
98
const x = store.getIn(["other_settings", "standby_timeout_m"]);
99
if (last_set_standby_timeout_m !== x) {
100
last_set_standby_timeout_m = x;
101
webapp_client.idle_client.set_standby_timeout_m(x);
102
}
103
});
104
}
105
106