Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/custom-software/util.ts
5747 views
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 type { IconName } from "@cocalc/frontend/components/icon";
7
8
import { join as path_join } from "path";
9
10
import { COLORS } from "@cocalc/util/theme";
11
12
export const RESET_ICON: IconName = "redo";
13
14
// Do NOT change this:
15
export type NAME_TYPE = "compute_images";
16
export const NAME = "compute_images" as NAME_TYPE;
17
18
export const CUSTOM_IMG_PREFIX = "custom/";
19
20
export const CUSTOM_SOFTWARE_HELP_URL =
21
"https://doc.cocalc.com/software.html#custom-environments";
22
23
export function compute_image2name(compute_image: string): string {
24
const name = compute_image.slice(CUSTOM_IMG_PREFIX.length);
25
return name.replace("/", ":");
26
}
27
28
export function compute_image2basename(compute_image: string): string {
29
const name = compute_image.slice(CUSTOM_IMG_PREFIX.length);
30
return name.split("/")[0];
31
}
32
33
export const title_style: React.CSSProperties = {
34
textOverflow: "ellipsis",
35
whiteSpace: "nowrap" as "nowrap",
36
overflow: "hidden",
37
paddingLeft: "10px",
38
margin: "5px 10px",
39
color: COLORS.GRAY,
40
} as const;
41
42
export function props2img(props: {
43
project_map?;
44
project_id: string;
45
images?;
46
}) {
47
if (props.project_map == null) return null;
48
const ci = props.project_map.getIn([props.project_id, "compute_image"]);
49
if (ci == null) return null;
50
if (!is_custom_image(ci)) return null;
51
return props.images?.get(compute_image2basename(ci));
52
}
53
54
// derive the actual compute image name (which will be set in the DB) from the selected ID.
55
export function custom_image_name(id: string): string {
56
let tag: string;
57
if (id.indexOf(":") >= 0) {
58
[id, tag] = id.split(":");
59
} else {
60
tag = "latest";
61
}
62
return path_join(CUSTOM_IMG_PREFIX, id, tag);
63
}
64
65
export function is_custom_image(img: string): boolean {
66
return img.startsWith(CUSTOM_IMG_PREFIX);
67
}
68
69