From fd7ac39a749c7d165eea5bfe0d64933039b9b958 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 18 Jul 2025 15:59:20 -0500 Subject: [PATCH] Start making preview pane work --- src/components/Shakespeare/ChatPane.tsx | 12 +- src/components/Shakespeare/PreviewPane.tsx | 129 +++++++++++++++++++-- src/test/setup.ts | 63 +++++++++- 3 files changed, 191 insertions(+), 13 deletions(-) diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 52ea9082..7449aa76 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -108,7 +108,11 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { try { if (item.isDirectory()) { console.log(`Creating directory: ${destItemPath}`); - await fsManager.fs.promises.mkdir(destItemPath, { recursive: true }); + try { + await fsManager.fs.promises.mkdir(destItemPath); + } catch { + // Directory might already exist + } await copyDirectory(sourceItemPath, destItemPath); } else { console.log(`Copying file: ${sourceItemPath} -> ${destItemPath}`); @@ -132,7 +136,11 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { try { if (item.isDirectory()) { console.log(`Processing directory: ${item.name}`); - await fsManager.fs.promises.mkdir(destPath, { recursive: true }); + try { + await fsManager.fs.promises.mkdir(destPath); + } catch { + // Directory might already exist + } await copyDirectory(sourcePath, destPath); } else { console.log(`Processing file: ${item.name}`); diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index 2daf9991..926651f9 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -2,6 +2,8 @@ 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'; @@ -15,6 +17,8 @@ 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); @@ -29,12 +33,86 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { } }, [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 handle relative asset paths + const processedHtml = await processHtmlForPreview(htmlContent, projectId); + setPreviewHtml(processedHtml); + } + } catch (error) { + console.error('Failed to check for built project:', error); + setHasBuiltProject(false); + } + }, [projectId]); + + const processHtmlForPreview = async (html: string, projectId: string): Promise => { + try { + // Create a temporary DOM to process the HTML + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + // Process script tags with relative paths + const scripts = doc.querySelectorAll('script[src]'); + for (const script of scripts) { + const src = script.getAttribute('src'); + if (src && !src.startsWith('http') && !src.startsWith('//')) { + try { + const assetPath = src.startsWith('/') ? `dist${src}` : `dist/${src}`; + const assetContent = await fsManager.readFile(projectId, assetPath); + + // Replace the script tag with inline script + const inlineScript = doc.createElement('script'); + inlineScript.textContent = assetContent; + script.parentNode?.replaceChild(inlineScript, script); + } catch (error) { + console.warn(`Failed to load script asset: ${src}`, error); + } + } + } + + // Process CSS link tags + const links = doc.querySelectorAll('link[rel="stylesheet"]'); + for (const link of links) { + const href = link.getAttribute('href'); + if (href && !href.startsWith('http') && !href.startsWith('//')) { + try { + const assetPath = href.startsWith('/') ? `dist${href}` : `dist/${href}`; + const cssContent = await fsManager.readFile(projectId, assetPath); + + // Replace the link tag with inline style + const style = doc.createElement('style'); + style.textContent = cssContent; + link.parentNode?.replaceChild(style, link); + } catch (error) { + console.warn(`Failed to load CSS asset: ${href}`, error); + } + } + } + + 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); }; @@ -54,19 +132,50 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
-
-
-

Project Preview

-

- Your project will be built and displayed here -

-
-

- Vite build integration coming soon... + {hasBuiltProject ? ( +

+
+ Project Preview + +
+