import type { Quiz } from "../quiz"; import { execPHP } from "../exec_php"; import React, { useState, useEffect } from "react"; import { useDebounce } from "use-debounce"; type Props = { quiz: Quiz; }; function FuncExpectedAnswer({ quiz }: Props) { const [argument, setArgument] = useState("123"); const [debouncedArgument] = useDebounce(argument, 1000); const [result, setResult] = useState(""); const [loading, setLoading] = useState(true); const handleArgumentChange = (e: React.ChangeEvent) => { setArgument(e.target.value); }; useEffect(() => { if (debouncedArgument === "") { setResult(""); return; } setLoading(true); setResult(""); const code = ` function f($x) { return ${quiz.func}($x); } try { var_dump(f(${debouncedArgument})); } catch (\\Throwable $e) { echo $e->getMessage(), PHP_EOL; } `; execPHP(code).then((result) => { const output = result.stdout + result.stderr; setResult(output.replaceAll(quiz.func, "")); setLoading(false); }); }, [debouncedArgument, quiz.func]); return (
{`f(`} {`)`} {loading ? "running..." : result} を返す。
); } export default FuncExpectedAnswer;