import { useState, useRef, useCallback, useEffect } from 'react';
import { Play, Square, RefreshCw, ExternalLink, X } from 'lucide-react';
interface ScriptPreviewProps {
htmlContent: string;
scriptContent: string;
fileName: string;
onClose: () => void;
}
const NAV_INTERCEPTOR = `
`;
export default function ScriptPreview({ htmlContent, scriptContent, fileName, onClose }: ScriptPreviewProps) {
const [running, setRunning] = useState(false);
const [key, setKey] = useState(0);
const [pendingUrl, setPendingUrl] = useState(null);
const iframeRef = useRef(null);
// Listen for navigation events from the iframe
useEffect(() => {
function handler(e: MessageEvent) {
if (e.data?.type === 'preview-navigate' && e.data.url) {
setPendingUrl(e.data.url);
}
}
window.addEventListener('message', handler);
return () => window.removeEventListener('message', handler);
}, []);
const buildFullHtml = useCallback(() => {
let fullHtml = htmlContent;
if (scriptContent) {
const scriptTag = ``;
if (fullHtml.includes('