| 18 | |
| 19 | React.useEffect(() => { |
| 20 | async function loadSourceCode() { |
| 21 | try { |
| 22 | const mod = await import(`../../public/r/${name}.json`) |
| 23 | const json = mod.default |
| 24 | |
| 25 | // Find the main component file that matches the name |
| 26 | const mainFile = json.files.find( |
| 27 | (file: any) => |
| 28 | file.path |
| 29 | .split("/") |
| 30 | .pop() |
| 31 | .replace(/\.(tsx|ts)$/, "") === name |
| 32 | ) |
| 33 | |
| 34 | if (mainFile) { |
| 35 | setSourceCode(mainFile.content) |
| 36 | } else { |
| 37 | console.error(`Could not find main file for ${name}`) |
| 38 | setSourceCode("") |
| 39 | } |
| 40 | } catch (error) { |
| 41 | console.error(`Failed to load source for ${name}:`, error) |
| 42 | setSourceCode("") |
| 43 | } |
| 44 | } |
| 45 | loadSourceCode() |
| 46 | }, [name]) |
| 47 | |