|
| 1 | +import { tw } from "twind"; |
| 2 | +import { FolderBlockProps } from "@githubnext/blocks"; |
| 3 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | +import "./style.css"; |
| 5 | +import { Button, Text, Link } from "@primer/react"; |
| 6 | + |
| 7 | +const SCRIMBA_BASE_URL = import.meta.env.DEV |
| 8 | + ? "https://dev.scrimba.com:3000" |
| 9 | + : "https://scrimba.com"; |
| 10 | +const SCRIMBA_URL = `${SCRIMBA_BASE_URL}/new/htmlblocks`; |
| 11 | + |
| 12 | +export default function (props: FolderBlockProps) { |
| 13 | + const { tree, context, onRequestGitHubData, files } = props; |
| 14 | + const [scrimMounted, setScrimMounted] = useState(false); |
| 15 | + const [scrimRecorded, setScrimRecorded] = useState(""); |
| 16 | + |
| 17 | + const frame = useRef(null); |
| 18 | + |
| 19 | + const getFilesRecursive = useCallback( |
| 20 | + async (folderPath: string) => { |
| 21 | + const apiUrl = `/repos/${context.owner}/${context.repo}/contents/${folderPath}`; |
| 22 | + const folderContents = await onRequestGitHubData(apiUrl, { |
| 23 | + ref: context.sha, |
| 24 | + }); |
| 25 | + |
| 26 | + const filePromises = folderContents.map(async (item) => { |
| 27 | + if (item.type === "file") { |
| 28 | + const fileResponse = await fetch(item.download_url); |
| 29 | + return { path: item.path, content: await fileResponse.text() }; |
| 30 | + } else if (item.type === "dir") { |
| 31 | + const subFolderFiles = await getFilesRecursive(item.path); |
| 32 | + return subFolderFiles; |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + let files = await Promise.all(filePromises) |
| 37 | + .then((files) => files.flat()) |
| 38 | + .catch((error) => { |
| 39 | + console.error( |
| 40 | + `Failed to get contents of files in ${folderPath}`, |
| 41 | + error |
| 42 | + ); |
| 43 | + return []; |
| 44 | + }); |
| 45 | + |
| 46 | + return files; |
| 47 | + }, |
| 48 | + [context.repo, context.owner, context.sha] |
| 49 | + ); |
| 50 | + |
| 51 | + const getFiles = useCallback( |
| 52 | + async (all?: boolean) => { |
| 53 | + if (!tree && !all) { |
| 54 | + return [{ ...context, type: "blob", content: props.content }]; |
| 55 | + } |
| 56 | + return getFilesRecursive(context.path); |
| 57 | + }, |
| 58 | + [context.repo, context.owner, context.sha, files, tree, props.content] |
| 59 | + ); |
| 60 | + |
| 61 | + const sendData = useCallback( |
| 62 | + (files, other?: boolean) => { |
| 63 | + if (frame.current) { |
| 64 | + let payload = { |
| 65 | + files, |
| 66 | + title: `Walkthrough of ${context.path.split("/").slice(-1)[0]} in ${ |
| 67 | + context.owner |
| 68 | + }/${context.repo}`, |
| 69 | + }; |
| 70 | + if (other) payload = files; |
| 71 | + frame.current.contentWindow.postMessage(payload, "*"); |
| 72 | + } |
| 73 | + }, |
| 74 | + [frame, context.owner, context.repo, context.path] |
| 75 | + ); |
| 76 | + |
| 77 | + const listenToRecorded = useCallback(() => { |
| 78 | + window.addEventListener("message", (e) => { |
| 79 | + if (e.origin !== SCRIMBA_BASE_URL) return; |
| 80 | + if (e.data.event == "recorded") { |
| 81 | + setScrimRecorded(e.data.id); |
| 82 | + } |
| 83 | + }); |
| 84 | + }, []); |
| 85 | + |
| 86 | + const reload = useCallback(async () => { |
| 87 | + let files; |
| 88 | + if (!tree) { |
| 89 | + const parts = context.path.split("/"); |
| 90 | + const path = parts[parts.length - 1]; |
| 91 | + files = [{ path, content: props.content, type: "blob" }]; |
| 92 | + } else { |
| 93 | + files = await getFiles(); |
| 94 | + } |
| 95 | + sendData(files); |
| 96 | + setScrimMounted(true); |
| 97 | + listenToRecorded(); |
| 98 | + }, [props.content, context.path]); |
| 99 | + |
| 100 | + const init = useCallback(async () => { |
| 101 | + let files = await getFiles(); |
| 102 | + window.addEventListener("message", (e) => { |
| 103 | + if (e.origin !== SCRIMBA_BASE_URL) return; |
| 104 | + |
| 105 | + if (e.data.mounted) { |
| 106 | + setScrimMounted(true); |
| 107 | + if (!tree) { |
| 108 | + const parts = context.path.split("/"); |
| 109 | + const path = parts[parts.length - 1]; |
| 110 | + files = [{ path, content: props.content, type: "blob" }]; |
| 111 | + } |
| 112 | + sendData(files); |
| 113 | + } |
| 114 | + }); |
| 115 | + }, [getFiles, props.content, context.path, frame]); |
| 116 | + |
| 117 | + const loadFiles = useCallback(async () => { |
| 118 | + if (!frame.current) return; |
| 119 | + const files = await getFiles(true); |
| 120 | + sendData(files); |
| 121 | + }, [getFiles, frame, context.folder]); |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + if (frame.current) { |
| 125 | + init(); |
| 126 | + listenToRecorded(); |
| 127 | + } |
| 128 | + }, [frame]); |
| 129 | + const publish = useCallback(() => { |
| 130 | + sendData({ action: "publish" }, true); |
| 131 | + }, [sendData]); |
| 132 | + |
| 133 | + return ( |
| 134 | + <div className={tw(`w-full h-full`)} id="example-block-code-block"> |
| 135 | + <div className={tw(`flex w-full h-full overflow-x-hidden flex-col`)}> |
| 136 | + <div className={tw(`flex ai-center jc-between flex-row`)}> |
| 137 | + {scrimMounted ? null : ( |
| 138 | + <Button onClick={reload}>Not working? Reload</Button> |
| 139 | + )} |
| 140 | + <Button onClick={loadFiles}>Load all files</Button> |
| 141 | + </div> |
| 142 | + {scrimRecorded ? ( |
| 143 | + <Text> |
| 144 | + <Button onClick={publish} className={tw("inline-block")}> |
| 145 | + Publish the scrim{" "} |
| 146 | + </Button> |
| 147 | + to make it available for everyone at{" "} |
| 148 | + <Link |
| 149 | + href={`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`} |
| 150 | + >{`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`}</Link> |
| 151 | + </Text> |
| 152 | + ) : null} |
| 153 | + <iframe |
| 154 | + allow="microphone;" |
| 155 | + ref={frame} |
| 156 | + src={SCRIMBA_URL} |
| 157 | + className={tw`my-2 h-full w-full`} |
| 158 | + /> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
0 commit comments