mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
Don't show preview tab for projects that are not buildable
This commit is contained in:
4 files changed
+196
-16
No files matched your search
@@ -31,6 +31,7 @@ interface PreviewPaneProps {
|
||||
onToggleView?: () => void;
|
||||
projectName?: string;
|
||||
onFirstInteraction?: () => void;
|
||||
isPreviewable?: boolean;
|
||||
}
|
||||
|
||||
interface JSONRPCRequest {
|
||||
@@ -69,7 +70,7 @@ interface ConsoleMessage {
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction }: PreviewPaneProps) {
|
||||
export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction, isPreviewable = true }: PreviewPaneProps) {
|
||||
const { t } = useTranslation();
|
||||
const [selectedFile, setSelectedFile] = useState<string | null>(null);
|
||||
const [fileContent, setFileContent] = useState<string>('');
|
||||
@@ -553,7 +554,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
return (
|
||||
<div className="h-full">
|
||||
<Tabs value={activeTab} className="h-full">
|
||||
<TabsContent value="preview" className="h-full mt-0">
|
||||
{isPreviewable && (
|
||||
<TabsContent value="preview" className="h-full mt-0">
|
||||
<div className="h-full w-full flex flex-col relative">
|
||||
{/* Always show browser address bar */}
|
||||
<div className="h-12 flex items-center w-full">
|
||||
@@ -567,7 +569,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
canGoForward={hasBuiltProject && historyIndex < navigationHistory.length - 1}
|
||||
extraContent={(
|
||||
<div className="flex items-center">
|
||||
{(!isMobile && onToggleView) && (
|
||||
{(!isMobile && onToggleView && isPreviewable) && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
@@ -635,6 +637,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
)}
|
||||
</div>
|
||||
</TabsContent>
|
||||
)}
|
||||
|
||||
<TabsContent value="code" className="h-full mt-0">
|
||||
{isMobile ? (
|
||||
@@ -700,7 +703,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
) : (
|
||||
<div className="h-full flex flex-col">
|
||||
{/* Code view header with back button */}
|
||||
{!isMobile && onToggleView && (
|
||||
{!isMobile && onToggleView && isPreviewable && (
|
||||
<div className="h-12 px-4 border-b flex items-center bg-gradient-to-r from-muted/20 to-background">
|
||||
<Button
|
||||
variant="ghost"
|
||||
@@ -715,6 +718,13 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
<GitStatusIndicator projectId={projectId} />
|
||||
</div>
|
||||
)}
|
||||
{/* Code view header without back button for non-previewable projects */}
|
||||
{!isMobile && !isPreviewable && (
|
||||
<div className="h-12 px-4 border-b flex items-center bg-gradient-to-r from-muted/20 to-background">
|
||||
<div className="flex-1" />
|
||||
<GitStatusIndicator projectId={projectId} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex-1 flex min-h-0">
|
||||
<div className="w-1/3 border-r flex flex-col">
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { useIsProjectPreviewable } from './useIsProjectPreviewable';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
|
||||
// Mock the useProjectsManager hook
|
||||
const mockFileExists = vi.fn();
|
||||
vi.mock('./useProjectsManager', () => ({
|
||||
useProjectsManager: () => ({
|
||||
fileExists: mockFileExists,
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('useIsProjectPreviewable', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('returns true when all required files exist', async () => {
|
||||
mockFileExists.mockResolvedValue(true);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(true);
|
||||
expect(mockFileExists).toHaveBeenCalledWith('test-project', 'index.html');
|
||||
expect(mockFileExists).toHaveBeenCalledWith('test-project', 'package.json');
|
||||
expect(mockFileExists).toHaveBeenCalledWith('test-project', 'package-lock.json');
|
||||
});
|
||||
|
||||
it('returns false when index.html is missing', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'index.html') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when package.json is missing', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'package.json') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when package-lock.json is missing', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'package-lock.json') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when projectId is empty', async () => {
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable(''),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
// When projectId is empty, the query is disabled, so it should not be loading or successful
|
||||
expect(result.current.data).toBe(false);
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(mockFileExists).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns false when file check throws an error', async () => {
|
||||
mockFileExists.mockRejectedValue(new Error('File system error'));
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useProjectsManager } from './useProjectsManager';
|
||||
|
||||
/**
|
||||
* Hook to check if a project is previewable
|
||||
* A project is previewable if it has index.html, package.json, and package-lock.json
|
||||
*/
|
||||
export function useIsProjectPreviewable(projectId: string) {
|
||||
const projectsManager = useProjectsManager();
|
||||
|
||||
const result = useQuery({
|
||||
queryKey: ['project-previewable', projectId],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
// Check for required files
|
||||
const [hasIndexHtml, hasPackageJson, hasPackageLock] = await Promise.all([
|
||||
projectsManager.fileExists(projectId, 'index.html'),
|
||||
projectsManager.fileExists(projectId, 'package.json'),
|
||||
projectsManager.fileExists(projectId, 'package-lock.json'),
|
||||
]);
|
||||
|
||||
return hasIndexHtml && hasPackageJson && hasPackageLock;
|
||||
} catch (error) {
|
||||
console.error('Failed to check if project is previewable:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
enabled: !!projectId,
|
||||
staleTime: 5000, // Cache for 5 seconds
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
// Return false when query is disabled (empty projectId) or when data is undefined
|
||||
return {
|
||||
...result,
|
||||
data: result.data ?? false,
|
||||
};
|
||||
}
|
||||
+27
-12
@@ -19,6 +19,7 @@ import { GitStatusIndicator } from '@/components/GitStatusIndicator';
|
||||
import { StarButton } from '@/components/StarButton';
|
||||
|
||||
import { useBuildProject } from '@/hooks/useBuildProject';
|
||||
import { useIsProjectPreviewable } from '@/hooks/useIsProjectPreviewable';
|
||||
|
||||
export function ProjectView() {
|
||||
const { projectId } = useParams<{ projectId: string }>();
|
||||
@@ -37,6 +38,7 @@ export function ProjectView() {
|
||||
const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile);
|
||||
|
||||
const build = useBuildProject(projectId!);
|
||||
const { data: isPreviewable = false } = useIsProjectPreviewable(projectId!);
|
||||
|
||||
// Keep-alive functionality to prevent tab throttling during AI processing
|
||||
const { updateMetadata } = useKeepAlive({
|
||||
@@ -69,6 +71,16 @@ export function ProjectView() {
|
||||
loadProject();
|
||||
}, [loadProject]);
|
||||
|
||||
// Switch away from preview mode if project is not previewable
|
||||
useEffect(() => {
|
||||
if (mobileView === 'preview' && !isPreviewable) {
|
||||
setMobileView('code');
|
||||
}
|
||||
if (activeTab === 'preview' && !isPreviewable) {
|
||||
setActiveTab('code');
|
||||
}
|
||||
}, [isPreviewable, mobileView, activeTab]);
|
||||
|
||||
const runBuild = async () => {
|
||||
if (build.isPending || !project) return;
|
||||
updateMetadata('Shakespeare', `Building ${project.name}...`);
|
||||
@@ -255,16 +267,18 @@ export function ProjectView() {
|
||||
<MessageSquare className="h-4 w-4 mr-1" />
|
||||
{t('chat')}
|
||||
</Button>
|
||||
<Button
|
||||
variant={mobileView === 'preview' ? 'default' : 'ghost'}
|
||||
size="sm"
|
||||
onClick={() => setMobileView('preview')}
|
||||
className="flex-1 rounded-none"
|
||||
disabled={!project}
|
||||
>
|
||||
<Eye className="h-4 w-4 mr-1" />
|
||||
{t('preview')}
|
||||
</Button>
|
||||
{isPreviewable && (
|
||||
<Button
|
||||
variant={mobileView === 'preview' ? 'default' : 'ghost'}
|
||||
size="sm"
|
||||
onClick={() => setMobileView('preview')}
|
||||
className="flex-1 rounded-none"
|
||||
disabled={!project}
|
||||
>
|
||||
<Eye className="h-4 w-4 mr-1" />
|
||||
{t('preview')}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant={mobileView === 'code' ? 'default' : 'ghost'}
|
||||
size="sm"
|
||||
@@ -415,10 +429,11 @@ export function ProjectView() {
|
||||
{project ? (
|
||||
<PreviewPane
|
||||
projectId={project.id}
|
||||
activeTab={activeTab}
|
||||
onToggleView={() => setActiveTab(activeTab === 'preview' ? 'code' : 'preview')}
|
||||
activeTab={isPreviewable ? activeTab : 'code'}
|
||||
onToggleView={isPreviewable ? () => setActiveTab(activeTab === 'preview' ? 'code' : 'preview') : undefined}
|
||||
projectName={project.name}
|
||||
onFirstInteraction={handleFirstInteraction}
|
||||
isPreviewable={isPreviewable}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
|
||||
Reference in new issue
Block a user