diff --git a/frontend/src/components/file-browser/FileDiffView.tsx b/frontend/src/components/file-browser/FileDiffView.tsx index 8bba1c3f..893ea8d9 100644 --- a/frontend/src/components/file-browser/FileDiffView.tsx +++ b/frontend/src/components/file-browser/FileDiffView.tsx @@ -1,5 +1,5 @@ import { useFileDiff } from '@/api/git' -import { Loader2, FileText, FilePlus, FileX, FileEdit, File, Plus, Minus, ArrowLeft } from 'lucide-react' +import { Loader2, FileText, FilePlus, FileX, FileEdit, File, Plus, Minus, ArrowLeft, ExternalLink } from 'lucide-react' import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import type { GitFileStatusType } from '@/types/git' @@ -8,6 +8,7 @@ interface FileDiffViewProps { repoId: number filePath: string onBack?: () => void + onOpenFile?: (path: string, lineNumber?: number) => void isMobile?: boolean } @@ -59,7 +60,7 @@ const statusConfig: Record void }) { if (line.type === 'header') { return (
@@ -88,8 +89,18 @@ function DiffLineComponent({ line, showLineNumbers }: { line: DiffLine; showLine ? 'text-red-400' : 'text-foreground' + const lineNumber = line.newLineNumber ?? line.oldLineNumber + const isClickable = onLineClick && lineNumber !== undefined + return ( -
+
isClickable && lineNumber !== undefined && onLineClick(lineNumber)} + > {showLineNumbers && (
@@ -111,7 +122,7 @@ function DiffLineComponent({ line, showLineNumbers }: { line: DiffLine; showLine ) } -export function FileDiffView({ repoId, filePath, onBack, isMobile = false }: FileDiffViewProps) { +export function FileDiffView({ repoId, filePath, onBack, onOpenFile, isMobile = false }: FileDiffViewProps) { const { data: diffData, isLoading, error } = useFileDiff(repoId, filePath) const fileName = filePath.split('/').pop() || filePath @@ -159,8 +170,23 @@ export function FileDiffView({ repoId, filePath, onBack, isMobile = false }: Fil )}
-
{fileName}
- {dirPath &&
{dirPath}
} + {onOpenFile ? ( + + ) : ( + <> +
{fileName}
+ {dirPath &&
{dirPath}
} + + )}
@@ -191,6 +217,7 @@ export function FileDiffView({ repoId, filePath, onBack, isMobile = false }: Fil key={index} line={line} showLineNumbers={!isMobile} + onLineClick={onOpenFile ? (lineNum) => onOpenFile(filePath, lineNum) : undefined} /> ))}
diff --git a/frontend/src/components/file-browser/FilePreview.tsx b/frontend/src/components/file-browser/FilePreview.tsx index 17c8b17f..21de7292 100644 --- a/frontend/src/components/file-browser/FilePreview.tsx +++ b/frontend/src/components/file-browser/FilePreview.tsx @@ -1,9 +1,9 @@ -import { useState, useCallback } from 'react' +import { useState, useCallback, useRef, useEffect } from 'react' import { Button } from '@/components/ui/button' -import { Download, Copy, X, Edit3, Save, X as XIcon } from 'lucide-react' +import { Download, X, Edit3, Save, X as XIcon } from 'lucide-react' import type { FileInfo } from '@/types/files' import { API_BASE_URL } from '@/config' -import { VirtualizedTextView } from '@/components/ui/virtualized-text-view' +import { VirtualizedTextView, type VirtualizedTextViewHandle } from '@/components/ui/virtualized-text-view' const API_BASE = API_BASE_URL @@ -14,15 +14,34 @@ interface FilePreviewProps { hideHeader?: boolean isMobileModal?: boolean onCloseModal?: () => void + onFileSaved?: () => void + initialLineNumber?: number } -export function FilePreview({ file, hideHeader = false, isMobileModal = false, onCloseModal }: FilePreviewProps) { +export function FilePreview({ file, hideHeader = false, isMobileModal = false, onCloseModal, onFileSaved, initialLineNumber }: FilePreviewProps) { const [viewMode, setViewMode] = useState<'preview' | 'edit'>('preview') const [editContent, setEditContent] = useState('') const [isSaving, setIsSaving] = useState(false) const [hasVirtualizedChanges, setHasVirtualizedChanges] = useState(false) + const [highlightedLine, setHighlightedLine] = useState(initialLineNumber) + const virtualizedRef = useRef(null) + const contentRef = useRef(null) const shouldVirtualize = file.size > VIRTUALIZATION_THRESHOLD_BYTES && !file.mimeType?.startsWith('image/') + + useEffect(() => { + if (initialLineNumber && contentRef.current && !shouldVirtualize) { + const lineHeight = 20 + const scrollPosition = (initialLineNumber - 1) * lineHeight + setHighlightedLine(initialLineNumber) + setTimeout(() => { + contentRef.current?.scrollTo({ top: scrollPosition, behavior: 'smooth' }) + }, 100) + setTimeout(() => { + setHighlightedLine(undefined) + }, 3000) + } + }, [initialLineNumber, shouldVirtualize, file.content]) const formatFileSize = (bytes: number) => { if (bytes === 0) return '0 Bytes' @@ -58,17 +77,6 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o } } - const handleCopyContent = async () => { - if (file.content) { - try { - const content = decodeBase64(file.content) - await navigator.clipboard.writeText(content) - } catch (error) { - console.error('Failed to copy content:', error) - } - } - } - const handleEdit = () => { if (shouldVirtualize) { setViewMode('edit') @@ -77,16 +85,14 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o return } - if (file.content) { - try { - const content = decodeBase64(file.content) - setEditContent(content) - setViewMode('edit') - const event = new CustomEvent('editModeChange', { detail: { isEditing: true } }) - window.dispatchEvent(event) - } catch (err) { - console.error('Failed to load content for editing:', err) - } + try { + const content = file.content ? decodeBase64(file.content) : '' + setEditContent(content) + setViewMode('edit') + const event = new CustomEvent('editModeChange', { detail: { isEditing: true } }) + window.dispatchEvent(event) + } catch (err) { + console.error('Failed to load content for editing:', err) } } @@ -108,6 +114,7 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o window.dispatchEvent(editEvent) const event = new CustomEvent('fileSaved', { detail: { path: file.path, content: editContent } }) window.dispatchEvent(event) + onFileSaved?.() } catch (err) { console.error('Failed to save file:', err) } finally { @@ -133,7 +140,8 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o window.dispatchEvent(editEvent) const event = new CustomEvent('fileSaved', { detail: { path: file.path } }) window.dispatchEvent(event) - }, [file.path]) + onFileSaved?.() + }, [file.path, onFileSaved]) const isTextFile = file.mimeType?.startsWith('text/') || ['application/json', 'application/xml', 'text/javascript', 'text/typescript'].includes(file.mimeType || '') @@ -154,12 +162,14 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o if (shouldVirtualize && isTextFile) { return ( ) } @@ -183,10 +193,28 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o try { const textContent = decodeBase64(file.content) + const lines = textContent.split('\n') return ( -
-            {textContent}
-          
+
+ {lines.map((line, index) => { + const lineNum = index + 1 + const isHighlighted = highlightedLine === lineNum + return ( +
+ + {lineNum} + +
+                    {line || ' '}
+                  
+
+ ) + })} +
) } catch { return ( @@ -204,7 +232,18 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o ) } - const showSaveButton = viewMode === 'edit' && !shouldVirtualize + const handleVirtualizedSaveClick = async () => { + if (virtualizedRef.current) { + setIsSaving(true) + try { + await virtualizedRef.current.save() + } finally { + setIsSaving(false) + } + } + } + + const showSaveButton = viewMode === 'edit' const showCancelButton = viewMode === 'edit' return ( @@ -231,37 +270,31 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o
{isTextFile && viewMode !== 'edit' && ( - )} {showSaveButton && ( - )} {showCancelButton && ( - )} - {isTextFile && viewMode !== 'edit' && !shouldVirtualize && ( - - )} - {viewMode !== 'edit' && ( - )} {viewMode !== 'edit' && isMobileModal && onCloseModal && ( - )} @@ -270,7 +303,10 @@ export function FilePreview({ file, hideHeader = false, isMobileModal = false, o )} -
+
{renderContent()}
diff --git a/frontend/src/components/file-browser/FilePreviewDialog.tsx b/frontend/src/components/file-browser/FilePreviewDialog.tsx new file mode 100644 index 00000000..12ef5a0f --- /dev/null +++ b/frontend/src/components/file-browser/FilePreviewDialog.tsx @@ -0,0 +1,90 @@ +import { useState, useEffect } from 'react' +import { Dialog, DialogContent } from '@/components/ui/dialog' +import { FilePreview } from './FilePreview' +import { Loader2, FileText } from 'lucide-react' +import { API_BASE_URL } from '@/config' +import type { FileInfo } from '@/types/files' + +interface FilePreviewDialogProps { + isOpen: boolean + onClose: () => void + filePath: string + repoBasePath?: string + onFileSaved?: () => void + initialLineNumber?: number +} + +export function FilePreviewDialog({ isOpen, onClose, filePath, repoBasePath, onFileSaved, initialLineNumber }: FilePreviewDialogProps) { + const [file, setFile] = useState(null) + const [isLoading, setIsLoading] = useState(false) + const [error, setError] = useState(null) + + useEffect(() => { + if (!isOpen || !filePath) { + setFile(null) + setError(null) + return + } + + const fetchFile = async () => { + setIsLoading(true) + setError(null) + + try { + const fullPath = repoBasePath ? `${repoBasePath}/${filePath}` : filePath + const response = await fetch(`${API_BASE_URL}/api/files/${fullPath}`) + + if (!response.ok) { + throw new Error(`Failed to load file: ${response.statusText}`) + } + + const data = await response.json() + setFile(data) + } catch (err) { + setError(err instanceof Error ? err.message : 'Failed to load file') + } finally { + setIsLoading(false) + } + } + + fetchFile() + }, [isOpen, filePath, repoBasePath]) + + const handleOpenChange = (open: boolean) => { + if (!open) { + onClose() + } + } + + return ( + + +
+ {isLoading ? ( +
+ +
+ ) : error ? ( +
+ +

Failed to load file

+

{error}

+
+ ) : file ? ( + + ) : null} +
+
+
+ ) +} diff --git a/frontend/src/components/file-browser/GitChangesSheet.tsx b/frontend/src/components/file-browser/GitChangesSheet.tsx index 9e08d1e1..d7e5aca9 100644 --- a/frontend/src/components/file-browser/GitChangesSheet.tsx +++ b/frontend/src/components/file-browser/GitChangesSheet.tsx @@ -2,24 +2,31 @@ import { useState, useEffect } from 'react' import { Dialog, DialogContent } from '@/components/ui/dialog' import { GitChangesPanel } from './GitChangesPanel' import { FileDiffView } from './FileDiffView' +import { FilePreviewDialog } from './FilePreviewDialog' import { Button } from '@/components/ui/button' import { X, GitBranch } from 'lucide-react' import { useMobile } from '@/hooks/useMobile' +import { useQueryClient } from '@tanstack/react-query' interface GitChangesSheetProps { isOpen: boolean onClose: () => void repoId: number currentBranch: string + repoLocalPath?: string } -export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch }: GitChangesSheetProps) { +export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch, repoLocalPath }: GitChangesSheetProps) { const [selectedFile, setSelectedFile] = useState() + const [previewFilePath, setPreviewFilePath] = useState(null) + const [previewLineNumber, setPreviewLineNumber] = useState() const isMobile = useMobile() + const queryClient = useQueryClient() useEffect(() => { if (!isOpen) { setSelectedFile(undefined) + setPreviewFilePath(null) } }, [isOpen]) @@ -31,6 +38,21 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch }: GitC setSelectedFile(undefined) } + const handleOpenFile = (path: string, lineNumber?: number) => { + setPreviewFilePath(path) + setPreviewLineNumber(lineNumber) + } + + const handleClosePreview = () => { + setPreviewFilePath(null) + setPreviewLineNumber(undefined) + } + + const handleFileSaved = () => { + queryClient.invalidateQueries({ queryKey: ['gitStatus'] }) + queryClient.invalidateQueries({ queryKey: ['fileDiff'] }) + } + const handleOpenChange = (open: boolean) => { if (!open) { onClose() @@ -68,6 +90,7 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch }: GitC repoId={repoId} filePath={selectedFile} onBack={handleBack} + onOpenFile={handleOpenFile} isMobile={true} /> ) : ( @@ -91,6 +114,7 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch }: GitC ) : ( @@ -102,6 +126,15 @@ export function GitChangesSheet({ isOpen, onClose, repoId, currentBranch }: GitC
)}
+ + ) diff --git a/frontend/src/components/repo/BranchSwitcher.tsx b/frontend/src/components/repo/BranchSwitcher.tsx index e1beeef3..43df2958 100644 --- a/frontend/src/components/repo/BranchSwitcher.tsx +++ b/frontend/src/components/repo/BranchSwitcher.tsx @@ -19,9 +19,10 @@ interface BranchSwitcherProps { currentBranch: string; isWorktree?: boolean; repoUrl: string; + repoLocalPath?: string; } -export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl }: BranchSwitcherProps) { +export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, repoLocalPath }: BranchSwitcherProps) { const [addBranchOpen, setAddBranchOpen] = useState(false); const [gitChangesOpen, setGitChangesOpen] = useState(false); const queryClient = useQueryClient(); @@ -122,6 +123,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl }: B onClose={() => setGitChangesOpen(false)} repoId={repoId} currentBranch={currentBranch} + repoLocalPath={repoLocalPath} /> ); diff --git a/frontend/src/components/session/SessionDetailHeader.tsx b/frontend/src/components/session/SessionDetailHeader.tsx index 007dabf7..6da25250 100644 --- a/frontend/src/components/session/SessionDetailHeader.tsx +++ b/frontend/src/components/session/SessionDetailHeader.tsx @@ -123,6 +123,7 @@ export function SessionDetailHeader({ currentBranch={currentBranch} isWorktree={repo.isWorktree} repoUrl={repo.repoUrl} + repoLocalPath={repo.localPath} />
diff --git a/frontend/src/components/ui/virtualized-text-view.tsx b/frontend/src/components/ui/virtualized-text-view.tsx index 4a29a0ad..5c296669 100644 --- a/frontend/src/components/ui/virtualized-text-view.tsx +++ b/frontend/src/components/ui/virtualized-text-view.tsx @@ -1,4 +1,4 @@ -import { useRef, useCallback, useEffect, useState, useMemo } from 'react' +import { useRef, useCallback, useEffect, useState, useMemo, forwardRef, useImperativeHandle } from 'react' import { useVirtualizedContent } from '@/hooks/useVirtualizedContent' interface VirtualizedTextViewProps { @@ -9,12 +9,17 @@ interface VirtualizedTextViewProps { onSaveStateChange?: (hasUnsavedChanges: boolean) => void onSave?: () => void className?: string + initialLineNumber?: number +} + +export interface VirtualizedTextViewHandle { + save: () => Promise } const LINE_HEIGHT = 20 const GUTTER_WIDTH = 60 -export function VirtualizedTextView({ +export const VirtualizedTextView = forwardRef(function VirtualizedTextView({ filePath, totalLines: initialTotalLines = 0, lineHeight = LINE_HEIGHT, @@ -22,10 +27,12 @@ export function VirtualizedTextView({ onSaveStateChange, onSave, className = '', -}: VirtualizedTextViewProps) { + initialLineNumber, +}, ref) { const containerRef = useRef(null) const [scrollTop, setScrollTop] = useState(0) const [viewportHeight, setViewportHeight] = useState(600) + const [highlightedLine, setHighlightedLine] = useState(initialLineNumber) const { lines, @@ -75,6 +82,19 @@ export function VirtualizedTextView({ resizeObserver.observe(container) return () => resizeObserver.disconnect() }, []) + + useEffect(() => { + if (initialLineNumber && containerRef.current) { + const scrollPosition = (initialLineNumber - 1) * lineHeight + setHighlightedLine(initialLineNumber) + setTimeout(() => { + containerRef.current?.scrollTo({ top: scrollPosition, behavior: 'smooth' }) + }, 100) + setTimeout(() => { + setHighlightedLine(undefined) + }, 3000) + } + }, [initialLineNumber, lineHeight]) const handleScroll = useCallback((e: React.UIEvent) => { setScrollTop(e.currentTarget.scrollTop) @@ -93,6 +113,10 @@ export function VirtualizedTextView({ } }, [saveEdits, onSave]) + useImperativeHandle(ref, () => ({ + save: handleSave, + }), [handleSave]) + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key === 's') { e.preventDefault() @@ -138,10 +162,12 @@ export function VirtualizedTextView({ style={{ height: '100%' }} >
- {visibleLines.map(({ lineNum, content, isEdited }) => ( + {visibleLines.map(({ lineNum, content, isEdited }) => { + const isHighlighted = highlightedLine === lineNum + 1 + return (
)}
- ))} + )})} {isLoading && visibleLines.length === 0 && (
@@ -197,4 +223,4 @@ export function VirtualizedTextView({ )}
) -} +})