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/account/public-paths/unpublish-everything.tsx
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
import { Alert, Button, Input } from "antd";
7
import { useIntl } from "react-intl";
8
9
import {
10
React,
11
useIsMountedRef,
12
useMemo,
13
useState,
14
} from "@cocalc/frontend/app-framework";
15
import { webapp_client } from "@cocalc/frontend/webapp-client";
16
import { PublicPath } from "@cocalc/util/db-schema/public-paths";
17
import { plural } from "@cocalc/util/misc";
18
19
interface Props {
20
data?: PublicPath[];
21
refresh: Function;
22
}
23
24
export const UnpublishEverything: React.FC<Props> = React.memo(
25
({ data, refresh }) => {
26
const intl = useIntl();
27
const [confirm, set_confirm] = useState<boolean>(false);
28
const [confirm_text, set_confirm_text] = useState<string>("");
29
const [counter, set_counter] = useState<number>(-1);
30
const isMountedRef = useIsMountedRef();
31
32
const unpublishEverything = intl.formatMessage({
33
id: "account.public-path.unpublish.title",
34
defaultMessage: "Unpublish Everything",
35
});
36
37
const num_published = useMemo(() => {
38
if (data == null) return -1;
39
let n = 0;
40
for (const x of data) {
41
if (!x.disabled) {
42
n += 1;
43
}
44
}
45
return n;
46
}, [data]);
47
48
function render_confirm(): JSX.Element {
49
const goal = "YES, UNPUBLISH EVERYTHING!";
50
const body = (
51
<div>
52
<div style={{ fontSize: "12pt", margin: "auto", maxWidth: "800px" }}>
53
{`Are you sure you want to unpublish ALL ${num_published} listed and unlisted ${plural(
54
num_published,
55
"path",
56
)} published in all projects on which you collaborate and have been active? You cannot easily undo this operation, though you could tediously republish everything. To unpublish everything type "${goal}" below, then click the button.`}
57
</div>
58
<br />
59
<br />
60
<Input
61
size="large"
62
placeholder={goal}
63
value={confirm_text}
64
onChange={(e) => set_confirm_text(e.target.value)}
65
/>
66
<br />
67
<br />
68
<Button
69
disabled={confirm_text != goal}
70
onClick={() => {
71
set_confirm(false);
72
set_confirm_text("");
73
disable_all();
74
}}
75
>
76
{unpublishEverything}
77
</Button>
78
</div>
79
);
80
return (
81
<Alert
82
style={{ marginBottom: "20px" }}
83
message={<h3>{unpublishEverything}?</h3>}
84
description={body}
85
type="warning"
86
showIcon
87
closable
88
afterClose={() => {
89
set_confirm(false);
90
set_confirm_text("");
91
}}
92
/>
93
);
94
}
95
96
async function disable_all(): Promise<void> {
97
if (data == null) return;
98
set_counter(0);
99
for (const x of data) {
100
if (x.disabled) continue;
101
if (!isMountedRef.current) return;
102
await webapp_client.async_query({
103
query: {
104
public_paths: {
105
id: x.id,
106
project_id: x.project_id,
107
path: x.path,
108
disabled: true,
109
},
110
},
111
});
112
set_counter(counter + 1);
113
}
114
refresh();
115
set_counter(-1);
116
}
117
118
return (
119
<div>
120
{confirm && render_confirm()}
121
{counter >= 0 && num_published > 0 && (
122
<h1>
123
Unpublished: {counter}/{num_published}
124
</h1>
125
)}
126
<Button
127
onClick={() => set_confirm(true)}
128
disabled={num_published == 0 || confirm}
129
>
130
{unpublishEverything}...
131
</Button>
132
</div>
133
);
134
},
135
);
136
137