import { useStore } from '@nanostores/react'; import { memo, useCallback, useEffect, useRef, useState } from 'react'; import { PortDropdown } from './PortDropdown'; import { IconButton } from '~/components/ui/IconButton'; import { workbenchStore } from '~/lib/stores/workbench'; export const Preview = memo(() => { const iframeRef = useRef(null); const inputRef = useRef(null); const [activePreviewIndex, setActivePreviewIndex] = useState(0); const [isPortDropdownOpen, setIsPortDropdownOpen] = useState(false); const hasSelectedPreview = useRef(false); const previews = useStore(workbenchStore.previews); const activePreview = previews[activePreviewIndex]; const [url, setUrl] = useState(''); const [iframeUrl, setIframeUrl] = useState(); useEffect(() => { if (!activePreview) { setUrl(''); setIframeUrl(undefined); return; } const { baseUrl } = activePreview; setUrl(baseUrl); setIframeUrl(baseUrl); }, [activePreview, iframeUrl]); const validateUrl = useCallback( (value: string) => { if (!activePreview) { return false; } const { baseUrl } = activePreview; if (value === baseUrl) { return true; } else if (value.startsWith(baseUrl)) { return ['/', '?', '#'].includes(value.charAt(baseUrl.length)); } return false; }, [activePreview], ); const findMinPortIndex = useCallback( (minIndex: number, preview: { port: number }, index: number, array: { port: number }[]) => { return preview.port < array[minIndex].port ? index : minIndex; }, [], ); // when previews change, display the lowest port if user hasn't selected a preview useEffect(() => { if (previews.length > 1 && !hasSelectedPreview.current) { const minPortIndex = previews.reduce(findMinPortIndex, 0); setActivePreviewIndex(minPortIndex); } }, [previews]); const reloadPreview = () => { if (iframeRef.current) { iframeRef.current.src = iframeRef.current.src; } }; return (
{isPortDropdownOpen && (
setIsPortDropdownOpen(false)} /> )}
{ console.log('Button clicked'); if (activePreview?.baseUrl) { console.log('Active preview baseUrl:', activePreview.baseUrl); // Extract the preview ID from the WebContainer URL const match = activePreview.baseUrl.match( /^https?:\/\/([^.]+)\.local-credentialless\.webcontainer-api\.io/, ); console.log('URL match:', match); if (match) { const previewId = match[1]; console.log('Preview ID:', previewId); // Open in new tab using our route with absolute path // Use the current port for development const port = window.location.port ? `:${window.location.port}` : ''; const previewUrl = `${window.location.protocol}//${window.location.hostname}${port}/webcontainer/preview/${previewId}`; console.log('Opening URL:', previewUrl); const newWindow = window.open(previewUrl, '_blank', 'noopener,noreferrer'); // Force focus on the new window if (newWindow) { newWindow.focus(); } else { console.warn('Failed to open new window'); } } else { console.warn('[Preview] Invalid WebContainer URL:', activePreview.baseUrl); } } else { console.warn('No active preview available'); } }} title="Open Preview in New Tab" />
{ setUrl(event.target.value); }} onKeyDown={(event) => { if (event.key === 'Enter' && validateUrl(url)) { setIframeUrl(url); if (inputRef.current) { inputRef.current.blur(); } } }} />
{previews.length > 1 && ( (hasSelectedPreview.current = value)} setIsDropdownOpen={setIsPortDropdownOpen} previews={previews} /> )}
{activePreview ? (