Use a browser-like UI in the project preview

This commit is contained in:
Alex Gleason committed 2025-09-13 19:01:21 -05:00
1 parent ebc4c2d997
commit ef6642181e
3 files changed
+216 -12

No files matched your search

+59 -12
View File
@@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button';
import { RefreshCw, FolderOpen, ArrowLeft } from 'lucide-react';
import { useIsMobile } from '@/hooks/useIsMobile';
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
import { BrowserAddressBar } from '@/components/ui/browser-address-bar';
import { FileTree } from './FileTree';
import { FileEditor } from './FileEditor';
@@ -53,6 +54,9 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
const [mobileCodeView, setMobileCodeView] = useState<'explorer' | 'editor'>('explorer');
const isMobile = useIsMobile();
const [hasBuiltProject, setHasBuiltProject] = useState(false);
const [currentPath, setCurrentPath] = useState('/');
const [navigationHistory, setNavigationHistory] = useState<string[]>(['/']);
const [historyIndex, setHistoryIndex] = useState(0);
const iframeRef = useRef<HTMLIFrameElement>(null);
const { fs } = useFS();
const projectsManager = useProjectsManager();
@@ -120,6 +124,51 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
}
}, []);
const navigateToPath = useCallback((path: string) => {
if (iframeRef.current) {
const baseUrl = `https://${projectId}.local-shakespeare.dev`;
const newUrl = `${baseUrl}${path}`;
iframeRef.current.src = newUrl;
setCurrentPath(path);
// Update navigation history
const newHistory = navigationHistory.slice(0, historyIndex + 1);
newHistory.push(path);
setNavigationHistory(newHistory);
setHistoryIndex(newHistory.length - 1);
}
}, [projectId, navigationHistory, historyIndex]);
const goBack = useCallback(() => {
if (historyIndex > 0) {
const newIndex = historyIndex - 1;
const path = navigationHistory[newIndex];
setHistoryIndex(newIndex);
setCurrentPath(path);
if (iframeRef.current) {
const baseUrl = `https://${projectId}.local-shakespeare.dev`;
const newUrl = `${baseUrl}${path}`;
iframeRef.current.src = newUrl;
}
}
}, [historyIndex, navigationHistory, projectId]);
const goForward = useCallback(() => {
if (historyIndex < navigationHistory.length - 1) {
const newIndex = historyIndex + 1;
const path = navigationHistory[newIndex];
setHistoryIndex(newIndex);
setCurrentPath(path);
if (iframeRef.current) {
const baseUrl = `https://${projectId}.local-shakespeare.dev`;
const newUrl = `${baseUrl}${path}`;
iframeRef.current.src = newUrl;
}
}
}, [historyIndex, navigationHistory, projectId]);
const sendResponse = useCallback((message: JSONRPCResponse) => {
if (iframeRef.current?.contentWindow) {
console.log(`Sending response to iframe:`, message);
@@ -303,20 +352,18 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
<TabsContent value="preview" className="h-full mt-0">
{hasBuiltProject ? (
<div className="h-full w-full flex flex-col">
<div className="flex items-center justify-between p-2 border-b bg-gradient-to-r from-primary/5 to-accent/5">
<span className="text-sm font-medium bg-gradient-to-r from-primary to-accent bg-clip-text text-transparent">Project Preview</span>
<Button
variant="ghost"
size="sm"
onClick={refreshIframe}
className="h-8 w-8 p-0 hover:bg-primary/10"
>
<RefreshCw className="h-4 w-4" />
</Button>
</div>
<BrowserAddressBar
currentPath={currentPath}
onNavigate={navigateToPath}
onRefresh={refreshIframe}
onBack={goBack}
onForward={goForward}
canGoBack={historyIndex > 0}
canGoForward={historyIndex < navigationHistory.length - 1}
/>
<iframe
ref={iframeRef}
src={`https://${projectId}.local-shakespeare.dev/`}
src={`https://${projectId}.local-shakespeare.dev${currentPath}`}
className="w-full flex-1 border-0"
title="Project Preview"
sandbox="allow-scripts allow-same-origin"
@@ -0,0 +1,61 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { BrowserAddressBar } from './browser-address-bar';
describe('BrowserAddressBar', () => {
it('renders navigation buttons and address input', () => {
render(<BrowserAddressBar />);
expect(screen.getByTitle('Go back')).toBeInTheDocument();
expect(screen.getByTitle('Go forward')).toBeInTheDocument();
expect(screen.getByTitle('Refresh')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Enter path (e.g., /, /about)')).toBeInTheDocument();
});
it('calls onNavigate with proper path when form is submitted', () => {
const onNavigate = vi.fn();
render(<BrowserAddressBar onNavigate={onNavigate} />);
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
fireEvent.change(input, { target: { value: 'about' } });
fireEvent.submit(input.closest('form')!);
expect(onNavigate).toHaveBeenCalledWith('/about');
});
it('disables back/forward buttons based on canGoBack/canGoForward props', () => {
render(
<BrowserAddressBar
canGoBack={false}
canGoForward={true}
/>
);
expect(screen.getByTitle('Go back')).toBeDisabled();
expect(screen.getByTitle('Go forward')).not.toBeDisabled();
});
it('calls navigation handlers when buttons are clicked', () => {
const onBack = vi.fn();
const onForward = vi.fn();
const onRefresh = vi.fn();
render(
<BrowserAddressBar
onBack={onBack}
onForward={onForward}
onRefresh={onRefresh}
canGoBack={true}
canGoForward={true}
/>
);
fireEvent.click(screen.getByTitle('Go back'));
fireEvent.click(screen.getByTitle('Go forward'));
fireEvent.click(screen.getByTitle('Refresh'));
expect(onBack).toHaveBeenCalled();
expect(onForward).toHaveBeenCalled();
expect(onRefresh).toHaveBeenCalled();
});
});
+96
View File
@@ -0,0 +1,96 @@
import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { ChevronLeft, ChevronRight, RefreshCw } from 'lucide-react';
import { cn } from '@/lib/utils';
interface BrowserAddressBarProps {
currentPath?: string;
onNavigate?: (path: string) => void;
onRefresh?: () => void;
onBack?: () => void;
onForward?: () => void;
canGoBack?: boolean;
canGoForward?: boolean;
className?: string;
}
export function BrowserAddressBar({
currentPath = '/',
onNavigate,
onRefresh,
onBack,
onForward,
canGoBack = false,
canGoForward = false,
className,
}: BrowserAddressBarProps) {
const [inputValue, setInputValue] = useState(currentPath);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
let path = inputValue.trim();
// Ensure path starts with /
if (!path.startsWith('/')) {
path = '/' + path;
}
onNavigate?.(path);
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
return (
<div className={cn(
"flex items-center gap-1 p-2 border-b bg-background",
className
)}>
{/* Navigation buttons */}
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
onClick={onBack}
disabled={!canGoBack}
className="h-8 w-8 p-0"
title="Go back"
>
<ChevronLeft className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={onForward}
disabled={!canGoForward}
className="h-8 w-8 p-0"
title="Go forward"
>
<ChevronRight className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={onRefresh}
className="h-8 w-8 p-0"
title="Refresh"
>
<RefreshCw className="h-4 w-4" />
</Button>
</div>
{/* Address bar */}
<form onSubmit={handleSubmit} className="flex-1 mx-2">
<Input
type="text"
value={inputValue}
onChange={handleInputChange}
placeholder="Enter path (e.g., /, /about)"
className="h-8 bg-muted/50 border-muted-foreground/20 focus:bg-background"
/>
</form>
</div>
);
}