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/app/query-params.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
// Initialize various things related to the overall page and query params (e.g., fullscreen).
7
import { redux } from "@cocalc/frontend/app-framework";
8
import target from "@cocalc/frontend/client/handle-target";
9
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
10
import { COCALC_FULLSCREEN } from "@cocalc/frontend/fullscreen";
11
import { parse_target } from "@cocalc/frontend/history";
12
import {
13
get_local_storage,
14
set_local_storage,
15
} from "@cocalc/frontend/misc/local-storage";
16
import { QueryParams } from "@cocalc/frontend/misc/query-params";
17
import { is_valid_uuid_string } from "@cocalc/util/misc";
18
19
export function init_query_params(): void {
20
const actions = redux.getActions("page");
21
// enable fullscreen mode upon loading a URL like /app?fullscreen and
22
// additionally kiosk-mode upon /app?fullscreen=kiosk
23
if (COCALC_FULLSCREEN === "kiosk") {
24
actions.set_fullscreen("kiosk");
25
// We also check if user is loading a specific project in kiosk mode
26
// (which is the only thing they should ever do!), and in that
27
// case we record the project_id, so that we can make various
28
// query optimizations elsewhere.
29
const x = parse_target(target);
30
if (x.page === "project" && x.target != null) {
31
const kiosk_project_id = x.target.slice(0, 36);
32
if (is_valid_uuid_string(kiosk_project_id)) {
33
actions.setState({ kiosk_project_id });
34
}
35
}
36
} else if (COCALC_FULLSCREEN === "default") {
37
actions.set_fullscreen("default");
38
// We no longer need fullscreen in the query parameter:
39
QueryParams.remove("fullscreen");
40
} else if (COCALC_FULLSCREEN === "project") {
41
actions.set_fullscreen("project");
42
}
43
44
const get_api_key_query_value = QueryParams.get("get_api_key");
45
if (get_api_key_query_value) {
46
actions.set_get_api_key(get_api_key_query_value);
47
actions.set_fullscreen("project");
48
}
49
50
// configure the session
51
// This makes it so the default session is 'default' and there is no
52
// way to NOT have a session, except via session=, which is treated
53
// as "no session" (also no session for kiosk mode).
54
// Note that we never have a session in kiosk mode, since you can't
55
// access the other files.
56
// If click on link with ?session=, then you get no session, e.g,. this
57
// is used for doing a pop-out of a single file. Should have no impact
58
// on sessions at all.
59
if (COCALC_FULLSCREEN === "kiosk") {
60
actions.set_session(""); // no session
61
} else {
62
const key = `session${appBasePath}`;
63
const querySession = QueryParams.get("session");
64
let session: any = querySession ?? get_local_storage(key) ?? "default";
65
66
if (typeof session != "string") {
67
// should never happen, but of course it could since user could put anything in URL query params
68
// We just reset to default in this case.
69
session = "default";
70
}
71
actions.set_session(session);
72
if (session) {
73
// So when you don't give session= param in this browser in the future
74
// it defaults to the one you did use (or "default").
75
set_local_storage(key, session);
76
}
77
}
78
// Do not need or want it in our URL once we've consumed it. Critical to
79
// not have session in the URL, so we can share url's without infected
80
// other user's session.
81
QueryParams.remove("session");
82
}
83
84