frontend/src/components/PreviewPanel.jsx

122 lines
3.1 KiB
React
Raw Normal View History

2025-04-28 12:13:10 -07:00
import React, { useState } from 'react';
export default function PreviewPanel({ code }) {
const [gameUrl, setGameUrl] = useState('');
const [settings, setSettings] = useState(null);
const [loadingSetup, setLoadingSetup] = useState(false);
2025-05-07 13:26:17 -07:00
const fetchBoard = async () => {
if (!gameUrl.trim()) return;
setLoadingSetup(true);
2025-04-28 12:13:10 -07:00
try {
2025-05-07 13:26:17 -07:00
const res = await fetch(
`/api/fetch-board?url=${encodeURIComponent(gameUrl.trim())}`
);
if (!res.ok) throw new Error('Fetch board failed');
setSettings(await res.json());
2025-04-28 12:13:10 -07:00
} catch (err) {
console.error(err);
2025-05-07 13:26:17 -07:00
} finally {
setLoadingSetup(false);
2025-04-28 12:13:10 -07:00
}
};
const gameId = gameUrl.trim().split('/').pop();
return (
2025-05-07 13:26:17 -07:00
<div style={{
flex: 1,
background: '#1a1a1a',
borderRadius: '12px',
boxShadow: '0 0 15px #d300c5, 0 0 25px #ff2a6d',
border: '1px solid #d300c5',
padding: '1rem',
color: '#eee',
display: 'flex',
flexDirection: 'column',
boxSizing: 'border-box'
}}>
<div style={{
backgroundColor: '#d300c5',
height: '6px',
borderRadius: '3px 3px 0 0',
marginBottom: '0.5rem'
}} />
<h3 style={{
fontSize: '1.2rem',
margin: '0 0 1rem',
color: '#d300c5',
textShadow: '0 0 5px #d300c5',
textAlign: 'center'
}}>
🎯 Live Arena Output
</h3>
2025-04-28 12:13:10 -07:00
2025-05-07 13:26:17 -07:00
<div style={{
background: 'rgba(255,255,255,0.05)',
border: '1px solid #ff2a6d',
borderRadius: '8px',
padding: '1rem',
marginBottom: '1rem'
}}>
2025-04-28 12:13:10 -07:00
<input
type="text"
placeholder="Game URL"
value={gameUrl}
2025-05-07 13:26:17 -07:00
onChange={e => setGameUrl(e.target.value)}
style={{
width: '100%',
padding: '0.5rem',
marginBottom: '0.75rem',
border: 'none',
borderRadius: 4,
background: 'transparent',
color: '#fff',
outline: 'none'
}}
2025-04-28 12:13:10 -07:00
/>
2025-05-07 13:26:17 -07:00
<button
type="button"
onClick={fetchBoard}
disabled={!gameUrl.trim() || loadingSetup}
style={{
width: '100%',
padding: '0.75rem',
backgroundColor: '#ff2a6d',
border: 'none',
borderRadius: '20px',
color: '#fff',
fontWeight: 'bold',
cursor: 'pointer'
}}
>
{loadingSetup ? 'Loading…' : 'FETCH BOARD'}
2025-04-28 12:13:10 -07:00
</button>
</div>
2025-05-07 13:26:17 -07:00
{/* <div style={{
background: 'rgba(255,255,255,0.05)',
border: '1px solid #ff2a6d',
borderRadius: '8px',
padding: '1rem',
marginBottom: '1rem'
}}>
</div> */}
<div style={{
flex: 1,
overflow: 'hidden',
borderRadius: '8px'
}}>
{settings && gameId && (
2025-04-28 12:13:10 -07:00
<iframe
2025-05-07 13:26:17 -07:00
src={`/proxy/?game=${encodeURIComponent(gameId)}&autoplay=false&showControls=true`}
style={{ width: '100%', height: '100%', border: 'none' }}
2025-04-28 12:13:10 -07:00
title="Battlesnake Board"
/>
2025-05-07 13:26:17 -07:00
)}
</div>
2025-04-28 12:13:10 -07:00
</div>
);
}