From 4adb983f7f82baff4923277b53496704a32b57ce Mon Sep 17 00:00:00 2001 From: seveibar Date: Mon, 14 Oct 2024 11:29:36 -0700 Subject: [PATCH] fix recursive importing --- fake-snippets-api/lib/db/seed.ts | 10 +++++----- src/hooks/use-run-tsx/index.tsx | 15 ++++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/fake-snippets-api/lib/db/seed.ts b/fake-snippets-api/lib/db/seed.ts index 0af303b..93f8e0d 100644 --- a/fake-snippets-api/lib/db/seed.ts +++ b/fake-snippets-api/lib/db/seed.ts @@ -66,19 +66,19 @@ exports.A555Timer = A555Timer; // Add a snippet that outputs a square waveform using the a555timer db.addSnippet({ - name: "seveibar/a555timer-square-wave", + name: "testuser/a555timer-square-wave", unscoped_name: "a555timer-square-wave", - owner_name: "seveibar", + owner_name: "testuser", code: ` import { A555Timer } from "@tsci/seveibar.a555timer" -export default () => ( +export const SquareWaveModule = () => ( ) `.trim(), - dts: "export declare const MyChip: (props: {\n name: string;\n}) => any;\n", + dts: 'export declare const SquareWaveModule: () => import("react/jsx-runtime").JSX.Element;\n', compiled_js: - '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.MyChip = void 0;\nvar _seveibar = require("@tsci/seveibar.a555timer");\nconst MyChip = props => /*#__PURE__*/React.createElement(_seveibar.A555Timer, {\n name: "U1"\n});\nexports.MyChip = MyChip;', + '"use strict";\n\nObject.defineProperty(exports, "__esModule", {\n value: true\n});\nexports.SquareWaveModule = void 0;\nvar _seveibar = require("@tsci/seveibar.a555timer");\nconst SquareWaveModule = () => /*#__PURE__*/React.createElement(_seveibar.A555Timer, {\n name: "U1"\n});\nexports.SquareWaveModule = SquareWaveModule;', created_at: new Date().toISOString(), updated_at: new Date().toISOString(), snippet_type: "package", diff --git a/src/hooks/use-run-tsx/index.tsx b/src/hooks/use-run-tsx/index.tsx index 1ae13f8..517ac7a 100644 --- a/src/hooks/use-run-tsx/index.tsx +++ b/src/hooks/use-run-tsx/index.tsx @@ -81,9 +81,16 @@ export const useRunTsx = ({ const fullSnippetName = importName .replace("@tsci/", "") .replace(".", "/") - const { snippet: importedSnippet } = await fetch( + const { snippet: importedSnippet, error } = await fetch( `${apiBaseUrl}/snippets/get?name=${fullSnippetName}`, - ).then((res) => res.json()) + ) + .then((res) => res.json()) + .catch((e) => ({ error: e })) + + if (error) { + console.error("Error fetching import", importName, error) + return + } const { compiled_js, code } = importedSnippet @@ -98,9 +105,7 @@ export const useRunTsx = ({ } try { - preSuppliedImports[importName] = evalCompiledJs( - importedSnippet.compiled_js, - ).exports + preSuppliedImports[importName] = evalCompiledJs(compiled_js).exports } catch (e) { console.error("Error importing snippet", e) }