import { useState, useEffect, useCallback } from 'react'; import { fsManager } from '@/lib/fs'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Button } from '@/components/ui/button'; import { RefreshCw } from 'lucide-react'; import { FileTree } from './FileTree'; import { FileEditor } from './FileEditor'; interface PreviewPaneProps { projectId: string; activeTab: 'preview' | 'code'; } export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { const [selectedFile, setSelectedFile] = useState(null); const [fileContent, setFileContent] = useState(''); const [isLoading, setIsLoading] = useState(false); const [hasBuiltProject, setHasBuiltProject] = useState(false); const [previewHtml, setPreviewHtml] = useState(''); const loadFileContent = useCallback(async (filePath: string) => { setIsLoading(true); try { const content = await fsManager.readFile(projectId, filePath); setFileContent(content); } catch (_error) { console.error('Failed to load file:', _error); setFileContent(''); } finally { setIsLoading(false); } }, [projectId]); const checkForBuiltProject = useCallback(async () => { try { const exists = await fsManager.fileExists(projectId, 'dist/index.html'); setHasBuiltProject(exists); if (exists) { // Load the built HTML content const htmlContent = await fsManager.readFile(projectId, 'dist/index.html'); // Process the HTML to add base tag for asset resolution const processedHtml = processHtmlForPreview(htmlContent, projectId); setPreviewHtml(processedHtml); } } catch (error) { console.error('Failed to check for built project:', error); setHasBuiltProject(false); } }, [projectId]); const processHtmlForPreview = (html: string, projectId: string): string => { try { // Create a temporary DOM to process the HTML const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); // Rewrite script src attributes const scripts = doc.querySelectorAll('script[src]'); scripts.forEach((script) => { const src = script.getAttribute('src'); if (src && !src.startsWith('http') && !src.startsWith('//')) { // Convert relative path to raw file path const cleanPath = src.startsWith('./') ? src.slice(2) : src.startsWith('/') ? src.slice(1) : src; const newSrc = `/project/${projectId}/files/dist/${cleanPath}`; script.setAttribute('src', newSrc); } }); // Rewrite link href attributes (for stylesheets and other resources) const links = doc.querySelectorAll('link[href]'); links.forEach((link) => { const href = link.getAttribute('href'); if (href && !href.startsWith('http') && !href.startsWith('//')) { // Convert relative path to raw file path const cleanPath = href.startsWith('./') ? href.slice(2) : href.startsWith('/') ? href.slice(1) : href; const newHref = `/project/${projectId}/files/dist/${cleanPath}`; link.setAttribute('href', newHref); } }); return doc.documentElement.outerHTML; } catch (error) { console.error('Failed to process HTML for preview:', error); return html; // Return original HTML if processing fails } }; useEffect(() => { if (selectedFile) { loadFileContent(selectedFile); } }, [selectedFile, loadFileContent]); useEffect(() => { checkForBuiltProject(); }, [checkForBuiltProject]); const handleFileSelect = (filePath: string) => { setSelectedFile(filePath); }; const handleFileSave = async (content: string) => { if (!selectedFile) return; try { await fsManager.fs.promises.writeFile(`/projects/${projectId}/${selectedFile}`, content); setFileContent(content); } catch (error) { console.error('Failed to save file:', error); } }; return (
{hasBuiltProject ? (
Project Preview