diff --git a/package.json b/package.json index b3d373f3..fce9b084 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "preview": "vite preview", "prepare": "husky", "lint": "eslint .", - "prettier": "prettier --write .", + "prettier": "pnpm run format", + "format": "prettier --write .", "test": "vitest" }, "dependencies": { diff --git a/src/components/CodeHeader.tsx b/src/components/CodeHeader.tsx index 1022db3c..75ad72dd 100644 --- a/src/components/CodeHeader.tsx +++ b/src/components/CodeHeader.tsx @@ -1,22 +1,20 @@ -import { memo, useCallback, useMemo, type ReactNode, useState } from "react"; +import { memo, type ReactNode, useCallback, useMemo, useState } from "react"; import { + Box, Flex, IconButton, - useClipboard, - useColorModeValue, - Text, - Box, Menu, MenuButton, - MenuItem, - MenuList, Spinner, + Text, + useClipboard, + useColorModeValue, } from "@chakra-ui/react"; -import { TbCopy, TbDownload, TbRun, TbExternalLink } from "react-icons/tb"; +import { TbCopy, TbDownload, TbExternalLink, TbRun } from "react-icons/tb"; import { download, formatAsCodeBlock } from "../lib/utils"; import { useAlert } from "../hooks/use-alert"; -import { isRunnableInBrowser, isRunnableOnServer, runCode } from "../lib/run-code"; +import { isRunnableInBrowser, runCode } from "../lib/run-code"; type PreHeaderProps = { language: string; @@ -36,12 +34,10 @@ function CodeHeader({ codeDownloadFilename, }: PreHeaderProps) { const { onCopy } = useClipboard(code); - const { info, error } = useAlert(); + const { info } = useAlert(); const [isRunning, setIsRunning] = useState(false); // Only show the "Run" button for JS code blocks, and only when we aren't already loading - const shouldShowRunButton = - (isRunnableInBrowser(language) || isRunnableOnServer(language)) && onPrompt; - const shouldShowRunMenuList = isRunnableOnServer(language) && onPrompt; + const shouldShowRunButton = isRunnableInBrowser(language) && onPrompt; const handleCopy = useCallback(() => { onCopy(); @@ -59,60 +55,6 @@ function CodeHeader({ }); }, [info, code, codeDownloadFilename]); - const handleRunRemote = useCallback(async () => { - if (!onPrompt) { - return; - } - - // https://docs.val.town/api/eval/ - const evalUrl = new URL("https://api.val.town/v1/eval"); - const res = await fetch(evalUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - code: code.replaceAll("\n", " ").trim(), - args: [], - }), - }); - - if (!res.ok) { - error({ title: "Error Running Code", message: "Unable to run code remotely" }); - return; - } - - let result: string; - try { - // val.town returns an empty body when code doesn't return a value, which breaks res.json() - result = await res.json(); - } catch { - error({ - title: "Server unable to parse code", - message: "Try rewriting the code as an async function returning a value.", - }); - return; - } - - try { - if (typeof result === "string") { - // catch corner cases with strings - if (!result.length || result[0] === "/") { - result = formatAsCodeBlock(JSON.stringify(result), "js"); - } else if (result.startsWith("<")) { - result = formatAsCodeBlock(result, "html"); - } else { - // result is good to include inline, might have formatting, etc - } - } - } catch (error: any) { - result = formatAsCodeBlock( - error instanceof Error ? `${error.name}: ${error.message}\n${error.stack}` : `${error}` - ); - } - if (result !== undefined) { - onPrompt(result); - } - }, [code, onPrompt, error]); - const handleRunBrowser = useCallback(async () => { if (!onPrompt) { return; @@ -210,14 +152,8 @@ function CodeHeader({ _dark={{ color: "gray.300" }} variant="ghost" isDisabled={isLoading} - onClick={shouldShowRunMenuList ? undefined : handleRunBrowser} + onClick={handleRunBrowser} /> - {shouldShowRunMenuList && ( - - Run in Browser - Run on Server - - )} )} ( callback: () => Promise ): Promise<{ logs: string | undefined; ret: T }> { @@ -57,7 +52,7 @@ async function captureConsole( }; // Prepare a string to store the captured messages - let capturedMessages: string = ""; + let capturedMessages = ""; // Function to create an overridden console method const createOverriddenMethod = (method: keyof Console) => {