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-filesystem/mount-filesystem.tsx
Views: 687
1
import { Alert, Button, Modal, Spin } from "antd";
2
import { useEffect, useRef, useState } from "react";
3
4
import { A } from "@cocalc/frontend/components/A";
5
import ShowError from "@cocalc/frontend/components/error";
6
import { Icon } from "@cocalc/frontend/components/icon";
7
import { checkInAll } from "@cocalc/frontend/compute/check-in";
8
import { CancelText } from "@cocalc/frontend/i18n/components";
9
import type { CloudFilesystem } from "@cocalc/util/db-schema/cloud-filesystems";
10
import { editCloudFilesystem } from "./api";
11
import { Mountpoint } from "./cloud-filesystem";
12
import { editModalStyle } from "./util";
13
14
interface Props {
15
cloudFilesystem: CloudFilesystem;
16
open?: boolean;
17
setOpen;
18
refresh;
19
}
20
21
// Actually toggles Mount status
22
export default function MountCloudFilesystem({
23
cloudFilesystem,
24
open,
25
setOpen,
26
refresh,
27
}: Props) {
28
const [mounting, setMounting] = useState<boolean>(false);
29
const [error, setError] = useState<string>("");
30
const buttonRef = useRef<any>(null);
31
32
useEffect(() => {
33
if (!cloudFilesystem.mount) {
34
// only focus for mounting (the non-dangerous one)
35
setTimeout(() => {
36
buttonRef.current?.focus();
37
}, 300);
38
}
39
}, []);
40
41
const doToggleMount = async () => {
42
try {
43
setMounting(true);
44
await editCloudFilesystem({
45
id: cloudFilesystem.id,
46
mount: !cloudFilesystem.mount,
47
});
48
checkInAll(cloudFilesystem.project_id);
49
refresh();
50
setOpen(false);
51
} catch (err) {
52
setError(`${err}`);
53
} finally {
54
setMounting(false);
55
}
56
};
57
const icon = cloudFilesystem.mount ? "stop" : "run";
58
const verb = cloudFilesystem.mount
59
? "Unmount and Disable Automount"
60
: "Mount and Enable Automount";
61
62
return (
63
<Modal
64
styles={{ body: editModalStyle(cloudFilesystem) }}
65
centered
66
title={
67
<>
68
<Icon name={icon} /> {verb} "{cloudFilesystem.title}"{" "}
69
{cloudFilesystem.mount ? "from" : "at"}{" "}
70
<Mountpoint {...cloudFilesystem} />?
71
</>
72
}
73
open={open}
74
onCancel={() => setOpen(false)}
75
footer={[
76
<Button key="cancel" onClick={() => setOpen(false)}>
77
<CancelText />
78
</Button>,
79
<Button
80
ref={buttonRef}
81
key="ok"
82
type={cloudFilesystem.mount ? "default" : "primary"}
83
danger={cloudFilesystem.mount}
84
disabled={mounting}
85
onClick={doToggleMount}
86
>
87
<Icon name={icon} /> {verb}{" "}
88
{mounting ? <Spin style={{ marginLeft: "15px" }} /> : undefined}
89
</Button>,
90
]}
91
>
92
<div>
93
<p>
94
Are you sure you want to{" "}
95
{cloudFilesystem.mount ? "unmount" : "automount"} this cloud
96
filesystem?
97
{cloudFilesystem.mount
98
? " The file system may be currently mounted so make sure no applications have anything in this filesystem open to avoid data loss. "
99
: " "}
100
{cloudFilesystem.mount ? "Unmounting" : "Automatic mounting"}{" "}
101
typically takes about <b>15 seconds</b>.
102
</p>
103
<Alert
104
showIcon
105
style={{ margin: "10px 0" }}
106
type="warning"
107
message={<b>Cloud File Systems Only Visible From Compute Servers</b>}
108
description={
109
<>
110
Currently cloud file systems can only be used from compute
111
servers, i.e., from a Jupyter notebook or terminal that is set to
112
use a compute server or from the file browser set to explore a
113
compute server.
114
</>
115
}
116
/>
117
{!cloudFilesystem.mount && (
118
<p style={{ color: "#666" }}>
119
<b>WARNING:</b> When a cloud file system is first created or has not
120
been used for a while, it can take several minutes to automount in a
121
running project while{" "}
122
<A href="https://cloud.google.com/iam/docs/access-change-propagation">
123
security policies
124
</A>{" "}
125
propagate.
126
</p>
127
)}
128
</div>
129
<ShowError error={error} setError={setError} />
130
</Modal>
131
);
132
}
133
134