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/course/configuration/envvars-config.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// This configures the datastore configuration for student and the shared project.
7
// basically: if it is "true", the datastore config of the teacher project is looked up when the project starts
8
// and used to configure it in read-only mode. In the future, a natural extension is to explicitly list the datastores
9
// that should be inherited, or configure the readonly property. but for now, it's just true or false.
10
11
import { redux, useTypedRedux, TypedMap } from "@cocalc/frontend/app-framework";
12
import { useEffect, useState } from "react";
13
import { ConfigurationActions } from "./actions";
14
import { Card, Typography, Switch, Form, Button } from "antd";
15
import { EnvVars, EnvVarsRecord } from "@cocalc/frontend/projects/actions";
16
import {
17
KUCALC_COCALC_COM,
18
KUCALC_ON_PREMISES,
19
} from "@cocalc/util/db-schema/site-defaults";
20
import { Icon } from "@cocalc/frontend/components";
21
import { ENV_VARS_ICON } from "@cocalc/frontend/project/settings/environment";
22
23
const ENVVARS_DEFAULT = false;
24
25
interface Props {
26
project_id: string;
27
actions: ConfigurationActions;
28
envvars?: EnvVars | TypedMap<EnvVarsRecord>;
29
close?: Function;
30
}
31
32
function normalizeTypeAndValue(
33
envvars: EnvVars | TypedMap<EnvVarsRecord>,
34
): NonNullable<EnvVars> {
35
if (typeof (envvars as any)?.inherit === "boolean") {
36
return envvars as NonNullable<EnvVars>;
37
}
38
if (typeof (envvars as any)?.toJS === "function") {
39
return normalizeTypeAndValue((envvars as TypedMap<EnvVarsRecord>).toJS());
40
}
41
return { inherit: ENVVARS_DEFAULT };
42
}
43
44
export function EnvironmentVariablesConfig({
45
actions,
46
envvars,
47
close,
48
project_id,
49
}: Props) {
50
const envvars1 = normalizeTypeAndValue(envvars);
51
const customize_kucalc = useTypedRedux("customize", "kucalc");
52
const [needSave, setNeedSave] = useState<boolean>(false);
53
54
// By default, we inherit the environment variables.
55
// As of this, we only support true/false.
56
const inherit = envvars1.inherit ?? ENVVARS_DEFAULT;
57
const [nextVal, setNextVal] = useState<boolean>(inherit);
58
59
useEffect(() => {
60
setNeedSave(nextVal != inherit);
61
}, [nextVal, inherit]);
62
63
useEffect(() => {
64
// needed because of realtime collaboration, multiple frames, modal, etc!
65
setNextVal(inherit);
66
}, [inherit]);
67
68
// this selector only make sense for cocalc.com and cocalc-onprem
69
if (
70
customize_kucalc !== KUCALC_COCALC_COM &&
71
customize_kucalc !== KUCALC_ON_PREMISES
72
)
73
return null;
74
75
function toggle() {
76
return (
77
<Form layout="inline">
78
<Form.Item label="Inherit settings:" style={{ marginBottom: 0 }}>
79
<Switch checked={nextVal} onChange={(val) => setNextVal(val)} />
80
</Form.Item>
81
<Form.Item>
82
<Button
83
disabled={!needSave}
84
type={needSave ? "primary" : "default"}
85
onClick={() => {
86
actions.set_envvars(nextVal);
87
close?.();
88
}}
89
>
90
Save
91
</Button>
92
</Form.Item>
93
</Form>
94
);
95
}
96
97
return (
98
<Card
99
title={
100
<>
101
<Icon name={ENV_VARS_ICON} /> Inherit Environment Variables
102
</>
103
}
104
>
105
<p>
106
If enabled, all student projects inherit the{" "}
107
<Typography.Text strong>environment variables</Typography.Text> of this
108
instructor project.
109
</p>
110
<p>
111
To configure them, please check{" "}
112
<a
113
onClick={() => {
114
redux.getProjectActions(project_id).set_active_tab("settings");
115
close?.();
116
}}
117
>
118
this project's settings
119
</a>{" "}
120
for more details. Changes to the configuration of this project will only
121
be reflected after the next start of a student project.
122
</p>
123
<p>
124
Note: environment variables from the instructor project overwrite
125
anything configured in the student project, as you can confirm by
126
looking at the settings of the student project after making this change
127
and configuring all student projects.
128
</p>
129
{toggle()}
130
</Card>
131
);
132
}
133
134