Add dynamic path display to file browser with truncation support

This commit is contained in:
Chris Scott committed 2025-11-27 08:28:30 -05:00
1 parent 0293bdbfa8
commit 16767a2a77
7 files changed
+76 -28

No files matched your search

+2
View File
@@ -24,6 +24,7 @@ interface FileInfo {
content?: string
children?: FileInfo[]
lastModified: Date
workspaceRoot?: string
}
interface FileUploadResult {
@@ -96,6 +97,7 @@ export async function getFile(userPath: string): Promise<FileInfo> {
return a.name.localeCompare(b.name)
}),
lastModified: stats.lastModified,
workspaceRoot: SHARED_WORKSPACE_BASE,
}
} else {
// It's a file - get content
@@ -20,9 +20,10 @@ interface FileBrowserProps {
onFileSelect?: (file: FileInfo) => void
embedded?: boolean
initialSelectedFile?: string
onDirectoryLoad?: (info: { workspaceRoot?: string; currentPath: string }) => void
}
export function FileBrowser({ basePath = '', onFileSelect, embedded = false, initialSelectedFile }: FileBrowserProps) {
export function FileBrowser({ basePath = '', onFileSelect, embedded = false, initialSelectedFile, onDirectoryLoad }: FileBrowserProps) {
const [currentPath, setCurrentPath] = useState(basePath)
const [files, setFiles] = useState<FileInfo | null>(null)
const [selectedFile, setSelectedFile] = useState<FileInfo | null>(null)
@@ -65,6 +66,7 @@ useEffect(() => {
const data = await response.json()
setFiles(data)
setCurrentPath(path)
onDirectoryLoad?.({ workspaceRoot: data.workspaceRoot, currentPath: path })
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load files')
} finally {
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react'
import { FileBrowser } from './FileBrowser'
import { Button } from '@/components/ui/button'
import { PathDisplay } from '@/components/ui/path-display'
import { X } from 'lucide-react'
interface FileBrowserSheetProps {
@@ -12,7 +13,29 @@ interface FileBrowserSheetProps {
}
export function FileBrowserSheet({ isOpen, onClose, basePath = '', repoName, initialSelectedFile }: FileBrowserSheetProps) {
const normalizedBasePath = basePath || '.'
const [isEditing, setIsEditing] = useState(false)
const [displayPath, setDisplayPath] = useState<string>('/')
const handleDirectoryLoad = (info: { workspaceRoot?: string; currentPath: string }) => {
if (!info.currentPath || info.currentPath === '.' || info.currentPath === '') {
setDisplayPath('/')
return
}
const pathParts = info.currentPath.split('/').filter(Boolean)
if (repoName) {
const repoIndex = pathParts.findIndex(p => p === repoName || p.startsWith(repoName + '-'))
if (repoIndex >= 0) {
const subPath = pathParts.slice(repoIndex + 1)
setDisplayPath(subPath.length > 0 ? '/' + subPath.join('/') : '/')
} else {
setDisplayPath('/' + pathParts.join('/'))
}
} else {
setDisplayPath('/' + pathParts.join('/'))
}
}
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
@@ -44,17 +67,15 @@ export function FileBrowserSheet({ isOpen, onClose, basePath = '', repoName, ini
<div className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm">
<div className="absolute inset-0 bg-background">
{/* Header */}
<div className="sticky top-0 z-10 border-b border-border bg-background backdrop-blur-sm px-4 py-3">
<div className="sticky top-0 z-10 border-b border-border bg-background backdrop-blur-sm px-4 py-1">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<h1 className="text-lg font-semibold text-foreground">
{repoName ? `${repoName} - Files` : 'Workspace Files'}
</h1>
{basePath && (
<span className="text-sm text-muted-foreground bg-muted px-2 py-1 rounded">
{basePath}
</span>
{(displayPath === '/' || !repoName) && (
<h1 className="text-sm font-semibold text-foreground">
{repoName || 'Workspace Files'}
</h1>
)}
<PathDisplay path={displayPath} maxSegments={3} />
</div>
{!isEditing && (
<Button
@@ -72,12 +93,13 @@ export function FileBrowserSheet({ isOpen, onClose, basePath = '', repoName, ini
{/* File Browser Content */}
<div className="h-[calc(100vh-73px)] overflow-hidden">
<FileBrowser
basePath={basePath}
basePath={normalizedBasePath}
embedded={true}
initialSelectedFile={initialSelectedFile}
onDirectoryLoad={handleDirectoryLoad}
/>
</div>
</div>
</div>
)
}
}
@@ -182,12 +182,6 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o
</div>
<div className="flex items-center gap-1 flex-shrink-0 mt-1">
{isMobileModal && onCloseModal && (
<Button variant="outline" size="sm" onClick={onCloseModal} className="border-destructive bg-destructive/10 text-destructive hover:bg-destructive/20 h-7 w-7 p-0">
<X className="w-3 h-3" />
</Button>
)}
{isTextFile && viewMode !== 'edit' && (
<Button variant="outline" size="sm" onClick={handleEdit} className="h-7 w-7 p-0">
<Edit3 className="w-3 h-3" />
@@ -216,6 +210,12 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o
<Download className="w-3 h-3" />
</Button>
)}
{viewMode !== 'edit' && isMobileModal && onCloseModal && (
<Button variant="outline" size="sm" onClick={onCloseModal} className="border-destructive bg-destructive/10 text-destructive hover:bg-destructive/20 h-7 w-7 p-0">
<X className="w-3 h-3" />
</Button>
)}
</div>
</div>
</>
@@ -15,14 +15,6 @@ export function MobileFilePreviewModal({
file,
showFilePreviewHeader = false,
}: MobileFilePreviewModalProps) {
const handleTouchStart = (e: React.TouchEvent) => {
e.preventDefault();
};
const handleTouchEnd = (e: React.TouchEvent) => {
e.preventDefault();
};
if (!file || file.isDirectory) {
return null;
}
@@ -31,8 +23,6 @@ export function MobileFilePreviewModal({
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent
className="w-screen h-screen max-w-none max-h-none p-0 bg-background border-0 flex flex-col"
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
hideCloseButton
>
<div
@@ -49,4 +39,3 @@ export function MobileFilePreviewModal({
</Dialog>
);
}
@@ -0,0 +1,32 @@
import { useMemo } from 'react'
interface PathDisplayProps {
path: string
maxSegments?: number
className?: string
}
export function PathDisplay({ path, maxSegments = 3, className = '' }: PathDisplayProps) {
const displayPath = useMemo(() => {
if (!path || path === '/') return '/'
const normalizedPath = path.startsWith('/') ? path : '/' + path
const segments = normalizedPath.split('/').filter(Boolean)
if (segments.length <= maxSegments) {
return '/' + segments.join('/')
}
const visibleSegments = segments.slice(-maxSegments)
return '/.../' + visibleSegments.join('/')
}, [path, maxSegments])
return (
<span
className={`text-sm text-muted-foreground bg-muted px-2 py-1 rounded font-mono truncate ${className}`}
title={path}
>
{displayPath}
</span>
)
}
+1
View File
@@ -7,4 +7,5 @@ export interface FileInfo {
content?: string
children?: FileInfo[]
lastModified: Date
workspaceRoot?: string
}