Spaces:
Sleeping
Sleeping
Update src/App.js
Browse files- src/App.js +165 -147
src/App.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
const API_URL = "/prolog/query"; // change
|
| 4 |
|
| 5 |
-
|
| 6 |
const [query, setQuery] = useState("member(X, [1,2,3]).");
|
| 7 |
const [maxSolutions, setMaxSolutions] = useState(10);
|
| 8 |
const [loading, setLoading] = useState(false);
|
|
@@ -21,24 +22,18 @@ export default function App() {
|
|
| 21 |
try {
|
| 22 |
const res = await fetch(API_URL, {
|
| 23 |
method: "POST",
|
| 24 |
-
headers: {
|
| 25 |
-
"Content-Type": "application/json",
|
| 26 |
-
},
|
| 27 |
body: JSON.stringify({
|
| 28 |
query,
|
| 29 |
max_solutions: Number(maxSolutions) || 10,
|
| 30 |
}),
|
| 31 |
});
|
| 32 |
|
| 33 |
-
if (!res.ok) {
|
| 34 |
-
throw new Error(`HTTP ${res.status}`);
|
| 35 |
-
}
|
| 36 |
|
| 37 |
const data = await res.json();
|
| 38 |
|
| 39 |
-
if (!data.ok)
|
| 40 |
-
throw new Error(data.error || "Prolog query failed");
|
| 41 |
-
}
|
| 42 |
|
| 43 |
setSolutions(data.solutions || []);
|
| 44 |
setRawOutput(data.raw || "");
|
|
@@ -50,7 +45,7 @@ export default function App() {
|
|
| 50 |
solutions: data.solutions || [],
|
| 51 |
raw: data.raw || "",
|
| 52 |
},
|
| 53 |
-
...prev,
|
| 54 |
]);
|
| 55 |
} catch (err) {
|
| 56 |
console.error(err);
|
|
@@ -67,150 +62,173 @@ export default function App() {
|
|
| 67 |
};
|
| 68 |
|
| 69 |
return (
|
| 70 |
-
<div className="
|
| 71 |
-
<div className="
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
</h1>
|
| 78 |
-
<p
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
value={query}
|
| 94 |
-
onChange={(e) => setQuery(e.target.value)}
|
| 95 |
-
placeholder="member(X, [1,2,3])."
|
| 96 |
-
/>
|
| 97 |
-
|
| 98 |
-
<div className="flex items-center gap-3">
|
| 99 |
-
<label className="text-xs text-slate-400">
|
| 100 |
-
Max solutions:
|
| 101 |
-
<input
|
| 102 |
-
type="number"
|
| 103 |
-
min={1}
|
| 104 |
-
max={100}
|
| 105 |
-
value={maxSolutions}
|
| 106 |
-
onChange={(e) => setMaxSolutions(e.target.value)}
|
| 107 |
-
className="ml-2 w-20 rounded-lg border border-slate-700 bg-slate-950/60 px-2 py-1 text-xs text-slate-100 focus:outline-none focus:ring-2 focus:ring-emerald-500/70 focus:border-emerald-500/70"
|
| 108 |
-
/>
|
| 109 |
-
</label>
|
| 110 |
-
|
| 111 |
-
<button
|
| 112 |
-
type="submit"
|
| 113 |
-
disabled={loading || !query.trim()}
|
| 114 |
-
className="ml-auto inline-flex items-center rounded-xl bg-emerald-500 px-4 py-1.5 text-sm font-medium text-slate-950 shadow-lg shadow-emerald-500/30 hover:bg-emerald-400 disabled:opacity-50 disabled:cursor-not-allowed transition"
|
| 115 |
-
>
|
| 116 |
-
{loading ? "Running…" : "Run query"}
|
| 117 |
-
</button>
|
| 118 |
-
</div>
|
| 119 |
-
|
| 120 |
-
{error && (
|
| 121 |
-
<div className="mt-2 rounded-xl border border-rose-500/40 bg-rose-950/50 px-3 py-2 text-xs text-rose-200">
|
| 122 |
-
<strong>Error:</strong> {error}
|
| 123 |
</div>
|
| 124 |
-
)}
|
| 125 |
-
</form>
|
| 126 |
-
|
| 127 |
-
{/* Results */}
|
| 128 |
-
<section className="space-y-3">
|
| 129 |
-
<h2 className="text-sm font-semibold text-slate-300">
|
| 130 |
-
Solutions
|
| 131 |
-
</h2>
|
| 132 |
-
<div className="rounded-2xl border border-slate-800 bg-slate-900/60 p-4 min-h-[80px]">
|
| 133 |
-
{loading && (
|
| 134 |
-
<p className="text-xs text-slate-400">Running query…</p>
|
| 135 |
-
)}
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
{
|
| 144 |
-
<
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
key={i}
|
| 148 |
-
className="rounded-xl bg-slate-950/70 px-3 py-2 border border-slate-800/80"
|
| 149 |
-
>
|
| 150 |
-
<span className="text-slate-400">Solution {i + 1}:</span>
|
| 151 |
-
<pre className="mt-1 whitespace-pre-wrap font-mono text-[11px] text-emerald-200">
|
| 152 |
-
{JSON.stringify(sol, null, 2)}
|
| 153 |
-
</pre>
|
| 154 |
-
</li>
|
| 155 |
-
))}
|
| 156 |
-
</ol>
|
| 157 |
)}
|
| 158 |
-
</
|
| 159 |
-
|
| 160 |
-
{/*
|
| 161 |
-
<
|
| 162 |
-
<
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
<p className="
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
</p>
|
| 175 |
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
</div>
|
| 177 |
-
</
|
| 178 |
</section>
|
| 179 |
-
</div>
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
<p className="font-mono text-[11px] text-slate-200 truncate">
|
| 198 |
-
{item.query}
|
| 199 |
-
</p>
|
| 200 |
-
<p className="mt-1 text-[10px] text-slate-500">
|
| 201 |
-
{item.solutions.length} solution
|
| 202 |
-
{item.solutions.length === 1 ? "" : "s"}
|
| 203 |
-
</p>
|
| 204 |
-
</button>
|
| 205 |
-
))}
|
| 206 |
-
</div>
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
</div>
|
| 215 |
);
|
| 216 |
}
|
|
|
|
|
|
|
|
|
| 1 |
+
// src/App.js
|
| 2 |
+
import React, { useState } from "react";
|
| 3 |
|
| 4 |
+
const API_URL = "/prolog/query"; // change if your backend is elsewhere
|
| 5 |
|
| 6 |
+
function App() {
|
| 7 |
const [query, setQuery] = useState("member(X, [1,2,3]).");
|
| 8 |
const [maxSolutions, setMaxSolutions] = useState(10);
|
| 9 |
const [loading, setLoading] = useState(false);
|
|
|
|
| 22 |
try {
|
| 23 |
const res = await fetch(API_URL, {
|
| 24 |
method: "POST",
|
| 25 |
+
headers: { "Content-Type": "application/json" },
|
|
|
|
|
|
|
| 26 |
body: JSON.stringify({
|
| 27 |
query,
|
| 28 |
max_solutions: Number(maxSolutions) || 10,
|
| 29 |
}),
|
| 30 |
});
|
| 31 |
|
| 32 |
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
|
|
|
|
|
| 33 |
|
| 34 |
const data = await res.json();
|
| 35 |
|
| 36 |
+
if (!data.ok) throw new Error(data.error || "Prolog query failed");
|
|
|
|
|
|
|
| 37 |
|
| 38 |
setSolutions(data.solutions || []);
|
| 39 |
setRawOutput(data.raw || "");
|
|
|
|
| 45 |
solutions: data.solutions || [],
|
| 46 |
raw: data.raw || "",
|
| 47 |
},
|
| 48 |
+
...prev.slice(0, 19), // keep last 20
|
| 49 |
]);
|
| 50 |
} catch (err) {
|
| 51 |
console.error(err);
|
|
|
|
| 62 |
};
|
| 63 |
|
| 64 |
return (
|
| 65 |
+
<div className="app-root">
|
| 66 |
+
<div className="app-bg-gradient" />
|
| 67 |
+
|
| 68 |
+
<main className="app-shell">
|
| 69 |
+
{/* Header */}
|
| 70 |
+
<header className="app-header">
|
| 71 |
+
<div>
|
| 72 |
+
<h1>SWI-Prolog Playground</h1>
|
| 73 |
+
<p>Experiment with Prolog queries and inspect solutions.</p>
|
| 74 |
+
</div>
|
| 75 |
+
<span className="app-badge">Beta</span>
|
| 76 |
+
</header>
|
| 77 |
+
|
| 78 |
+
<div className="app-layout">
|
| 79 |
+
{/* Left column */}
|
| 80 |
+
<section className="app-main-panel">
|
| 81 |
+
{/* Query card */}
|
| 82 |
+
<form className="card card-query" onSubmit={runQuery}>
|
| 83 |
+
<div className="card-header">
|
| 84 |
+
<h2>Query</h2>
|
| 85 |
+
<span className="card-subtitle">
|
| 86 |
+
Enter a Prolog goal and run it on the backend.
|
| 87 |
+
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
<label className="field-label">Prolog goal</label>
|
| 91 |
+
<textarea
|
| 92 |
+
className="field-textarea"
|
| 93 |
+
value={query}
|
| 94 |
+
onChange={(e) => setQuery(e.target.value)}
|
| 95 |
+
placeholder="member(X, [1,2,3])."
|
| 96 |
+
rows={4}
|
| 97 |
+
/>
|
| 98 |
+
|
| 99 |
+
<div className="field-row">
|
| 100 |
+
<div className="field-inline">
|
| 101 |
+
<label className="field-label">Max solutions</label>
|
| 102 |
+
<input
|
| 103 |
+
type="number"
|
| 104 |
+
min={1}
|
| 105 |
+
max={100}
|
| 106 |
+
value={maxSolutions}
|
| 107 |
+
onChange={(e) => setMaxSolutions(e.target.value)}
|
| 108 |
+
className="field-input small"
|
| 109 |
+
/>
|
| 110 |
+
</div>
|
| 111 |
+
<div className="spacer" />
|
| 112 |
+
<button
|
| 113 |
+
type="submit"
|
| 114 |
+
disabled={loading || !query.trim()}
|
| 115 |
+
className="button-primary"
|
| 116 |
+
>
|
| 117 |
+
{loading ? "Running…" : "Run query"}
|
| 118 |
+
</button>
|
| 119 |
+
</div>
|
| 120 |
|
| 121 |
+
{error && (
|
| 122 |
+
<div className="alert alert-error">
|
| 123 |
+
<strong>Error:</strong> {error}
|
| 124 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
)}
|
| 126 |
+
</form>
|
| 127 |
+
|
| 128 |
+
{/* Solutions card */}
|
| 129 |
+
<section className="card card-results">
|
| 130 |
+
<div className="card-header">
|
| 131 |
+
<h2>Solutions</h2>
|
| 132 |
+
<span className="card-subtitle">
|
| 133 |
+
Returned as structured bindings per solution.
|
| 134 |
+
</span>
|
| 135 |
+
</div>
|
| 136 |
+
|
| 137 |
+
<div className="results-body">
|
| 138 |
+
{loading && (
|
| 139 |
+
<p className="muted">Running query, please wait…</p>
|
| 140 |
+
)}
|
| 141 |
+
|
| 142 |
+
{!loading && solutions.length === 0 && !rawOutput && !error && (
|
| 143 |
+
<p className="muted">
|
| 144 |
+
No results yet. Run a query to see solutions here.
|
| 145 |
</p>
|
| 146 |
)}
|
| 147 |
+
|
| 148 |
+
{!loading && solutions.length > 0 && (
|
| 149 |
+
<ol className="solutions-list">
|
| 150 |
+
{solutions.map((sol, i) => (
|
| 151 |
+
<li key={i} className="solution-item">
|
| 152 |
+
<div className="solution-header">
|
| 153 |
+
<span>Solution {i + 1}</span>
|
| 154 |
+
</div>
|
| 155 |
+
<pre className="solution-json">
|
| 156 |
+
{JSON.stringify(sol, null, 2)}
|
| 157 |
+
</pre>
|
| 158 |
+
</li>
|
| 159 |
+
))}
|
| 160 |
+
</ol>
|
| 161 |
+
)}
|
| 162 |
+
</div>
|
| 163 |
+
|
| 164 |
+
<div className="raw-output">
|
| 165 |
+
<div className="raw-header">
|
| 166 |
+
<span>Raw Prolog output</span>
|
| 167 |
+
</div>
|
| 168 |
+
<div className="raw-body">
|
| 169 |
+
{rawOutput ? (
|
| 170 |
+
<pre className="raw-pre">{rawOutput}</pre>
|
| 171 |
+
) : (
|
| 172 |
+
<p className="muted">
|
| 173 |
+
When the backend returns REPL-style output, it will show
|
| 174 |
+
up here (e.g. <code>X = 1 ; X = 2 ; X = 3.</code>).
|
| 175 |
+
</p>
|
| 176 |
+
)}
|
| 177 |
+
</div>
|
| 178 |
</div>
|
| 179 |
+
</section>
|
| 180 |
</section>
|
|
|
|
| 181 |
|
| 182 |
+
{/* Right column – history */}
|
| 183 |
+
<aside className="app-side-panel">
|
| 184 |
+
<section className="card card-history">
|
| 185 |
+
<div className="card-header">
|
| 186 |
+
<h2>History</h2>
|
| 187 |
+
<span className="card-subtitle">
|
| 188 |
+
Click a past query to reload it.
|
| 189 |
+
</span>
|
| 190 |
+
</div>
|
| 191 |
+
|
| 192 |
+
<div className="history-list">
|
| 193 |
+
{history.length === 0 && (
|
| 194 |
+
<p className="muted">
|
| 195 |
+
Run some queries to see them appear here.
|
| 196 |
+
</p>
|
| 197 |
+
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
{history.map((item) => (
|
| 200 |
+
<button
|
| 201 |
+
key={item.id}
|
| 202 |
+
type="button"
|
| 203 |
+
className="history-item"
|
| 204 |
+
onClick={() => loadFromHistory(item)}
|
| 205 |
+
>
|
| 206 |
+
<div className="history-query" title={item.query}>
|
| 207 |
+
{item.query}
|
| 208 |
+
</div>
|
| 209 |
+
<div className="history-meta">
|
| 210 |
+
{item.solutions.length} solution
|
| 211 |
+
{item.solutions.length === 1 ? "" : "s"}
|
| 212 |
+
</div>
|
| 213 |
+
</button>
|
| 214 |
+
))}
|
| 215 |
+
</div>
|
| 216 |
+
</section>
|
| 217 |
+
|
| 218 |
+
<section className="card card-footer">
|
| 219 |
+
<p className="footer-text">
|
| 220 |
+
Backend expected at{" "}
|
| 221 |
+
<code className="code-inline">{API_URL}</code>.
|
| 222 |
+
<br />
|
| 223 |
+
Update <code className="code-inline">API_URL</code> in{" "}
|
| 224 |
+
<code className="code-inline">App.js</code> if needed.
|
| 225 |
+
</p>
|
| 226 |
+
</section>
|
| 227 |
+
</aside>
|
| 228 |
+
</div>
|
| 229 |
+
</main>
|
| 230 |
</div>
|
| 231 |
);
|
| 232 |
}
|
| 233 |
+
|
| 234 |
+
export default App;
|