From 8a12731a2cdb1f2ed89876b413f1dc1fbe99591a Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sat, 20 Jun 2026 23:18:41 -0400 Subject: [PATCH] feat: enhance HTML artifact preview with mobile support and test cleanup --- .../components/file-browser/FileBrowser.tsx | 61 ++++++++++++++++- .../file-browser/FileBrowserSheet.test.tsx | 65 ++++++++++++++++++- .../file-browser/MobileFilePreviewModal.tsx | 19 +++++- .../html-preview/HtmlArtifactPanel.test.tsx | 18 +---- .../html-preview/HtmlArtifactPanel.tsx | 56 +++++++--------- .../html-preview/HtmlPreviewFrame.tsx | 7 +- frontend/src/lib/htmlArtifacts.test.ts | 26 ++++++++ frontend/src/lib/htmlArtifacts.ts | 24 +++++++ 8 files changed, 219 insertions(+), 57 deletions(-) diff --git a/frontend/src/components/file-browser/FileBrowser.tsx b/frontend/src/components/file-browser/FileBrowser.tsx index c6df1a35..b88c38f1 100644 --- a/frontend/src/components/file-browser/FileBrowser.tsx +++ b/frontend/src/components/file-browser/FileBrowser.tsx @@ -12,6 +12,9 @@ import { FolderOpen, Upload, RefreshCw, X } from 'lucide-react' import type { FileInfo } from '@/types/files' import { useMobile } from '@/hooks/useMobile' import { getFileApiUrl, useFile } from '@/api/files' +import { HtmlArtifactPanel } from '@/components/html-preview/HtmlArtifactPanel' +import type { HtmlArtifact } from '@/lib/htmlArtifacts' +import { createHtmlArtifact, isHtmlPath } from '@/lib/htmlArtifacts' export interface FileBrowserHandle { goBack: () => void @@ -51,6 +54,10 @@ const encodeBase64 = (content: string) => { return btoa(binary) } +const isHtmlFileInfo = (file: FileInfo): boolean => ( + isHtmlPath(file.path) || isHtmlPath(file.name) || file.mimeType === 'text/html' +) + async function readFileEntry(entry: FileSystemFileEntry): Promise { return new Promise((resolve, reject) => { entry.file(resolve, reject) @@ -135,6 +142,7 @@ export const FileBrowser = forwardRef(funct const [currentPath, setCurrentPath] = useState(basePath) const [files, setFiles] = useState(null) const [selectedFile, setSelectedFile] = useState(null) + const [htmlArtifact, setHtmlArtifact] = useState(null) const [searchQuery, setSearchQuery] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) @@ -152,7 +160,17 @@ useEffect(() => { if (initialFileData) { setSelectedFile(initialFileData) if (isMobile) { - setIsPreviewModalOpen(true) + if (isHtmlFileInfo(initialFileData)) { + setHtmlArtifact(createHtmlArtifact({ + source: 'file', + path: initialFileData.path, + title: initialFileData.name, + })) + setIsPreviewModalOpen(false) + } else { + setHtmlArtifact(null) + setIsPreviewModalOpen(true) + } onPreviewStateChange?.(true) } } @@ -262,7 +280,17 @@ useEffect(() => { // On mobile, open preview in modal if (isMobile) { - setIsPreviewModalOpen(true) + if (isHtmlFileInfo(fullFileData)) { + setHtmlArtifact(createHtmlArtifact({ + source: 'file', + path: fullFileData.path, + title: fullFileData.name, + })) + setIsPreviewModalOpen(false) + } else { + setHtmlArtifact(null) + setIsPreviewModalOpen(true) + } onPreviewStateChange?.(true) } } catch (err) { @@ -275,10 +303,19 @@ useEffect(() => { const handleCloseModal = useCallback(() => { setIsPreviewModalOpen(false) + setHtmlArtifact(null) setSelectedFile(null) onPreviewStateChange?.(false) }, [onPreviewStateChange]) + const handleCloseHtmlArtifact = useCallback(() => { + setHtmlArtifact(null) + setSelectedFile(null) + onPreviewStateChange?.(false) + }, [onPreviewStateChange]) + + const handleToggleHtmlArtifactFullscreen = useCallback(() => {}, []) + const handleDirectoryClick = (path: string) => { loadFiles(path) } @@ -645,6 +682,16 @@ useEffect(() => { file={selectedFile} showFilePreviewHeader={true} /> + + {isMobile && htmlArtifact && ( + + )} ) } @@ -746,6 +793,16 @@ useEffect(() => { onClose={handleCloseModal} file={selectedFile} /> + + {isMobile && htmlArtifact && ( + + )} {uploadDialog} diff --git a/frontend/src/components/file-browser/FileBrowserSheet.test.tsx b/frontend/src/components/file-browser/FileBrowserSheet.test.tsx index af322b0a..421c97a6 100644 --- a/frontend/src/components/file-browser/FileBrowserSheet.test.tsx +++ b/frontend/src/components/file-browser/FileBrowserSheet.test.tsx @@ -7,6 +7,15 @@ import { FileBrowser } from './FileBrowser' import type { FileBrowserHandle } from './FileBrowser' import * as useMobile from '../../hooks/useMobile' +vi.mock('@/components/html-preview/HtmlArtifactPanel', () => ({ + HtmlArtifactPanel: ({ artifact, onClose }: { artifact: { path?: string } | null; onClose: () => void }) => ( +
+ {artifact?.path} + +
+ ), +})) + function createQueryClient() { return new QueryClient({ defaultOptions: { @@ -87,6 +96,60 @@ describe('FileBrowserSheet', () => { }) describe('FileBrowser navigation', () => { + it('opens mobile HTML files with the shared HTML artifact panel', async () => { + const mobileSpy = vi.spyOn(useMobile, 'useMobile').mockReturnValue(true) + const originalFetch = globalThis.fetch + const htmlFile = { + name: 'dashboard.html', + path: 'test-repo/dashboard.html', + isDirectory: false, + size: 24, + mimeType: 'text/html', + content: btoa('

Dashboard

'), + lastModified: new Date(), + } + + globalThis.fetch = vi.fn(async (input: RequestInfo | URL) => { + const url = new URL(String(input), 'http://localhost') + const path = url.searchParams.get('path') + + if (path === htmlFile.path) { + return { + ok: true, + json: async () => htmlFile, + } as Response + } + + return { + ok: true, + json: async () => ({ + name: 'test-repo', + path: 'test-repo', + isDirectory: true, + children: [htmlFile], + }), + } as Response + }) + + try { + render( + , + { wrapper: createWrapper() } + ) + + fireEvent.click(await screen.findByText('dashboard.html')) + + expect(await screen.findByTestId('html-artifact-panel')).toHaveTextContent('test-repo/dashboard.html') + expect(screen.queryByTitle('HTML preview: dashboard.html')).not.toBeInTheDocument() + } finally { + globalThis.fetch = originalFetch + mobileSpy.mockRestore() + } + }) + it('exposes imperative handle with goBack, canGoBack, and getCurrentPath', () => { const ref = { current: null as unknown as FileBrowserHandle } @@ -1091,5 +1154,3 @@ describe('FileBrowserSheet swipe decision integration', () => { mockUseSwipeBack.mockRestore() }) }) - - diff --git a/frontend/src/components/file-browser/MobileFilePreviewModal.tsx b/frontend/src/components/file-browser/MobileFilePreviewModal.tsx index 9e70cd7f..7319823d 100644 --- a/frontend/src/components/file-browser/MobileFilePreviewModal.tsx +++ b/frontend/src/components/file-browser/MobileFilePreviewModal.tsx @@ -1,9 +1,11 @@ import { memo, useCallback, useState, useEffect, useRef } from "react"; import { FilePreview } from "./FilePreview"; import { FullscreenSheet } from "@/components/ui/fullscreen-sheet"; +import { Button } from "@/components/ui/button"; import type { FileInfo } from "@/types/files"; import { GPU_ACCELERATED_STYLE, MODAL_TRANSITION_MS } from "@/lib/utils"; import { useSwipeBack } from "@/hooks/useMobile"; +import { X } from "lucide-react"; interface MobileFilePreviewModalProps { isOpen: boolean; @@ -69,13 +71,24 @@ export const MobileFilePreviewModal = memo(function MobileFilePreviewModal({ return (
- -
+ +
+ {!showFilePreviewHeader && ( + + )}
diff --git a/frontend/src/components/html-preview/HtmlArtifactPanel.test.tsx b/frontend/src/components/html-preview/HtmlArtifactPanel.test.tsx index 0f05ce9e..95f60033 100644 --- a/frontend/src/components/html-preview/HtmlArtifactPanel.test.tsx +++ b/frontend/src/components/html-preview/HtmlArtifactPanel.test.tsx @@ -42,7 +42,8 @@ describe('HtmlArtifactPanel', () => { const iframe = screen.getByTitle('Test Preview') expect(iframe).toBeInTheDocument() - expect(iframe).toHaveAttribute('srcdoc', '

Hello World

') + expect(iframe.getAttribute('srcdoc')).toContain('') + expect(iframe.getAttribute('srcdoc')).toContain('

Hello World

') expect(iframe).toHaveAttribute('sandbox', 'allow-scripts') }) @@ -148,21 +149,6 @@ describe('HtmlArtifactPanel', () => { expect(sheetRoot).toBeInTheDocument() }) - it('shows source label (File artifact / Inline artifact)', () => { - const inlineArtifact = createInlineArtifact() - render( - {}} - onToggleFullscreen={() => {}} - />, - ) - - expect(screen.getByText('(Inline artifact)')).toBeInTheDocument() - }) - it('returns null when artifact is null', () => { const { container } = render( -
- {title} - ({sourceLabel}) -
-
- {!isMobile && ( - - )} + const headerButtons = ( +
+ {!isMobile && ( -
+ )} +
) @@ -72,11 +64,11 @@ export function HtmlArtifactPanel({ if (isMobile || isFullscreen) { return ( - - - {header} - - + + +
+ {headerButtons} +
{frame}
@@ -84,13 +76,13 @@ export function HtmlArtifactPanel({ } return ( -
-
- {header} -
+
{frame}
+
+ {headerButtons} +
) } diff --git a/frontend/src/components/html-preview/HtmlPreviewFrame.tsx b/frontend/src/components/html-preview/HtmlPreviewFrame.tsx index 5fdff014..9ce7fd2c 100644 --- a/frontend/src/components/html-preview/HtmlPreviewFrame.tsx +++ b/frontend/src/components/html-preview/HtmlPreviewFrame.tsx @@ -1,4 +1,5 @@ import { cn } from '@/lib/utils' +import { normalizeHtmlPreviewDocument } from '@/lib/htmlArtifacts' interface HtmlPreviewFrameProps { title: string @@ -9,14 +10,16 @@ interface HtmlPreviewFrameProps { export function HtmlPreviewFrame({ title, src, srcDoc, className }: HtmlPreviewFrameProps) { const sandbox = src ? 'allow-scripts allow-same-origin' : 'allow-scripts' + const normalizedSrcDoc = srcDoc ? normalizeHtmlPreviewDocument(srcDoc) : undefined return (