|
| 1 | +import { |
| 2 | + createContext, |
| 3 | + useCallback, |
| 4 | + useContext, |
| 5 | + useEffect, |
| 6 | + useMemo, |
| 7 | + useState, |
| 8 | +} from "react"; |
| 9 | +import Spinner from "react-bootstrap/Spinner"; |
| 10 | +import { parse } from "yaml"; |
| 11 | + |
| 12 | +const CatalogContext = createContext(); |
| 13 | + |
| 14 | +const CATALOGS = [ |
| 15 | + { |
| 16 | + name: "Awesome Labspaces", |
| 17 | + url: "https://raw.githubusercontent.com/dockersamples/awesome-labspaces/refs/heads/main/catalog.yaml", |
| 18 | + }, |
| 19 | +]; |
| 20 | + |
| 21 | +const LOCALSTORAGE_CATALOG_KEY = "labspaces.catalogs"; |
| 22 | +const LOCALSTORAGE_ADDITIONAL_LABSPACES_KEY = "labspaces.additionalLabspaces"; |
| 23 | + |
| 24 | +export function CatalogContextProvider({ children }) { |
| 25 | + const [catalogs, setCatalogs] = useState( |
| 26 | + localStorage.getItem(LOCALSTORAGE_CATALOG_KEY) |
| 27 | + ? JSON.parse(localStorage.getItem(LOCALSTORAGE_CATALOG_KEY)) |
| 28 | + : CATALOGS, |
| 29 | + ); |
| 30 | + const [catalogDetails, setCatalogDetails] = useState(null); |
| 31 | + const [customLabspaces, setCustomLabspaces] = useState( |
| 32 | + localStorage.getItem(LOCALSTORAGE_ADDITIONAL_LABSPACES_KEY) |
| 33 | + ? JSON.parse(localStorage.getItem(LOCALSTORAGE_ADDITIONAL_LABSPACES_KEY)) |
| 34 | + : [], |
| 35 | + ); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + localStorage.setItem(LOCALSTORAGE_CATALOG_KEY, JSON.stringify(catalogs)); |
| 39 | + }, [catalogs]); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + localStorage.setItem( |
| 43 | + LOCALSTORAGE_ADDITIONAL_LABSPACES_KEY, |
| 44 | + JSON.stringify(customLabspaces), |
| 45 | + ); |
| 46 | + }, [customLabspaces]); |
| 47 | + |
| 48 | + const addCatalog = useCallback( |
| 49 | + (name, url) => { |
| 50 | + setCatalogs((catalog) => [...catalog, { name, url }]); |
| 51 | + }, |
| 52 | + [setCatalogs], |
| 53 | + ); |
| 54 | + |
| 55 | + const removeCatalog = useCallback( |
| 56 | + (url) => { |
| 57 | + setCatalogs((catalogs) => catalogs.filter((c) => c.url !== url)); |
| 58 | + }, |
| 59 | + [setCatalogs], |
| 60 | + ); |
| 61 | + |
| 62 | + const tags = useMemo(() => { |
| 63 | + if (!catalogDetails) return null; |
| 64 | + const allTags = []; |
| 65 | + catalogDetails.forEach((catalog) => { |
| 66 | + catalog.tags.forEach((tag) => { |
| 67 | + if (!allTags.find((t) => t.label === tag.label)) { |
| 68 | + allTags.push(tag); |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + return allTags.sort((a, b) => a.label.localeCompare(b.label)); |
| 73 | + }, [catalogDetails]); |
| 74 | + |
| 75 | + const labspaces = useMemo(() => { |
| 76 | + if (!catalogDetails) return null; |
| 77 | + const allLabspaces = [...customLabspaces]; |
| 78 | + catalogDetails.forEach((catalog) => { |
| 79 | + catalog.labspaces.forEach((labspace) => { |
| 80 | + allLabspaces.push({ ...labspace, catalog: catalog.url }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + return allLabspaces.sort((a, b) => { |
| 84 | + if (a.highlighted && !b.highlighted) return -1; |
| 85 | + if (!a.highlighted && b.highlighted) return 1; |
| 86 | + return a.title.localeCompare(b.title); |
| 87 | + }); |
| 88 | + }, [catalogDetails, customLabspaces]); |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + Promise.all( |
| 92 | + catalogs.map((catalog) => |
| 93 | + fetch(catalog.url) |
| 94 | + .then((res) => res.text()) |
| 95 | + .then((text) => parse(text)) |
| 96 | + .then((data) => ({ |
| 97 | + ...catalog, |
| 98 | + tags: data.tags || [], |
| 99 | + labspaces: data.labspaces || [], |
| 100 | + })), |
| 101 | + ), |
| 102 | + ).then((results) => setCatalogDetails(results)); |
| 103 | + }, [catalogs]); |
| 104 | + |
| 105 | + const addCustomLabspace = useCallback( |
| 106 | + (title, publishedRepo) => { |
| 107 | + const newLabspace = { title, publishedRepo }; |
| 108 | + setCustomLabspaces((labspaces) => [...labspaces, newLabspace]); |
| 109 | + }, |
| 110 | + [setCustomLabspaces], |
| 111 | + ); |
| 112 | + |
| 113 | + const removeCustomLabspace = useCallback( |
| 114 | + (publishedRepo) => { |
| 115 | + setCustomLabspaces((labs) => |
| 116 | + labs.filter((l) => l.publishedRepo !== publishedRepo), |
| 117 | + ); |
| 118 | + }, |
| 119 | + [setCustomLabspaces], |
| 120 | + ); |
| 121 | + |
| 122 | + if (!catalogDetails || !labspaces || !tags) { |
| 123 | + return ( |
| 124 | + <div |
| 125 | + className="d-flex justify-content-center align-items-center" |
| 126 | + style={{ height: "100vh" }} |
| 127 | + > |
| 128 | + <Spinner animation="border" role="status"> |
| 129 | + <span className="visually-hidden">Loading...</span> |
| 130 | + </Spinner> |
| 131 | + </div> |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + return ( |
| 136 | + <CatalogContext.Provider |
| 137 | + value={{ |
| 138 | + catalogs: catalogDetails, |
| 139 | + addCatalog, |
| 140 | + removeCatalog, |
| 141 | + tags, |
| 142 | + labspaces, |
| 143 | + addCustomLabspace, |
| 144 | + removeCustomLabspace, |
| 145 | + }} |
| 146 | + > |
| 147 | + {children} |
| 148 | + </CatalogContext.Provider> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +export const useCatalogs = () => useContext(CatalogContext); |
0 commit comments