Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/compute/images-hook.tsx
Views: 687
import { redux, useTypedRedux } from "@cocalc/frontend/app-framework";1import ShowError from "@cocalc/frontend/components/error";2import type {3Architecture,4Images,5GoogleCloudImages,6} from "@cocalc/util/db-schema/compute-servers";7import { useEffect } from "react";8import type { CustomizeActions } from "@cocalc/frontend/customize";910type Name = "compute_servers_images" | "compute_servers_images_google";1112export async function reloadImages(name: Name, reload?: boolean) {13const actions = redux.getActions("customize") as CustomizeActions;14switch (name) {15case "compute_servers_images":16await actions.updateComputeServerImages(reload);17return;18case "compute_servers_images_google":19await actions.updateComputeServerImagesGoogle(reload);20return;21default:22// non-fatal, since reloading something uknown is trivial.23console.warn(`uknown images -- "${name}"`);24}25}2627export async function forceRefreshImages() {28await reloadImages("compute_servers_images", true);29await reloadImages("compute_servers_images_google", true);30}3132function useImages0(name: Name) {33const IMAGES = useTypedRedux("customize", name);34useEffect(() => {35if (IMAGES == null) {36reloadImages(name);37}38}, []);3940if (IMAGES == null) {41return [null, null];42}43if (typeof IMAGES == "string") {44return [45null,46<ShowError47error={`Error loading ${name} -- ${IMAGES}`}48setError={reloadImages}49/>,50];51}52if (name == "compute_servers_images") {53return [IMAGES.toJS() as Images, null];54} else if (name == "compute_servers_images_google") {55return [IMAGES.toJS() as GoogleCloudImages, null];56} else {57throw Error("bug");58}59}6061export function useImages(): [Images | null, JSX.Element | null] {62const [images, error] = useImages0("compute_servers_images");63if (error != null) {64return [null, error as JSX.Element];65} else if (images == null) {66return [images as null, error as null];67} else {68return [images as Images, error as null];69}70}7172// tag, arch, etc., are first through makeValidGoogleName73type GoogleImages = {74[image: string]: {75[tag: string]: { arch: Architecture; tested?: boolean };76};77};7879export function useGoogleImages(): [GoogleImages | null, JSX.Element | null] {80const [images, error] = useImages0("compute_servers_images_google");81if (error != null) {82return [null, error as JSX.Element];83} else if (images == null) {84return [images as null, error as null];85} else {86const x: GoogleImages = {};87for (const name in images) {88const { labels } = images[name];89if (x[labels.image] == null) {90x[labels.image] = {};91}92x[labels.image][`${labels.tag}-${labels.arch}`] = labels;93}94return [x, error as null];95}96}979899