Skip to content

Commit

Permalink
Allow running JS/TS remotely via val.town's eval endpoint (#403)
Browse files Browse the repository at this point in the history
* Allow running JS/TS remotely via val.town's eval endpoint

* Use strategy=fixed for menu
  • Loading branch information
humphd authored Feb 15, 2024
1 parent ca5d7b6 commit 4016e91
Showing 1 changed file with 77 additions and 13 deletions.
90 changes: 77 additions & 13 deletions src/components/CodeHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
useColorModeValue,
Text,
Box,
Menu,
MenuButton,
MenuItem,
MenuList,
} from "@chakra-ui/react";
import { TbCopy, TbDownload, TbRun, TbExternalLink } from "react-icons/tb";

Expand All @@ -32,7 +36,7 @@ function CodeHeader({
codeDownloadFilename,
}: PreHeaderProps) {
const { onCopy } = useClipboard(code);
const { info } = useAlert();
const { info, error } = useAlert();
// Only show the "Run" button for JS code blocks, and only when we aren't already loading
const shouldShowRunButton = isRunnable(language) && onPrompt;

Expand All @@ -52,7 +56,61 @@ function CodeHeader({
});
}, [info, code, codeDownloadFilename]);

const handleRun = useCallback(async () => {
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 (err) {
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;
}
Expand Down Expand Up @@ -135,17 +193,23 @@ function CodeHeader({
</Box>
<ButtonGroup isAttached pr={2}>
{shouldShowRunButton && (
<IconButton
size="sm"
aria-label="Run code"
title="Run code"
icon={<TbRun />}
color="gray.600"
_dark={{ color: "gray.300" }}
variant="ghost"
onClick={handleRun}
isDisabled={isLoading}
/>
<Menu strategy="fixed">
<MenuButton
as={IconButton}
size="sm"
aria-label="Run code"
title="Run code"
icon={<TbRun />}
color="gray.600"
_dark={{ color: "gray.300" }}
variant="ghost"
isDisabled={isLoading}
/>
<MenuList>
<MenuItem onClick={handleRunBrowser}>Run in Browser</MenuItem>
<MenuItem onClick={handleRunRemote}>Run on Server</MenuItem>
</MenuList>
</Menu>
)}
<IconButton
size="sm"
Expand Down

0 comments on commit 4016e91

Please sign in to comment.