modified preview panel

This commit is contained in:
yoshi 2025-05-07 14:11:04 -07:00
parent 7f520ab1d9
commit 7842629c4b

View file

@ -1,28 +1,19 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
export default function PreviewPanel({ code }) { const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const [gameUrl, setGameUrl] = useState('');
const [settings, setSettings] = useState(null);
const [loadingSetup, setLoadingSetup] = useState(false);
const fetchBoard = async () => { export default function PreviewPanel({ code }) {
if (!gameUrl.trim()) return; const [gameId, setGameId] = useState('');
setLoadingSetup(true); const [submitted, setSubmitted] = useState(false);
try {
const res = await fetch( const isValid = uuidRegex.test(gameId);
`/api/fetch-board?url=${encodeURIComponent(gameUrl.trim())}`
); const handleClick = () => {
if (!res.ok) throw new Error('Fetch board failed'); if (isValid) {
setSettings(await res.json()); setSubmitted(true);
} catch (err) {
console.error(err);
} finally {
setLoadingSetup(false);
} }
}; };
const gameId = gameUrl.trim().split('/').pop();
return ( return (
<div style={{ <div style={{
flex: 1, flex: 1,
@ -61,9 +52,9 @@ export default function PreviewPanel({ code }) {
}}> }}>
<input <input
type="text" type="text"
placeholder="Game URL" placeholder="Game ID (UUID)"
value={gameUrl} value={gameId}
onChange={e => setGameUrl(e.target.value)} onChange={e => { setGameId(e.target.value.trim()); setSubmitted(false); }}
style={{ style={{
width: '100%', width: '100%',
padding: '0.5rem', padding: '0.5rem',
@ -75,44 +66,42 @@ export default function PreviewPanel({ code }) {
outline: 'none' outline: 'none'
}} }}
/> />
{!isValid && gameId && (
<p style={{ color: 'salmon', margin: '0 0 0.5rem' }}>
Invalid Game ID format.
</p>
)}
<button <button
type="button" type="button"
onClick={fetchBoard} onClick={handleClick}
disabled={!gameUrl.trim() || loadingSetup} disabled={!isValid}
style={{ style={{
width: '100%', width: '100%',
padding: '0.75rem', padding: '0.75rem',
backgroundColor: '#ff2a6d', backgroundColor: isValid ? '#ff2a6d' : '#555',
border: 'none', border: 'none',
borderRadius: '20px', borderRadius: '20px',
color: '#fff', color: '#fff',
fontWeight: 'bold', fontWeight: 'bold',
cursor: 'pointer' cursor: isValid ? 'pointer' : 'not-allowed'
}} }}
> >
{loadingSetup ? 'Loading…' : 'FETCH BOARD'} {submitted ? 'Loaded' : 'LOAD BOARD'}
</button> </button>
</div> </div>
{/* <div style={{
background: 'rgba(255,255,255,0.05)',
border: '1px solid #ff2a6d',
borderRadius: '8px',
padding: '1rem',
marginBottom: '1rem'
}}>
</div> */}
<div style={{ <div style={{
flex: 1, flex: 1,
overflow: 'hidden', overflow: 'hidden',
borderRadius: '8px' borderRadius: '8px'
}}> }}>
{settings && gameId && ( {submitted && (
<iframe <iframe
src={`/proxy/?game=${encodeURIComponent(gameId)}&autoplay=false&showControls=true`} src={`https://gameboard-service-aged-glitter-8141.fly.dev/?game=${encodeURIComponent(gameId)}&autoplay=false&showControls=true`}
style={{ width: '100%', height: '100%', border: 'none' }}
title="Battlesnake Board" title="Battlesnake Board"
style={{ width: '100%', height: '100%', border: 'none' }}
allowFullScreen
scrolling="no"
/> />
)} )}
</div> </div>