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/images-hook.tsx
Views: 687
1
import { redux, useTypedRedux } from "@cocalc/frontend/app-framework";
2
import ShowError from "@cocalc/frontend/components/error";
3
import type {
4
Architecture,
5
Images,
6
GoogleCloudImages,
7
} from "@cocalc/util/db-schema/compute-servers";
8
import { useEffect } from "react";
9
import type { CustomizeActions } from "@cocalc/frontend/customize";
10
11
type Name = "compute_servers_images" | "compute_servers_images_google";
12
13
export async function reloadImages(name: Name, reload?: boolean) {
14
const actions = redux.getActions("customize") as CustomizeActions;
15
switch (name) {
16
case "compute_servers_images":
17
await actions.updateComputeServerImages(reload);
18
return;
19
case "compute_servers_images_google":
20
await actions.updateComputeServerImagesGoogle(reload);
21
return;
22
default:
23
// non-fatal, since reloading something uknown is trivial.
24
console.warn(`uknown images -- "${name}"`);
25
}
26
}
27
28
export async function forceRefreshImages() {
29
await reloadImages("compute_servers_images", true);
30
await reloadImages("compute_servers_images_google", true);
31
}
32
33
function useImages0(name: Name) {
34
const IMAGES = useTypedRedux("customize", name);
35
useEffect(() => {
36
if (IMAGES == null) {
37
reloadImages(name);
38
}
39
}, []);
40
41
if (IMAGES == null) {
42
return [null, null];
43
}
44
if (typeof IMAGES == "string") {
45
return [
46
null,
47
<ShowError
48
error={`Error loading ${name} -- ${IMAGES}`}
49
setError={reloadImages}
50
/>,
51
];
52
}
53
if (name == "compute_servers_images") {
54
return [IMAGES.toJS() as Images, null];
55
} else if (name == "compute_servers_images_google") {
56
return [IMAGES.toJS() as GoogleCloudImages, null];
57
} else {
58
throw Error("bug");
59
}
60
}
61
62
export function useImages(): [Images | null, JSX.Element | null] {
63
const [images, error] = useImages0("compute_servers_images");
64
if (error != null) {
65
return [null, error as JSX.Element];
66
} else if (images == null) {
67
return [images as null, error as null];
68
} else {
69
return [images as Images, error as null];
70
}
71
}
72
73
// tag, arch, etc., are first through makeValidGoogleName
74
type GoogleImages = {
75
[image: string]: {
76
[tag: string]: { arch: Architecture; tested?: boolean };
77
};
78
};
79
80
export function useGoogleImages(): [GoogleImages | null, JSX.Element | null] {
81
const [images, error] = useImages0("compute_servers_images_google");
82
if (error != null) {
83
return [null, error as JSX.Element];
84
} else if (images == null) {
85
return [images as null, error as null];
86
} else {
87
const x: GoogleImages = {};
88
for (const name in images) {
89
const { labels } = images[name];
90
if (x[labels.image] == null) {
91
x[labels.image] = {};
92
}
93
x[labels.image][`${labels.tag}-${labels.arch}`] = labels;
94
}
95
return [x, error as null];
96
}
97
}
98
99