|
| 1 | +/** @jsxImportSource react */ |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { fetchReadmeHTML, fetchSourceHTML } from "./utils.js"; |
| 6 | + |
| 7 | +const sources = { |
| 8 | + "./index.html": { lang: "html" }, |
| 9 | + "./main.tsx": { lang: "tsx" }, |
| 10 | + "./utils.js": { lang: "javascript" }, |
| 11 | +} as const satisfies { [url: string]: { lang: string } }; |
| 12 | + |
| 13 | +function useSourceHTML(url: keyof typeof sources) { |
| 14 | + const [sourceHTML, setSourceHTML] = useState<string | undefined>(undefined); |
| 15 | + const [loading, setLoading] = useState(false); |
| 16 | + |
| 17 | + useEffect(function updateSourceHTML() { |
| 18 | + const ctrl = new AbortController(); |
| 19 | + setLoading(true); |
| 20 | + fetchSourceHTML(url, sources[url].lang, ctrl.signal) |
| 21 | + .then(setSourceHTML, (error) => { |
| 22 | + if (!ctrl.signal.aborted) throw error; |
| 23 | + }) |
| 24 | + .finally(() => { |
| 25 | + if (!ctrl.signal.aborted) setLoading(false); |
| 26 | + }); |
| 27 | + return () => ctrl.abort(); |
| 28 | + }, [url]); |
| 29 | + |
| 30 | + return [sourceHTML, { loading }] as const; |
| 31 | +} |
| 32 | + |
| 33 | +function useReadmeHTML() { |
| 34 | + const [readmeHTML, setReadmeHTML] = useState<string | undefined>(undefined); |
| 35 | + |
| 36 | + useEffect(function updateReadmeHTML() { |
| 37 | + const ctrl = new AbortController(); |
| 38 | + fetchReadmeHTML(ctrl.signal) |
| 39 | + .then(setReadmeHTML, (error) => { |
| 40 | + if (!ctrl.signal.aborted) throw error; |
| 41 | + }); |
| 42 | + return () => ctrl.abort(); |
| 43 | + }, []); |
| 44 | + |
| 45 | + return [readmeHTML] as const; |
| 46 | +} |
| 47 | + |
| 48 | +function SourceView(props: { |
| 49 | + url: keyof typeof sources; |
| 50 | + onLoadingChange?: (loading: boolean) => void; |
| 51 | +}) { |
| 52 | + const [sourceHTML, { loading }] = useSourceHTML(props.url); |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + props.onLoadingChange?.(loading); |
| 56 | + }, [loading, props.onLoadingChange]); |
| 57 | + |
| 58 | + return <div dangerouslySetInnerHTML={{ __html: sourceHTML ?? "" }}></div>; |
| 59 | +} |
| 60 | + |
| 61 | +function SourcesView() { |
| 62 | + const [selectedSourceUrl, setSelectedSourceUrl] = useState< |
| 63 | + keyof typeof sources |
| 64 | + >("./index.html"); |
| 65 | + const [loading, setLoading] = useState(false); |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <label> |
| 70 | + Source: |
| 71 | + <select |
| 72 | + onChange={function handleChange(event) { |
| 73 | + setSelectedSourceUrl( |
| 74 | + (event.currentTarget as HTMLSelectElement) |
| 75 | + .value as keyof typeof sources, |
| 76 | + ); |
| 77 | + }} |
| 78 | + > |
| 79 | + {Object.keys(sources).map((url) => ( |
| 80 | + <option key={url} value={url}>{url}</option> |
| 81 | + ))} |
| 82 | + </select> |
| 83 | + </label> |
| 84 | + {loading && <progress />} |
| 85 | + <SourceView |
| 86 | + url={selectedSourceUrl} |
| 87 | + onLoadingChange={setLoading} |
| 88 | + /> |
| 89 | + </> |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +function ReadmeView() { |
| 94 | + const [readmeHTML] = useReadmeHTML(); |
| 95 | + |
| 96 | + return <div dangerouslySetInnerHTML={{ __html: readmeHTML ?? "" }}></div>; |
| 97 | +} |
| 98 | + |
| 99 | +function App() { |
| 100 | + return ( |
| 101 | + <> |
| 102 | + <ReadmeView /> |
| 103 | + <SourcesView /> |
| 104 | + </> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +createRoot(document.body).render(<App />); |
0 commit comments