127 lines
3.9 KiB
React
127 lines
3.9 KiB
React
"use client";
|
|
|
|
import React from "react";
|
|
import { useState, useRef } from "react";
|
|
import DiagnosticsView from "@/components/diagnostics-view";
|
|
import SituationGraphView from "@/components/situation-graph-view";
|
|
|
|
const MAX_LENGTH = 10000;
|
|
|
|
export async function submitScenarioForStartCase(fetchImpl, scenario) {
|
|
return fetchImpl("/api/cases/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ scenario }),
|
|
});
|
|
}
|
|
|
|
export function ScenarioResultPanels({ status, result }) {
|
|
if (!result) return null;
|
|
|
|
const hasGraph = Boolean(result.situationGraph);
|
|
const hasQuestion = Boolean(result.selectedQuestion?.question);
|
|
const hasDiagnostics = Boolean(result.diagnostics);
|
|
|
|
return (
|
|
<>
|
|
{status === "error" && (
|
|
<div className="space-y-3">
|
|
{result.error && (
|
|
<div className="rounded-lg border border-red-300 bg-red-50 px-4 py-3 text-sm text-red-700 whitespace-pre-wrap">
|
|
Error: {result.error}
|
|
</div>
|
|
)}
|
|
{!hasGraph && !hasQuestion && (
|
|
<div className="rounded-lg border border-yellow-300 bg-yellow-50 px-4 py-2 text-sm text-yellow-800">
|
|
Validation failed — no structured graph output was produced.
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{(status === "success" || hasGraph || hasQuestion) && (
|
|
<SituationGraphView
|
|
situationGraph={result.situationGraph}
|
|
selectedQuestion={result.selectedQuestion}
|
|
/>
|
|
)}
|
|
|
|
{hasDiagnostics && <DiagnosticsView result={result} />}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function ScenarioForm() {
|
|
const [scenario, setScenario] = useState("");
|
|
const [status, setStatus] = useState("idle"); // idle | loading | error | success
|
|
const [result, setResult] = useState(null);
|
|
const textareaRef = useRef(null);
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
setStatus("loading");
|
|
setResult(null);
|
|
|
|
try {
|
|
const res = await submitScenarioForStartCase(fetch, scenario);
|
|
|
|
const data = await res.json();
|
|
|
|
if (res.ok && data.success) {
|
|
setStatus("success");
|
|
setResult(data);
|
|
} else {
|
|
setStatus("error");
|
|
setResult(data);
|
|
}
|
|
} catch (err) {
|
|
setStatus("error");
|
|
setResult({ error: err.message || "Network request failed" });
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<textarea
|
|
ref={textareaRef}
|
|
value={scenario}
|
|
onChange={(e) => setScenario(e.target.value)}
|
|
placeholder="Describe the scenario you want analysed..."
|
|
rows={10}
|
|
className="w-full rounded-lg border border-gray-300 px-4 py-3 text-sm focus:border-gray-500 focus:outline-none focus:ring-2 focus:ring-gray-400"
|
|
/>
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-xs text-gray-400">
|
|
{scenario.length}/{MAX_LENGTH}
|
|
</span>
|
|
<button
|
|
type="submit"
|
|
disabled={status === "loading" || !scenario.trim()}
|
|
className="rounded-lg bg-gray-900 px-6 py-2.5 text-sm font-medium text-white transition hover:bg-gray-700 disabled:cursor-not-allowed disabled:opacity-40"
|
|
>
|
|
{status === "loading" ? "Analysing..." : "Analyse"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
<ScenarioResultPanels status={status} result={result} />
|
|
|
|
{status === "loading" && (
|
|
<div className="py-12 text-center text-sm text-gray-400">
|
|
Waiting for model response...
|
|
</div>
|
|
)}
|
|
|
|
{/* Empty state */}
|
|
{status === "idle" && (
|
|
<div className="rounded-lg border border-dashed border-gray-300 bg-gray-50 px-6 py-8 text-center">
|
|
<p className="text-sm text-gray-400">
|
|
Enter a scenario above and click Analyse to begin.
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|