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/compute/cloud.tsx
Views: 687
1
import {
2
Cloud as CloudType,
3
State,
4
} from "@cocalc/util/db-schema/compute-servers";
5
import { CLOUDS_BY_NAME } from "@cocalc/util/compute/cloud/clouds";
6
import { Select, Space, Spin, Tooltip } from "antd";
7
import { useEffect, useState } from "react";
8
import { setServerCloud } from "./api";
9
import { useStore } from "@cocalc/frontend/app-framework";
10
import DisplayCloud from "./display-cloud";
11
12
interface Props {
13
cloud: CloudType;
14
state?: State;
15
editable?: boolean;
16
height?: number | string;
17
setError?;
18
id?: number;
19
setCloud?: (cloud: CloudType) => void;
20
style?;
21
onChange?;
22
}
23
24
export default function Cloud({
25
cloud,
26
state,
27
editable,
28
height,
29
setError,
30
id,
31
setCloud,
32
style,
33
}: Props) {
34
const [newCloud, setNewCloud] = useState<CloudType>(cloud);
35
const [saving, setSaving] = useState<boolean>(false);
36
const customize = useStore("customize");
37
useEffect(() => {
38
setNewCloud(cloud);
39
}, [cloud]);
40
41
const label = (
42
<DisplayCloud
43
cloud={cloud}
44
height={height}
45
style={!editable ? style : undefined}
46
/>
47
);
48
if (!editable) {
49
return label;
50
}
51
52
const options: { value: string; label: JSX.Element; key: string }[] = [];
53
for (const cloud in CLOUDS_BY_NAME) {
54
if (customize?.get(`compute_servers_${cloud}_enabled`)) {
55
options.push({
56
key: cloud,
57
value: cloud,
58
label: <Cloud editable={false} cloud={cloud as CloudType} />,
59
});
60
}
61
}
62
63
if (state != "deprovisioned" && setCloud == null) {
64
return (
65
<Tooltip
66
title="You must first deprovision the compute server VM before you can change the
67
cloud provider."
68
>
69
<span>{label}</span>
70
</Tooltip>
71
);
72
}
73
74
return (
75
<Space>
76
<Select
77
value={newCloud}
78
style={{ width: 180, ...style }}
79
onChange={async (value) => {
80
if (value == newCloud) {
81
// nothing to do
82
return;
83
}
84
setNewCloud(value);
85
if (setCloud != null) {
86
setCloud(value);
87
}
88
if (id) {
89
// save to backend
90
try {
91
setSaving(true);
92
await setServerCloud({ cloud: value, id });
93
} catch (err) {
94
setError(`${err}`);
95
} finally {
96
setSaving(false);
97
}
98
}
99
}}
100
options={options}
101
/>
102
{saving && <Spin />}
103
</Space>
104
);
105
}
106
107