({
code,
runCodeAction,
}: {
code: string;
runCodeAction: (v: string) => Promise<string>;
})
| 5 | import { useActionState } from "react"; |
| 6 | |
| 7 | export default function CodeRunnerServerAction({ |
| 8 | code, |
| 9 | runCodeAction, |
| 10 | }: { |
| 11 | code: string; |
| 12 | runCodeAction: (v: string) => Promise<string>; |
| 13 | }) { |
| 14 | const [state, action, isPending] = useActionState(async () => { |
| 15 | return runCodeAction(code); |
| 16 | }, null); |
| 17 | |
| 18 | return ( |
| 19 | <div> |
| 20 | {state === null ? ( |
| 21 | <div className="flex flex-col items-center justify-center"> |
| 22 | <p>Run the code to generate an output</p> |
| 23 | |
| 24 | <form action={action} className="mt-4"> |
| 25 | <button |
| 26 | disabled={isPending} |
| 27 | className="inline-flex rounded border border-gray-300 px-2.5 py-1 text-sm text-gray-600 transition enabled:hover:bg-gray-100 disabled:opacity-75" |
| 28 | > |
| 29 | <Spinner loading={isPending}> |
| 30 | <span className="inline-flex items-center gap-1"> |
| 31 | <PlayIcon className="size-4" /> |
| 32 | Run Code |
| 33 | </span> |
| 34 | </Spinner> |
| 35 | </button> |
| 36 | </form> |
| 37 | </div> |
| 38 | ) : ( |
| 39 | <div>{state}</div> |
| 40 | )} |
| 41 | </div> |
| 42 | ); |
| 43 | } |
nothing calls this directly
no outgoing calls
no test coverage detected