Spaces:
Running
Running
File size: 14,053 Bytes
aaa634c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | import { useState, useEffect } from 'react';
import { supabase } from '../supabaseClient';
interface MySQLStatusData {
active?: boolean;
title: string;
objective: string;
points: number;
challenge_type: string;
schema: Record<string, Array<{ name: string; type: string }>>;
completed: boolean;
total_score: number;
}
export default function MySQLPlayground() {
const [status, setStatus] = useState<MySQLStatusData | null>(null);
const [query, setQuery] = useState('');
const [running, setRunning] = useState(false);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const [results, setResults] = useState<{
columns: string[];
rows: any[];
rowsAffected: number;
isCorrect?: boolean;
} | null>(null);
const fetchStatus = async () => {
setLoading(true);
setError('');
try {
const { data: { session } } = await supabase.auth.getSession();
if (!session) {
setError('Unauthenticated session');
return;
}
const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/challenges/mysql/status`, {
headers: { 'Authorization': `Bearer ${session.access_token}` }
});
if (res.ok) {
const data = await res.json();
if (data.active) {
setStatus(data);
} else {
setStatus(null);
}
} else {
setError('Failed to fetch playground status.');
}
} catch {
setError('Connection timed out.');
} finally {
setLoading(false);
}
};
const handleInit = async () => {
setLoading(true);
setError('');
setResults(null);
try {
const { data: { session } } = await supabase.auth.getSession();
if (!session) {
setError('Unauthenticated session');
return;
}
const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/challenges/mysql/init`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${session.access_token}` }
});
if (res.ok) {
const data = await res.json();
setStatus({
active: true,
title: data.title,
objective: data.objective,
points: data.points,
schema: data.schema,
challenge_type: data.challenge_type,
completed: false,
total_score: data.total_score
});
} else {
setError('Failed to initialize mock database playground.');
}
} catch {
setError('Server connection error.');
} finally {
setLoading(false);
}
};
const handleRunQuery = async () => {
if (!query.trim()) return;
setRunning(true);
setError('');
setResults(null);
try {
const { data: { session } } = await supabase.auth.getSession();
if (!session) {
setError('Unauthenticated session');
return;
}
const res = await fetch(`${import.meta.env.VITE_API_BASE_URL}/api/challenges/mysql/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${session.access_token}`
},
body: JSON.stringify({ query })
});
if (res.ok) {
const data = await res.json();
if (data.success) {
setResults({
columns: data.columns,
rows: data.rows,
rowsAffected: data.rows_affected,
isCorrect: data.is_correct
});
if (data.is_correct) {
setStatus(prev => prev ? { ...prev, completed: true, total_score: data.total_score } : null);
}
if (data.schema) {
setStatus(prev => prev ? { ...prev, schema: data.schema } : null);
}
} else {
setError(data.error);
}
} else {
setError('Database server query failure.');
}
} catch {
setError('Query connection lost.');
} finally {
setRunning(false);
}
};
useEffect(() => {
fetchStatus();
}, []);
if (loading) {
return (
<div className="w-full h-full bg-paper-white p-6 flex flex-col items-center justify-center font-arcade text-[10px]">
<div className="animate-bounce mb-2">INITIALIZING SQL PLAYGROUND...</div>
<div className="w-48 h-4 bg-white border-[3px] border-ink-black overflow-hidden relative">
<div className="absolute inset-y-0 left-0 bg-[#0ea5e9] w-1/2 animate-pulse border-r-2 border-ink-black" />
</div>
</div>
);
}
// Dashboard state: no active database initialized
if (!status) {
return (
<div className="w-full h-full bg-paper-white p-6 flex flex-col justify-between items-center text-center text-ink-black text-xs md:text-sm font-sans select-none">
<div className="flex-1 flex flex-col justify-center items-center gap-4">
<div className="w-16 h-16 bg-white border-[3px] border-ink-black flex items-center justify-center shadow-[4px_4px_0_0_#1E293B]">
<span className="material-symbols-outlined text-4xl text-[#0ea5e9]" style={{ fontVariationSettings: "'FILL' 1" }}>
database
</span>
</div>
<h3 className="font-headline-md font-bold text-lg">Interactive MySQL Playground</h3>
<p className="max-w-md text-slate-600 leading-relaxed font-semibold">
Test and run relational queries against isolated schemas dynamically built by the AI generator. Includes full support for:
</p>
<div className="flex flex-wrap justify-center gap-2 font-mono text-[10px] text-slate-700 font-bold uppercase mt-1">
<span className="bg-blue-100 border border-blue-400 px-2 py-0.5">SELECT / JOIN</span>
<span className="bg-emerald-100 border border-emerald-400 px-2 py-0.5">UPDATE / DELETE</span>
<span className="bg-purple-100 border border-purple-400 px-2 py-0.5">WINDOW FUNCTIONS</span>
</div>
</div>
<div className="w-full border-t border-slate-200 pt-4 flex flex-col gap-2 shrink-0">
<button
onClick={handleInit}
className="w-full py-2.5 bg-[#0ea5e9] hover:bg-sky-400 text-white font-window-title font-bold text-xs uppercase border-[3px] border-ink-black shadow-[4px_4px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none transition-all cursor-pointer text-center"
>
CREATE NEW DATABASE INSTANCE
</button>
</div>
</div>
);
}
return (
<div className="w-full h-full bg-paper-white text-ink-black flex flex-col overflow-hidden text-xs md:text-sm">
{/* Play Workspace Scrollable Body */}
<div className="flex-1 overflow-y-auto p-4 custom-scrollbar flex flex-col gap-4">
{/* Objectives Badge Header */}
<div className="flex justify-between items-center bg-slate-100 border-[3px] border-ink-black p-3 shrink-0">
<div className="flex gap-2">
<span className="bg-blue-100 text-blue-800 px-2 py-0.5 border-2 border-ink-black font-bold text-[9px] uppercase">
{status.challenge_type}
</span>
<span className="bg-yellow-100 text-yellow-800 px-2 py-0.5 border-2 border-ink-black font-bold text-[9px]">
+{status.points} PTS
</span>
{status.completed ? (
<span className="bg-emerald-500 text-white px-2 py-0.5 border-2 border-ink-black font-bold text-[9px]">
SOLVED
</span>
) : (
<span className="bg-rose-500 text-white px-2 py-0.5 border-2 border-ink-black font-bold text-[9px]">
UNSOLVED
</span>
)}
</div>
<div className="font-arcade text-[8px] text-sky-700 font-bold bg-white border-2 border-ink-black px-2 py-0.5 shadow-[1px_1px_0_0_#1E293B]">
SCORE: {status.total_score}
</div>
</div>
{/* Objective banner */}
<div className="border-[3px] border-yellow-400 bg-yellow-50 p-3 rounded font-sans leading-relaxed text-xs">
<h4 className="font-arcade text-[9px] text-yellow-800 mb-1">MISSION OBJECTIVE</h4>
<p className="font-bold text-slate-800">{status.objective}</p>
</div>
{/* Schema Tree */}
<div className="border-[3px] border-ink-black bg-white p-3">
<div className="font-bold text-[10px] border-b border-slate-300 pb-1 mb-2 flex items-center gap-1.5">
<span className="material-symbols-outlined text-sm">schema</span>
<span>SCHEMA EXPLORER (MOCK INSTANCE)</span>
</div>
<div className="grid grid-cols-2 gap-3 max-h-[140px] overflow-y-auto custom-scrollbar font-mono text-[10px]">
{Object.entries(status.schema).map(([tableName, cols]) => (
<div key={tableName} className="border border-slate-300 p-2 bg-slate-50">
<div className="font-bold text-blue-700 border-b border-slate-200 pb-0.5 mb-1 flex items-center gap-1">
<span className="material-symbols-outlined text-[12px]">table_chart</span>
{tableName}
</div>
<ul className="space-y-0.5 text-slate-600 text-[9px]">
{cols.map((col, cIdx) => (
<li key={cIdx} className="flex justify-between">
<span>{col.name}</span>
<span className="text-slate-400 italic text-[8px]">{col.type}</span>
</li>
))}
</ul>
</div>
))}
</div>
</div>
{/* Query Input Editor */}
<div className="flex flex-col min-h-[120px]">
<div className="font-bold text-[10px] mb-1 tracking-wide uppercase flex items-center gap-1">
<span className="material-symbols-outlined text-sm text-sky-600">terminal</span>
<span>Enter SQL Query:</span>
</div>
<textarea
value={query}
onChange={(e) => setQuery(e.target.value)}
className="flex-1 w-full bg-slate-900 text-[#4ADE80] font-mono p-3 border-[3px] border-ink-black rounded outline-none focus:ring-2 focus:ring-sky-500 text-xs leading-relaxed resize-none"
placeholder="SELECT * FROM customers WHERE score > 100;"
spellCheck="false"
/>
</div>
{/* Run Button bar */}
<div className="flex gap-3">
<button
onClick={handleInit}
className="flex-1 bg-white hover:bg-slate-100 text-ink-black border-[3px] border-ink-black font-bold font-window-title text-xs py-2 shadow-[3px_3px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none cursor-pointer"
>
NEW PROBLEM
</button>
<button
disabled={running}
onClick={handleRunQuery}
className="flex-[2] bg-highlight-pink text-white font-window-title font-bold py-2 border-[3px] border-ink-black shadow-[3px_3px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none hover:bg-pink-400 transition-all disabled:opacity-50 cursor-pointer text-center text-xs"
>
{running ? 'EXECUTING SQL...' : 'EXECUTE SQL QUERY'}
</button>
</div>
{/* Query Error Console */}
{error && (
<div className="border-[3px] border-red-500 bg-red-50 text-red-800 p-3 font-mono text-xs whitespace-pre-wrap">
<div className="font-bold mb-1">SQL RUNTIME / SYNTAX ERROR:</div>
{error}
</div>
)}
{/* Tabular Result Grid */}
{results && (
<div className="border-[3px] border-ink-black bg-white p-3 flex flex-col">
<div className="font-bold text-[10px] border-b border-slate-300 pb-1 mb-2 flex justify-between items-center">
<span>QUERY OUTPUT BUFFER</span>
{results.isCorrect && (
<span className="text-green-600 font-bold animate-bounce text-[9px]">
✓ MISSION ACHIEVED!
</span>
)}
</div>
<div className="text-[9px] text-slate-500 mb-2 font-mono">
Rows Affected: {results.rowsAffected} | Total Returned: {results.rows.length}
</div>
{results.rows.length > 0 ? (
<div className="overflow-x-auto border border-slate-300 max-h-[180px] custom-scrollbar">
<table className="border-collapse w-full font-mono text-[9px] min-w-full">
<thead>
<tr className="bg-slate-100 border-b border-slate-300">
{results.columns.map((col, idx) => (
<th key={idx} className="border-r border-slate-300 p-1.5 text-left font-bold text-slate-700">
{col}
</th>
))}
</tr>
</thead>
<tbody className="divide-y divide-slate-200">
{results.rows.map((row, rIdx) => (
<tr key={rIdx} className="hover:bg-slate-50">
{results.columns.map((col, cIdx) => (
<td key={cIdx} className="border-r border-slate-200 p-1.5 text-slate-800 whitespace-nowrap">
{row[col] !== null ? String(row[col]) : <span className="text-slate-300 italic">NULL</span>}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
) : (
<div className="p-3 border border-dashed border-slate-300 text-center text-slate-400 italic font-sans text-xs">
Query executed successfully. Result set is empty (or modification completed).
</div>
)}
</div>
)}
</div>
</div>
);
}
|