code tabs with overflow

This commit is contained in:
lemonknowsall committed 2026-01-23 00:56:48 -08:00
1 parent 92ef8fb3f3
commit d03c092ec9
2 files changed
+177 -140

No files matched your search

+11 -113
View File
@@ -1,140 +1,38 @@
import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea';
import { CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Save, Loader2, ImageOff } from 'lucide-react';
import { ImageOff } from 'lucide-react';
import { cn } from '@/lib/utils';
import { useGitStatus } from '@/hooks/useGitStatus';
import { isMediaFile } from '@/lib/fileUtils';
interface FileEditorProps {
filePath: string;
content: string;
onSave: (content: string) => void;
onContentChange: (content: string) => void;
isLoading: boolean;
projectId?: string;
}
export function FileEditor({ filePath, content, onSave, isLoading, projectId }: FileEditorProps) {
const { t } = useTranslation();
export function FileEditor({ filePath, content, onContentChange, isLoading, projectId }: FileEditorProps) {
const [editedContent, setEditedContent] = useState(content);
const [isSaving, setIsSaving] = useState(false);
const [hasChanges, setHasChanges] = useState(false);
const { data: gitStatus } = useGitStatus(projectId || null);
const isMedia = isMediaFile(filePath);
useEffect(() => {
setEditedContent(content);
setHasChanges(false);
}, [content, filePath]);
useEffect(() => {
setHasChanges(editedContent !== content);
}, [editedContent, content]);
const handleSave = async () => {
setIsSaving(true);
try {
await onSave(editedContent);
setHasChanges(false);
} catch (error) {
console.error('Failed to save file:', error);
} finally {
setIsSaving(false);
}
};
const getLanguageFromPath = (path: string): string => {
const extension = path.split('.').pop()?.toLowerCase();
const languageMap: Record<string, string> = {
js: 'javascript',
jsx: 'javascript',
ts: 'typescript',
tsx: 'typescript',
html: 'html',
css: 'css',
json: 'json',
md: 'markdown',
py: 'python',
java: 'java',
cpp: 'cpp',
c: 'c',
sh: 'bash',
yml: 'yaml',
yaml: 'yaml',
};
return languageMap[extension || ''] || 'text';
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newContent = e.target.value;
setEditedContent(newContent);
onContentChange(newContent);
};
const getMonospaceFont = () => {
return 'font-mono text-sm';
};
// Helper function to get git status for the current file
const getFileGitStatus = (filePath: string) => {
if (!gitStatus?.changedFiles) return null;
const fileChange = gitStatus.changedFiles.find(change => change.filepath === filePath);
return fileChange?.status || null;
};
// Helper function to get styling classes based on git status
const getGitStatusClasses = (status: string | null) => {
switch (status) {
case 'added':
case 'untracked':
return 'text-green-600 dark:text-green-400';
case 'modified':
case 'staged':
return 'text-yellow-600 dark:text-yellow-400';
default:
return '';
}
};
// Get git status for the current file
const currentFileGitStatus = getFileGitStatus(filePath);
const gitStatusClasses = getGitStatusClasses(currentFileGitStatus);
const isMedia = isMediaFile(filePath);
return (
<div className="h-full flex flex-col">
<CardHeader className="border-b py-2 sm:py-3">
<div className="flex items-center justify-between">
<CardTitle className={cn("text-sm sm:text-base truncate flex-1 mr-2", gitStatusClasses)}>{filePath}</CardTitle>
<div className="flex items-center space-x-1 sm:space-x-2">
{hasChanges && !isMedia && (
<span className="text-xs sm:text-sm text-muted-foreground hidden sm:inline">{t('unsavedChanges')}</span>
)}
{!isMedia && (
<Button
onClick={handleSave}
disabled={!hasChanges || isSaving || isLoading}
size="sm"
className="focus-ring"
>
{isSaving ? (
<>
<Loader2 className="mr-1 sm:mr-2 h-4 w-4 animate-spin" />
<span className="hidden sm:inline">{t('saving')}</span>
</>
) : (
<>
<Save className="sm:mr-2 h-4 w-4" />
<span className="hidden sm:inline">{t('save')}</span>
</>
)}
</Button>
)}
</div>
</div>
{!isMedia && (
<div className="text-xs text-muted-foreground mt-1">
{t('languageLabel')}: {getLanguageFromPath(filePath)}
</div>
)}
</CardHeader>
<CardContent className="flex-1 p-0">
<div className="flex-1 p-0">
{isLoading ? (
<div className="flex items-center justify-center h-full">
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary"></div>
@@ -146,7 +44,7 @@ export function FileEditor({ filePath, content, onSave, isLoading, projectId }:
) : (
<Textarea
value={editedContent}
onChange={(e) => setEditedContent(e.target.value)}
onChange={handleChange}
className={cn(
'w-full h-full resize-none border-0 rounded-none',
'focus:outline-none focus:ring-0',
@@ -157,7 +55,7 @@ export function FileEditor({ filePath, content, onSave, isLoading, projectId }:
spellCheck={false}
/>
)}
</CardContent>
</div>
</div>
);
}
+166 -27
View File
@@ -10,7 +10,7 @@ import { useBuildProject } from '@/hooks/useBuildProject';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
import { Button } from '@/components/ui/button';
import { FolderOpen, ArrowLeft, Bug, Copy, Check, Loader2, Code, X, Terminal, Expand, Shrink, Hammer, RefreshCw } from 'lucide-react';
import { FolderOpen, ArrowLeft, Bug, Copy, Check, Loader2, Code, X, Terminal, Expand, Shrink, Hammer, RefreshCw, Save } from 'lucide-react';
import { useIsMobile } from '@/hooks/useIsMobile';
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
import { BranchSwitcher } from '@/components/BranchSwitcher';
@@ -68,7 +68,11 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
const { config } = useAppContext();
const { previewDomain } = config;
const [selectedFile, setSelectedFile] = useState<string | null>(null);
const [fileContent, setFileContent] = useState<string>('');
const [openFiles, setOpenFiles] = useState<string[]>([]);
const [activeFileTab, setActiveFileTab] = useState<string | null>(null);
const [fileContents, setFileContents] = useState<Record<string, string>>({});
const [editedContents, setEditedContents] = useState<Record<string, string>>({});
const [isSaving, setIsSaving] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const isMobile = useIsMobile();
const [hasBuiltProject, setHasBuiltProject] = useState(false);
@@ -124,22 +128,26 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
const loadFileContent = useCallback(async (filePath: string) => {
// Skip loading media files - they can't be edited as text
if (isMediaFile(filePath)) {
setFileContent('');
setIsLoading(false);
setFileContents(prev => ({ ...prev, [filePath]: '' }));
return;
}
// Don't reload if we already have the content
if (fileContents[filePath]) {
return;
}
setIsLoading(true);
try {
const content = await projectsManager.readFile(projectId, filePath);
setFileContent(content);
setFileContents(prev => ({ ...prev, [filePath]: content }));
} catch (_error) {
console.error('Failed to load file:', _error);
setFileContent('');
setFileContents(prev => ({ ...prev, [filePath]: '' }));
} finally {
setIsLoading(false);
}
}, [projectId, projectsManager]);
}, [projectId, projectsManager, fileContents]);
const getContentType = (filename: string): string => {
const ext = filename.split('.').pop()?.toLowerCase();
@@ -415,17 +423,23 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
return () => window.removeEventListener('message', handleMessage);
}, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, projectId, previewDomain]);
// Load content for all open files
useEffect(() => {
if (selectedFile) {
loadFileContent(selectedFile);
}
}, [selectedFile, loadFileContent]);
openFiles.forEach(filePath => {
if (!fileContents[filePath] && !isMediaFile(filePath)) {
loadFileContent(filePath);
}
});
}, [openFiles, fileContents, loadFileContent]);
// Reset selected file and navigation history when projectId changes
const prevProjectIdRef = useRef<string>();
useEffect(() => {
setSelectedFile(null);
setFileContent('');
setOpenFiles([]);
setActiveFileTab(null);
setFileContents({});
setEditedContents({});
setBuildError(null);
// Reset navigation history and state
@@ -460,15 +474,64 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
}, [projectId, checkForBuiltProject, refreshIframe]);
const handleFileSelect = (filePath: string) => {
// Skip media files
if (isMediaFile(filePath)) {
return;
}
// Add to open files if not already open
if (!openFiles.includes(filePath)) {
setOpenFiles(prev => [...prev, filePath]);
}
// Set as active tab
setActiveFileTab(filePath);
setSelectedFile(filePath);
};
const handleFileSave = async (content: string) => {
if (!selectedFile) return;
const handleCloseTab = (filePath: string, e: React.MouseEvent) => {
e.stopPropagation();
// Remove from open files
setOpenFiles(prev => prev.filter(f => f !== filePath));
// Remove from file contents
setFileContents(prev => {
const newContents = { ...prev };
delete newContents[filePath];
return newContents;
});
// If this was the active tab, switch to another tab or clear
if (activeFileTab === filePath) {
const remainingFiles = openFiles.filter(f => f !== filePath);
if (remainingFiles.length > 0) {
setActiveFileTab(remainingFiles[remainingFiles.length - 1]);
setSelectedFile(remainingFiles[remainingFiles.length - 1]);
} else {
setActiveFileTab(null);
setSelectedFile(null);
}
}
};
const handleFileSave = async () => {
if (!activeFileTab) return;
const contentToSave = editedContents[activeFileTab];
if (contentToSave === undefined || contentToSave === fileContents[activeFileTab]) {
return; // No changes to save
}
setIsSaving(true);
try {
await fs.writeFile(`${projectsPath}/${projectId}/${selectedFile}`, content);
setFileContent(content);
await fs.writeFile(`${projectsPath}/${projectId}/${activeFileTab}`, contentToSave);
setFileContents(prev => ({ ...prev, [activeFileTab]: contentToSave }));
setEditedContents(prev => {
const newContents = { ...prev };
delete newContents[activeFileTab];
return newContents;
});
// Automatically trigger a rebuild after saving a file
if (isPreviewable) {
@@ -481,6 +544,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
}
} catch (error) {
console.error('Failed to save file:', error);
} finally {
setIsSaving(false);
}
};
@@ -775,7 +840,34 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
</Button>
)}
{logsPanelTab === 'code' && (
<BranchSwitcher projectId={projectId} />
<>
<BranchSwitcher projectId={projectId} />
{activeFileTab && (() => {
const hasUnsavedChanges = editedContents[activeFileTab] !== undefined &&
editedContents[activeFileTab] !== fileContents[activeFileTab];
return (
<Button
variant="ghost"
size="sm"
onClick={handleFileSave}
disabled={!hasUnsavedChanges || isSaving || isLoading}
className="h-7 gap-1.5 text-xs"
>
{isSaving ? (
<>
<Loader2 className="h-3 w-3 animate-spin" />
<span className="hidden sm:inline">{t('saving')}</span>
</>
) : (
<>
<Save className="h-3 w-3" />
<span className="hidden sm:inline">{t('save')}</span>
</>
)}
</Button>
);
})()}
</>
)}
<Button
variant="ghost"
@@ -852,20 +944,67 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
<FileTree
projectId={projectId}
onFileSelect={handleFileSelect}
selectedFile={selectedFile}
selectedFile={activeFileTab}
/>
</div>
</ScrollArea>
</div>
<div className="flex-1 min-h-0">
{selectedFile ? (
<FileEditor
filePath={selectedFile}
content={fileContent}
onSave={handleFileSave}
isLoading={isLoading}
projectId={projectId}
/>
<div className="flex-1 min-h-0 flex flex-col overflow-hidden">
{openFiles.length > 0 ? (
<>
{/* File Tabs */}
<div className="border-b flex-shrink-0 w-full overflow-x-auto overflow-y-hidden">
<div className="flex items-center gap-1 px-2" style={{ width: 'max-content' }}>
{openFiles.map((filePath) => {
const fileName = filePath.split('/').pop() || filePath;
const hasUnsavedChanges = editedContents[filePath] !== undefined &&
editedContents[filePath] !== fileContents[filePath];
const isActive = activeFileTab === filePath;
return (
<button
key={filePath}
onClick={() => {
setActiveFileTab(filePath);
setSelectedFile(filePath);
}}
className={cn(
"flex items-center gap-2 px-3 py-1.5 text-sm border-b-2 transition-colors flex-shrink-0 whitespace-nowrap",
isActive
? "border-primary text-foreground"
: "border-transparent text-muted-foreground hover:text-foreground"
)}
>
<span>{fileName}</span>
{hasUnsavedChanges && (
<span className="h-1.5 w-1.5 rounded-full bg-primary flex-shrink-0" />
)}
<button
onClick={(e) => handleCloseTab(filePath, e)}
className="ml-1 hover:bg-muted rounded p-0.5 flex-shrink-0"
>
<X className="h-3 w-3" />
</button>
</button>
);
})}
</div>
</div>
{/* Editor */}
{activeFileTab && (
<FileEditor
filePath={activeFileTab}
content={editedContents[activeFileTab] !== undefined
? editedContents[activeFileTab]
: (fileContents[activeFileTab] ?? '')}
onContentChange={(content) => {
setEditedContents(prev => ({ ...prev, [activeFileTab]: content }));
}}
isLoading={isLoading && !fileContents[activeFileTab]}
projectId={projectId}
/>
)}
</>
) : (
<div className="h-full flex items-center justify-center">
<div className="text-center">