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/inline.tsx
Views: 687
1
/*
2
Simple inline display of the compute server with given id, for use
3
elsewhere in cocalc.
4
5
This may get more sophisticated, e.g., clickable link, hover for status, etc.
6
*/
7
8
import { useEffect, useState } from "react";
9
import getTitle from "./get-title";
10
import { Spin, Tooltip } from "antd";
11
import { avatar_fontcolor } from "@cocalc/frontend/account/avatar/font-color";
12
import { PROJECT_COLOR } from "./select-server";
13
import { trunc_middle } from "@cocalc/util/misc";
14
15
interface Props {
16
id: number;
17
noColor?: boolean;
18
colorOnly?: boolean;
19
style?;
20
idOnly?: boolean;
21
titleOnly?: boolean;
22
prompt?: boolean;
23
computeServer?; // immutable js object from store, if known
24
colorLabel?; // put in middle of colorOnly
25
}
26
27
export default function ComputeServer({
28
id,
29
noColor,
30
colorOnly,
31
style,
32
titleOnly,
33
idOnly,
34
prompt,
35
computeServer,
36
colorLabel,
37
}: Props) {
38
const [server, setServer] = useState<null | {
39
title: string;
40
color: string;
41
project_specific_id: number;
42
}>(
43
computeServer != null
44
? {
45
title: computeServer.get("title"),
46
color: computeServer.get("color"),
47
project_specific_id: computeServer.get("project_specific_id"),
48
}
49
: null,
50
);
51
useEffect(() => {
52
if (computeServer != null) {
53
setServer({
54
title: computeServer.get("title"),
55
color: computeServer.get("color"),
56
project_specific_id: computeServer.get("project_specific_id"),
57
});
58
return;
59
}
60
if (!id) {
61
setServer({
62
title: "Home Base",
63
color: PROJECT_COLOR,
64
project_specific_id: 0,
65
});
66
return;
67
}
68
(async () => {
69
try {
70
setServer(await getTitle(id));
71
} catch (err) {
72
console.warn(err);
73
}
74
})();
75
}, [id, computeServer]);
76
77
if (colorOnly) {
78
return (
79
<div
80
style={{
81
backgroundColor: server?.color,
82
height: "3px",
83
textAlign: "center",
84
color: server?.color ? avatar_fontcolor(server?.color) : undefined,
85
...style,
86
}}
87
>
88
{colorLabel}
89
</div>
90
);
91
}
92
93
if (prompt) {
94
const s = (
95
<span style={style}>
96
compute-server-{server?.project_specific_id ?? "?"}
97
</span>
98
);
99
if (server == null) {
100
return s;
101
}
102
return (
103
<Tooltip title={<>Compute Server '{trunc_middle(server.title, 40)}'</>}>
104
{s}
105
</Tooltip>
106
);
107
}
108
109
if (server == null) {
110
return (
111
<span style={style}>
112
<Spin />
113
</span>
114
);
115
}
116
let label;
117
if (idOnly) {
118
label = `Id: ${server.project_specific_id}`;
119
} else {
120
label = titleOnly ? (
121
trunc_middle(server.title, 30)
122
) : (
123
<>
124
Compute Server '{trunc_middle(server.title, 30)}' (Id:{" "}
125
{server.project_specific_id})
126
</>
127
);
128
}
129
if (noColor) {
130
return <span style={style}>{label}</span>;
131
}
132
return (
133
<span
134
style={{
135
backgroundColor: server.color,
136
color: avatar_fontcolor(server.color),
137
overflow: "hidden",
138
padding: "0 5px",
139
borderRadius: "3px",
140
...style,
141
}}
142
>
143
{label}
144
</span>
145
);
146
}
147
148