From facdf9357e576f6d47e7dd23f4f7b2b34f6a8d30 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Mon, 17 Mar 2025 22:19:18 +0900
Subject: commit files

---
 src/components/FuncExpectedAnswer.tsx | 59 +++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 src/components/FuncExpectedAnswer.tsx

(limited to 'src/components/FuncExpectedAnswer.tsx')

diff --git a/src/components/FuncExpectedAnswer.tsx b/src/components/FuncExpectedAnswer.tsx
new file mode 100644
index 0000000..f412ba5
--- /dev/null
+++ b/src/components/FuncExpectedAnswer.tsx
@@ -0,0 +1,59 @@
+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<string>("123");
+	const [debouncedArgument] = useDebounce(argument, 1000);
+	const [result, setResult] = useState<string>("");
+	const [loading, setLoading] = useState<boolean>(true);
+
+	const handleArgumentChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+		setArgument(e.target.value);
+	};
+
+	useEffect(() => {
+		if (debouncedArgument === "") {
+			setResult("<empty>");
+			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, "<answer is masked>"));
+			setLoading(false);
+		});
+	}, [debouncedArgument, quiz.func]);
+
+	return (
+		<div>
+			<code>
+				{`f(`}
+				<input type="text" value={argument} onChange={handleArgumentChange} />
+				{`)`}
+			</code>
+			は <code>{loading ? "running..." : result}</code> を返す。
+		</div>
+	);
+}
+
+export default FuncExpectedAnswer;
-- 
cgit v1.2.3-70-g09d2

