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/course/configuration/envvars-config.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// This configures the datastore configuration for student and the shared project.6// basically: if it is "true", the datastore config of the teacher project is looked up when the project starts7// and used to configure it in read-only mode. In the future, a natural extension is to explicitly list the datastores8// that should be inherited, or configure the readonly property. but for now, it's just true or false.910import { redux, useTypedRedux, TypedMap } from "@cocalc/frontend/app-framework";11import { useEffect, useState } from "react";12import { ConfigurationActions } from "./actions";13import { Card, Typography, Switch, Form, Button } from "antd";14import { EnvVars, EnvVarsRecord } from "@cocalc/frontend/projects/actions";15import {16KUCALC_COCALC_COM,17KUCALC_ON_PREMISES,18} from "@cocalc/util/db-schema/site-defaults";19import { Icon } from "@cocalc/frontend/components";20import { ENV_VARS_ICON } from "@cocalc/frontend/project/settings/environment";2122const ENVVARS_DEFAULT = false;2324interface Props {25project_id: string;26actions: ConfigurationActions;27envvars?: EnvVars | TypedMap<EnvVarsRecord>;28close?: Function;29}3031function normalizeTypeAndValue(32envvars: EnvVars | TypedMap<EnvVarsRecord>,33): NonNullable<EnvVars> {34if (typeof (envvars as any)?.inherit === "boolean") {35return envvars as NonNullable<EnvVars>;36}37if (typeof (envvars as any)?.toJS === "function") {38return normalizeTypeAndValue((envvars as TypedMap<EnvVarsRecord>).toJS());39}40return { inherit: ENVVARS_DEFAULT };41}4243export function EnvironmentVariablesConfig({44actions,45envvars,46close,47project_id,48}: Props) {49const envvars1 = normalizeTypeAndValue(envvars);50const customize_kucalc = useTypedRedux("customize", "kucalc");51const [needSave, setNeedSave] = useState<boolean>(false);5253// By default, we inherit the environment variables.54// As of this, we only support true/false.55const inherit = envvars1.inherit ?? ENVVARS_DEFAULT;56const [nextVal, setNextVal] = useState<boolean>(inherit);5758useEffect(() => {59setNeedSave(nextVal != inherit);60}, [nextVal, inherit]);6162useEffect(() => {63// needed because of realtime collaboration, multiple frames, modal, etc!64setNextVal(inherit);65}, [inherit]);6667// this selector only make sense for cocalc.com and cocalc-onprem68if (69customize_kucalc !== KUCALC_COCALC_COM &&70customize_kucalc !== KUCALC_ON_PREMISES71)72return null;7374function toggle() {75return (76<Form layout="inline">77<Form.Item label="Inherit settings:" style={{ marginBottom: 0 }}>78<Switch checked={nextVal} onChange={(val) => setNextVal(val)} />79</Form.Item>80<Form.Item>81<Button82disabled={!needSave}83type={needSave ? "primary" : "default"}84onClick={() => {85actions.set_envvars(nextVal);86close?.();87}}88>89Save90</Button>91</Form.Item>92</Form>93);94}9596return (97<Card98title={99<>100<Icon name={ENV_VARS_ICON} /> Inherit Environment Variables101</>102}103>104<p>105If enabled, all student projects inherit the{" "}106<Typography.Text strong>environment variables</Typography.Text> of this107instructor project.108</p>109<p>110To configure them, please check{" "}111<a112onClick={() => {113redux.getProjectActions(project_id).set_active_tab("settings");114close?.();115}}116>117this project's settings118</a>{" "}119for more details. Changes to the configuration of this project will only120be reflected after the next start of a student project.121</p>122<p>123Note: environment variables from the instructor project overwrite124anything configured in the student project, as you can confirm by125looking at the settings of the student project after making this change126and configuring all student projects.127</p>128{toggle()}129</Card>130);131}132133134