import { useRef, useEffect, useState } from 'react'; import { useLivePreview } from '@/hooks/useLivePreview'; import { RefreshCw, Monitor, Tablet, Smartphone, ExternalLink } from 'lucide-react'; import { cn } from '@/lib/utils'; const viewports = [ { icon: Smartphone, width: 375, label: 'Mobile' }, { icon: Tablet, width: 768, label: 'Tablet' }, { icon: Monitor, width: '100%', label: 'Desktop' }, ]; export default function LivePreview() { const { srcdoc } = useLivePreview(); const iframeRef = useRef(null); const [viewportIndex, setViewportIndex] = useState(2); const [consoleOutput, setConsoleOutput] = useState>([]); const [showConsole, setShowConsole] = useState(false); const [refreshKey, setRefreshKey] = useState(0); useEffect(() => { const handleMessage = (event: MessageEvent) => { if (event.data?.type === 'console') { setConsoleOutput((prev) => [...prev.slice(-50), { method: event.data.method, args: event.data.args }]); } }; window.addEventListener('message', handleMessage); return () => window.removeEventListener('message', handleMessage); }, []); const viewport = viewports[viewportIndex]; const handleOpenInNewTab = () => { const newWindow = window.open('', '_blank'); if (newWindow) { newWindow.document.open(); newWindow.document.write(srcdoc); newWindow.document.close(); newWindow.document.title = 'GLMPilot Preview'; } }; return (
{/* Toolbar */}
{viewports.map((vp, i) => ( ))}
{/* Preview */}