mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Feature/enhanced source control (#138)
* feat: enhance source control with discard and commit detail views - Add discard changes functionality with confirmation dialog - Implement commit detail view with file list and diffs - Update commit badges: blue for unpushed (local), purple for pushed (remote) - Display current branch in commits header - Reuse existing components (DeleteDialog, FileDiffView, GitFlatFileList) - Add proper error handling and toast notifications - Extend backend API with /discard and /commit/:hash endpoints - Update types to include CommitDetails and CommitFile interfaces Backend: - Add discardChanges() method to GitService - Add getCommitDetails() and getCommitDiff() methods - Add POST /:id/git/discard endpoint - Add GET /:id/git/commit/:hash and /:id/git/commit/:hash/diff endpoints Frontend: - Add discard button (rotate-left icon) to file items - Add gitDiscardFiles API function and useGit mutation - Create CommitDetailView component - Update CommitsTab with color coding and branch display - Extend SourceControlPanel for commit detail navigation - Add confirmation dialog using DeleteDialog pattern Tests: - Backend tests passing with 77% line coverage - Linting passes for all modified files - Type safety maintained throughout BREAKING CHANGES: None * Fix critical bugs from code review - Fix syntax error in GitService.ts (missing closing brace for getCommitDiff) - Make AUTH_SECRET mandatory in crypto.ts to prevent weak encryption fallback - Fix race condition in PassphraseHandler by using unique request IDs - Add error logging to ssh-key-manager catch blocks - Add type guards (isGitCredential, filterGitCredentials) to replace unsafe assertions - Fix TypeScript errors and variable shadowing in GitService and settings - Remove unused variable in sshHostKeyHandler - Update GitService tests with proper mock data structure All changes verified: lint passes, typecheck passes, 102/103 tests pass * Fix critical bugs from code review - Fix git restore command syntax (--source=HEAD → --source HEAD) - Use crypto.randomBytes for secure requestId generation - Add requestId to SSH host key broadcast for frontend compatibility - Add missing onDiscard prop to GitFlatFileList - Remove dead code (187 unreachable lines) in ChangesTab - Fix regex alternation bug in commit stat parsing - Optimize getCommitDetails with --numstat (N+1 → 1 git call) - Optimize unpushed commit check to use merge-base instead of full list - Remove useless conditional in handleDiscard - Fix type contract violation in CommitDetailView onSelect * fix: resolve code review issues - Add missing handleDiscard function in ChangesTab.tsx - Fix duplicate requestId property in sshHostKeyHandler.ts - Remove unnecessary type assertion in settings.ts * feat: enhance source control with commit view improvements and diff fixes - Add readOnly mode to GitFlatFileList to hide stage/unstage buttons in commit view - Fix commit file diff display by using correct git show command syntax - Add line stats display (+additions/-deletions) for each file in commit view - Show total commit stats in header (Changed Files N +X -Y) - Fix commit file list parsing to use --name-status and --numstat - Add 500KB diff size limit with truncation warning - Fix discard changes for untracked files by deleting them directly - Update Dockerfile to copy backend/node_modules correctly - Fix TypeScript error in CommitDetailView onFileSelect call * Refactor API error handling and enhance source control functionality * Fix code review issues: OAuth error handling, unpushed detection, comments, and duplicates - Remove shared handleApiError import from OAuth and restore local pattern - Fix isCommitUnpushed to use getUnpushedCommitHashes for correct upstream detection - Remove inline comments from ssh-key-manager.ts (style violation) - Remove duplicate fetchCommitDiff function - Add missing getErrorMessage import to repo-git.ts * Refactor imports and type assertions in GitService, SSH tests, and handleApiError * Refactor GitService methods for improved error handling and code clarity; add discardChanges functionality and enhance commit details retrieval * Refactor Git and SSH services: streamline imports, enhance error handling, and remove unused code * Refactor source control components: remove unused useGitAction hook and enhance error handling in handleGitAction * Enhance error handling in Git routes and improve UI components for better user experience * Remove unnecessary blank line in GitService class * Fix commit log parsing and rename detection in GitService * Refactor source control UI for full-screen diffs, improve mobile view, and add discard confirmation dialog * Fix lint warnings and ignore test files in GitGuardian scan --------- Co-authored-by: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com>
This commit is contained in:
1 parent
ba0501ccf7
commit
da283a82cf
20 files changed
+1612
-206
No files matched your search
@@ -4,23 +4,28 @@ import { useGit } from '@/hooks/useGit'
|
||||
import { GitFlatFileList } from './GitFlatFileList'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
|
||||
import { FileDiffView } from '@/components/file-browser/FileDiffView'
|
||||
import { DiscardDialog } from '@/components/ui/discard-dialog'
|
||||
import { Loader2, GitCommit, FileText, AlertCircle } from 'lucide-react'
|
||||
|
||||
interface ChangesTabProps {
|
||||
repoId: number
|
||||
onFileSelect: (path: string, staged: boolean) => void
|
||||
onClearFileSelection?: () => void
|
||||
selectedFile?: {path: string, staged: boolean}
|
||||
isMobile: boolean
|
||||
onError?: (error: unknown) => void
|
||||
}
|
||||
|
||||
export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onError }: ChangesTabProps) {
|
||||
export function ChangesTab({ repoId, onFileSelect, onClearFileSelection, selectedFile, isMobile, onError }: ChangesTabProps) {
|
||||
const { data: status, isLoading, error } = useGitStatus(repoId)
|
||||
const git = useGit(repoId, onError)
|
||||
const [commitMessage, setCommitMessage] = useState('')
|
||||
|
||||
const [discardDialogOpen, setDiscardDialogOpen] = useState(false)
|
||||
const [discardPaths, setDiscardPaths] = useState<string[]>([])
|
||||
const [discardStaged, setDiscardStaged] = useState(false)
|
||||
|
||||
const stagedFiles = status?.files.filter(f => f.staged) || []
|
||||
const unstagedFiles = status?.files.filter(f => !f.staged) || []
|
||||
const canCommit = commitMessage.trim() && stagedFiles.length > 0 && !git.commit.isPending
|
||||
@@ -33,6 +38,23 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onErr
|
||||
git.unstageFiles.mutate(paths)
|
||||
}
|
||||
|
||||
const handleDiscard = (paths: string[], staged: boolean) => {
|
||||
setDiscardPaths(paths)
|
||||
setDiscardStaged(staged)
|
||||
setDiscardDialogOpen(true)
|
||||
}
|
||||
|
||||
const confirmDiscard = () => {
|
||||
git.discardFiles.mutate({ paths: discardPaths, staged: discardStaged })
|
||||
setDiscardDialogOpen(false)
|
||||
setDiscardPaths([])
|
||||
}
|
||||
|
||||
const cancelDiscard = () => {
|
||||
setDiscardDialogOpen(false)
|
||||
setDiscardPaths([])
|
||||
}
|
||||
|
||||
const handleCommit = () => {
|
||||
git.commit.mutate({ message: commitMessage.trim() })
|
||||
setCommitMessage('')
|
||||
@@ -60,83 +82,13 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onErr
|
||||
|
||||
if (isMobile && selectedFile) {
|
||||
return (
|
||||
<div className="flex flex-col h-full">
|
||||
<Tabs defaultValue="files" className="flex flex-col h-full">
|
||||
<TabsList className="flex-shrink-0">
|
||||
<TabsTrigger value="files">Files ({stagedFiles.length + unstagedFiles.length})</TabsTrigger>
|
||||
<TabsTrigger value="diff">{selectedFile.path.split('/').pop() || selectedFile.path}</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="files" className="flex-1 overflow-hidden mt-0">
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex-1 overflow-y-auto p-3 space-y-4">
|
||||
{status.hasChanges ? (
|
||||
<>
|
||||
{stagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={stagedFiles}
|
||||
staged={true}
|
||||
onSelect={onFileSelect}
|
||||
onUnstage={handleUnstage}
|
||||
selectedFile={selectedFile?.path}
|
||||
/>
|
||||
)}
|
||||
|
||||
{unstagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={unstagedFiles}
|
||||
staged={false}
|
||||
onSelect={onFileSelect}
|
||||
onStage={handleStage}
|
||||
selectedFile={selectedFile?.path}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="text-center py-12 text-muted-foreground">
|
||||
<FileText className="w-8 h-8 mx-auto mb-2 opacity-50" />
|
||||
<p className="text-sm">No uncommitted changes</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{status.hasChanges && (
|
||||
<div className="p-3 border-t border-border space-y-2 flex-shrink-0">
|
||||
<Textarea
|
||||
placeholder="Commit message..."
|
||||
value={commitMessage}
|
||||
onChange={(e) => setCommitMessage(e.target.value)}
|
||||
className="min-h-[80px] md:text-sm resize-none"
|
||||
onKeyDown={(e) => {
|
||||
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter' && canCommit) {
|
||||
handleCommit()
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
onClick={handleCommit}
|
||||
disabled={!canCommit}
|
||||
className="w-full h-9"
|
||||
>
|
||||
{git.commit.isPending ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin mr-2" />
|
||||
) : (
|
||||
<GitCommit className="w-4 h-4 mr-2" />
|
||||
)}
|
||||
Commit {stagedFiles.length > 0 && `(${stagedFiles.length} staged)`}
|
||||
</Button>
|
||||
<p className="text-[10px] text-muted-foreground text-center">
|
||||
{navigator.platform.includes('Mac') ? '⌘' : 'Ctrl'}+Enter to commit
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="diff" className="flex-1 overflow-hidden mt-0">
|
||||
<FileDiffView repoId={repoId} filePath={selectedFile.path} includeStaged={selectedFile.staged} />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
<div className="flex flex-col h-full min-h-0 overflow-hidden">
|
||||
<FileDiffView
|
||||
repoId={repoId}
|
||||
filePath={selectedFile.path}
|
||||
includeStaged={selectedFile.staged}
|
||||
onBack={onClearFileSelection}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
@@ -151,6 +103,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onErr
|
||||
staged={true}
|
||||
onSelect={onFileSelect}
|
||||
onUnstage={handleUnstage}
|
||||
onDiscard={handleDiscard}
|
||||
selectedFile={selectedFile?.path}
|
||||
/>
|
||||
)}
|
||||
@@ -161,6 +114,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onErr
|
||||
staged={false}
|
||||
onSelect={onFileSelect}
|
||||
onStage={handleStage}
|
||||
onDiscard={handleDiscard}
|
||||
selectedFile={selectedFile?.path}
|
||||
/>
|
||||
)}
|
||||
@@ -203,6 +157,15 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile, onErr
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DiscardDialog
|
||||
open={discardDialogOpen}
|
||||
onOpenChange={setDiscardDialogOpen}
|
||||
onConfirm={confirmDiscard}
|
||||
onCancel={cancelDiscard}
|
||||
fileCount={discardPaths.length}
|
||||
isDiscarding={git.discardFiles.isPending}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useCommitDetails } from '@/api/git'
|
||||
import { Loader2, GitCommit, ArrowLeft } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { GitFlatFileList } from './GitFlatFileList'
|
||||
import { FileDiffView } from '@/components/file-browser/FileDiffView'
|
||||
|
||||
interface CommitDetailViewProps {
|
||||
repoId: number
|
||||
commitHash: string
|
||||
onBack: () => void
|
||||
onFileSelect: (path: string) => void
|
||||
selectedFile?: string
|
||||
}
|
||||
|
||||
export function CommitDetailView({ repoId, commitHash, onBack, onFileSelect, selectedFile }: CommitDetailViewProps) {
|
||||
const { data: commit, isLoading, error } = useCommitDetails(repoId, commitHash)
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center h-full py-12">
|
||||
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (error || !commit) {
|
||||
return (
|
||||
<div className="text-center py-12 text-muted-foreground">
|
||||
<GitCommit className="w-8 h-8 mx-auto mb-2 opacity-50" />
|
||||
<p className="text-sm">Failed to load commit details</p>
|
||||
<p className="text-xs mt-1">{error?.message}</p>
|
||||
<Button variant="outline" size="sm" onClick={onBack} className="mt-4">
|
||||
<ArrowLeft className="w-4 h-4 mr-2" />
|
||||
Back to commits
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const commitFiles = commit.files.map(f => ({
|
||||
path: f.path,
|
||||
status: f.status,
|
||||
staged: false,
|
||||
oldPath: f.oldPath,
|
||||
additions: f.additions,
|
||||
deletions: f.deletions
|
||||
}))
|
||||
|
||||
const totalAdditions = commit.files.reduce((sum, f) => sum + (f.additions || 0), 0)
|
||||
const totalDeletions = commit.files.reduce((sum, f) => sum + (f.deletions || 0), 0)
|
||||
|
||||
if (selectedFile) {
|
||||
return (
|
||||
<div className="flex flex-col h-full min-h-0 overflow-hidden">
|
||||
<FileDiffView
|
||||
repoId={repoId}
|
||||
filePath={selectedFile}
|
||||
includeStaged={false}
|
||||
commitHash={commitHash}
|
||||
onBack={() => onFileSelect('')}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="px-3 py-3 border-b border-border flex-shrink-0 space-y-2">
|
||||
<div className="flex items-start gap-2">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onBack}
|
||||
className="h-7 w-7 p-0 flex-shrink-0 mt-0.5"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
</Button>
|
||||
<p className="text-sm font-medium leading-snug">{commit.message}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground pl-9">
|
||||
<span className="font-mono">{commit.hash.substring(0, 7)}</span>
|
||||
<span>·</span>
|
||||
<span className="truncate">{commit.authorName}</span>
|
||||
<span>·</span>
|
||||
<span className="flex-shrink-0">{new Date(parseInt(commit.date, 10) * 1000).toLocaleDateString()}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto px-1 pt-1">
|
||||
<GitFlatFileList
|
||||
files={commitFiles}
|
||||
staged={false}
|
||||
onSelect={(path) => onFileSelect(path)}
|
||||
selectedFile={selectedFile}
|
||||
readOnly={true}
|
||||
totalAdditions={totalAdditions}
|
||||
totalDeletions={totalDeletions}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -59,34 +59,41 @@ export function CommitsTab({ repoId, onSelectCommit }: CommitsTabProps) {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full overflow-y-auto">
|
||||
{data.commits.map((commit) => (
|
||||
<button
|
||||
key={commit.hash}
|
||||
className="flex items-start gap-3 px-3 py-2 text-left hover:bg-accent/50 transition-colors border-b border-border last:border-0"
|
||||
onClick={() => onSelectCommit?.(commit.hash)}
|
||||
>
|
||||
<div className="flex-shrink-0 w-8 h-8 rounded-full bg-accent flex items-center justify-center mt-0.5">
|
||||
<GitCommit className="w-4 h-4 text-muted-foreground" />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium truncate">{commit.message}</p>
|
||||
<div className="flex items-center gap-2 mt-1 text-xs text-muted-foreground">
|
||||
<span className="font-mono">{commit.hash.substring(0, 7)}</span>
|
||||
<span>·</span>
|
||||
<span className="truncate">{commit.authorName}</span>
|
||||
<span>·</span>
|
||||
<span className="flex-shrink-0">{formatRelativeTime(commit.date)}</span>
|
||||
{commit.unpushed && (
|
||||
<span className={cn('flex items-center gap-0.5 px-1 rounded', GIT_UI_COLORS.unpushed)}>
|
||||
<ArrowUp className="w-3 h-3" />
|
||||
Local
|
||||
</span>
|
||||
)}
|
||||
<div className="flex flex-col h-full overflow-x-hidden">
|
||||
<div className="flex-1 overflow-y-auto overflow-x-hidden">
|
||||
{data.commits.map((commit) => (
|
||||
<button
|
||||
key={commit.hash}
|
||||
className="flex items-start gap-3 px-3 py-2 text-left hover:bg-accent/50 transition-colors border-b border-border last:border-0"
|
||||
onClick={() => onSelectCommit?.(commit.hash)}
|
||||
>
|
||||
<div className="flex-shrink-0 w-8 h-8 rounded-full bg-accent flex items-center justify-center mt-0.5">
|
||||
<GitCommit className="w-4 h-4 text-muted-foreground" />
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium line-clamp-2">{commit.message}</p>
|
||||
<div className="flex items-center gap-2 mt-1 text-xs text-muted-foreground overflow-hidden">
|
||||
<span className="font-mono">{commit.hash.substring(0, 7)}</span>
|
||||
<span>·</span>
|
||||
<span className="truncate">{commit.authorName}</span>
|
||||
<span>·</span>
|
||||
<span className="flex-shrink-0">{formatRelativeTime(commit.date)}</span>
|
||||
{commit.unpushed && (
|
||||
<span className={cn('flex items-center gap-0.5 px-1 rounded', GIT_UI_COLORS.unpushed)}>
|
||||
<ArrowUp className="w-3 h-3" />
|
||||
Local
|
||||
</span>
|
||||
)}
|
||||
{!commit.unpushed && (
|
||||
<span className={cn('flex items-center gap-0.5 px-1 rounded', GIT_UI_COLORS.pushed)}>
|
||||
Remote
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Plus, Minus, FileText, FilePlus, FileX, FileSearch, CircleDot } from 'lucide-react'
|
||||
import { Plus, Minus, FileText, FilePlus, FileX, FileSearch, CircleDot, RotateCcw } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { GitFileStatus } from '@/types/git'
|
||||
import { GIT_STATUS_COLORS, GIT_UI_COLORS } from '@/lib/git-status-styles'
|
||||
@@ -10,6 +10,7 @@ interface GitFlatFileItemProps {
|
||||
onSelect: (path: string, staged: boolean) => void
|
||||
onStage?: (path: string) => void
|
||||
onUnstage?: (path: string) => void
|
||||
onDiscard?: (path: string, staged: boolean) => void
|
||||
}
|
||||
|
||||
const statusIcons = {
|
||||
@@ -21,7 +22,7 @@ const statusIcons = {
|
||||
copied: FilePlus,
|
||||
}
|
||||
|
||||
export function GitFlatFileItem({ file, isSelected, onSelect, onStage, onUnstage }: GitFlatFileItemProps) {
|
||||
export function GitFlatFileItem({ file, isSelected, onSelect, onStage, onUnstage, onDiscard }: GitFlatFileItemProps) {
|
||||
const StatusIcon = statusIcons[file.status] || FileText
|
||||
const statusColor = GIT_STATUS_COLORS[file.status] || 'text-muted-foreground'
|
||||
|
||||
@@ -37,6 +38,13 @@ export function GitFlatFileItem({ file, isSelected, onSelect, onStage, onUnstage
|
||||
}
|
||||
}
|
||||
|
||||
const handleDiscard = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
if (onDiscard) {
|
||||
onDiscard(file.path, file.staged)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
@@ -59,18 +67,41 @@ export function GitFlatFileItem({ file, isSelected, onSelect, onStage, onUnstage
|
||||
staged
|
||||
</span>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0"
|
||||
onClick={handleAction}
|
||||
>
|
||||
{file.staged ? (
|
||||
<Minus className={cn('w-3 h-3', GIT_UI_COLORS.unstage)} />
|
||||
) : (
|
||||
<Plus className={cn('w-3 h-3', GIT_UI_COLORS.stage)} />
|
||||
)}
|
||||
</Button>
|
||||
{(file.additions !== undefined || file.deletions !== undefined) && (
|
||||
<div className="flex items-center gap-1 text-xs flex-shrink-0">
|
||||
{file.additions !== undefined && file.additions > 0 && (
|
||||
<span className="text-green-500">+{file.additions}</span>
|
||||
)}
|
||||
{file.deletions !== undefined && file.deletions > 0 && (
|
||||
<span className="text-red-500">-{file.deletions}</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{(onStage || onUnstage) && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0"
|
||||
onClick={handleAction}
|
||||
>
|
||||
{file.staged ? (
|
||||
<Minus className={cn('w-3 h-3', GIT_UI_COLORS.unstage)} />
|
||||
) : (
|
||||
<Plus className={cn('w-3 h-3', GIT_UI_COLORS.stage)} />
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
{onDiscard && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0"
|
||||
onClick={handleDiscard}
|
||||
title="Discard changes"
|
||||
>
|
||||
<RotateCcw className="w-3 h-3 text-rose-500" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -12,7 +12,11 @@ interface GitFlatFileListProps {
|
||||
onSelect: (path: string, staged: boolean) => void
|
||||
onStage?: (paths: string[]) => void
|
||||
onUnstage?: (paths: string[]) => void
|
||||
onDiscard?: (paths: string[], staged: boolean) => void
|
||||
selectedFile?: string
|
||||
readOnly?: boolean
|
||||
totalAdditions?: number
|
||||
totalDeletions?: number
|
||||
}
|
||||
|
||||
interface GroupedFiles {
|
||||
@@ -28,7 +32,11 @@ export function GitFlatFileList({
|
||||
onSelect,
|
||||
onStage,
|
||||
onUnstage,
|
||||
onDiscard,
|
||||
selectedFile,
|
||||
readOnly = false,
|
||||
totalAdditions,
|
||||
totalDeletions,
|
||||
}: GitFlatFileListProps) {
|
||||
const [collapsedGroups, setCollapsedGroups] = useState<Set<GitFileStatusType>>(new Set())
|
||||
|
||||
@@ -83,6 +91,13 @@ export function GitFlatFileList({
|
||||
}
|
||||
}
|
||||
|
||||
const handleDiscardAll = (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
if (onDiscard) {
|
||||
onDiscard(filteredFiles.map(f => f.path), staged)
|
||||
}
|
||||
}
|
||||
|
||||
if (filteredFiles.length === 0) {
|
||||
return null
|
||||
}
|
||||
@@ -93,24 +108,44 @@ export function GitFlatFileList({
|
||||
<span className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
|
||||
{staged ? 'Staged Changes' : 'Changes'} ({filteredFiles.length})
|
||||
</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
onClick={staged ? handleUnstageAll : handleStageAll}
|
||||
>
|
||||
{staged ? (
|
||||
<>
|
||||
<Minus className="w-3 h-3 mr-1" />
|
||||
Unstage All
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Plus className="w-3 h-3 mr-1" />
|
||||
Stage All
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
{(totalAdditions !== undefined && totalAdditions > 0 || totalDeletions !== undefined && totalDeletions > 0) && (
|
||||
<div className="flex items-center gap-1 text-xs">
|
||||
{totalAdditions !== undefined && totalAdditions > 0 && <span className="text-green-500">+{totalAdditions}</span>}
|
||||
{totalDeletions !== undefined && totalDeletions > 0 && <span className="text-red-500">-{totalDeletions}</span>}
|
||||
</div>
|
||||
)}
|
||||
{!readOnly && (
|
||||
<div className="flex items-center gap-1">
|
||||
{onDiscard && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs text-rose-500 hover:text-rose-600"
|
||||
onClick={handleDiscardAll}
|
||||
>
|
||||
Discard All
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
onClick={staged ? handleUnstageAll : handleStageAll}
|
||||
>
|
||||
{staged ? (
|
||||
<>
|
||||
<Minus className="w-3 h-3 mr-1" />
|
||||
Unstage All
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Plus className="w-3 h-3 mr-1" />
|
||||
Stage All
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{groupedFiles.map(({ status, files: groupFiles }) => {
|
||||
@@ -143,8 +178,9 @@ export function GitFlatFileList({
|
||||
file={file}
|
||||
isSelected={selectedFile === file.path}
|
||||
onSelect={onSelect}
|
||||
onStage={handleStageFile}
|
||||
onUnstage={handleUnstageFile}
|
||||
onStage={readOnly ? undefined : handleStageFile}
|
||||
onUnstage={readOnly ? undefined : handleUnstageFile}
|
||||
onDiscard={readOnly ? undefined : onDiscard ? ((path: string, staged: boolean) => onDiscard([path], staged)) : undefined}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useGit } from '@/hooks/useGit'
|
||||
import { ChangesTab } from './ChangesTab'
|
||||
import { CommitsTab } from './CommitsTab'
|
||||
import { BranchesTab } from './BranchesTab'
|
||||
import { CommitDetailView } from './CommitDetailView'
|
||||
import { GitErrorBanner } from './GitErrorBanner'
|
||||
import { FileDiffView } from '@/components/file-browser/FileDiffView'
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -37,6 +38,7 @@ interface SourceControlPanelProps {
|
||||
}
|
||||
|
||||
type Tab = 'changes' | 'commits' | 'branches'
|
||||
type View = 'default' | 'commit-detail'
|
||||
|
||||
export function SourceControlPanel({
|
||||
repoId,
|
||||
@@ -49,6 +51,9 @@ export function SourceControlPanel({
|
||||
}: SourceControlPanelProps) {
|
||||
const [activeTab, setActiveTab] = useState<Tab>('changes')
|
||||
const [selectedFile, setSelectedFile] = useState<{path: string, staged: boolean} | undefined>()
|
||||
const [currentView, setCurrentView] = useState<View>('default')
|
||||
const [selectedCommit, setSelectedCommit] = useState<string | undefined>()
|
||||
const [selectedCommitFile, setSelectedCommitFile] = useState<string | undefined>()
|
||||
const [gitError, setGitError] = useState<{ summary: string; detail?: string } | null>(null)
|
||||
const { data: status } = useGitStatus(repoId)
|
||||
const isMobile = useMobile()
|
||||
@@ -72,6 +77,18 @@ export function SourceControlPanel({
|
||||
}
|
||||
}
|
||||
|
||||
const handleSelectCommit = (hash: string) => {
|
||||
setSelectedCommit(hash)
|
||||
setCurrentView('commit-detail')
|
||||
}
|
||||
|
||||
const handleBackToCommits = () => {
|
||||
setSelectedCommit(undefined)
|
||||
setSelectedCommitFile(undefined)
|
||||
setCurrentView('default')
|
||||
setActiveTab('commits')
|
||||
}
|
||||
|
||||
const tabs: { id: Tab; label: string; icon: React.ElementType }[] = [
|
||||
{ id: 'changes', label: 'Changes', icon: FileCode },
|
||||
{ id: 'commits', label: 'Commits', icon: History },
|
||||
@@ -82,7 +99,7 @@ export function SourceControlPanel({
|
||||
const stagedCount = status?.files.filter(f => f.staged).length || 0
|
||||
|
||||
const content = (
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex flex-col h-full gap-0">
|
||||
<div className="flex items-center justify-between px-3 py-2 border-b border-border flex-shrink-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
@@ -154,69 +171,79 @@ export function SourceControlPanel({
|
||||
<GitErrorBanner error={gitError} onDismiss={() => setGitError(null)} />
|
||||
)}
|
||||
|
||||
<div className="flex border-b border-border flex-shrink-0">
|
||||
{tabs.map((tab) => {
|
||||
const Icon = tab.icon
|
||||
return (
|
||||
<button
|
||||
key={tab.id}
|
||||
className={cn(
|
||||
'flex items-center gap-1.5 px-3 py-2 text-sm transition-colors border-b-2 -mb-px',
|
||||
activeTab === tab.id
|
||||
? 'border-primary text-foreground'
|
||||
: 'border-transparent text-muted-foreground hover:text-foreground hover:bg-accent'
|
||||
)}
|
||||
onClick={() => setActiveTab(tab.id)}
|
||||
>
|
||||
<Icon className="w-4 h-4" />
|
||||
<span>{tab.label}</span>
|
||||
{tab.id === 'changes' && changesCount > 0 && (
|
||||
<span className="text-xs px-1.5 py-0.5 rounded-full bg-accent">
|
||||
{stagedCount > 0 ? `${stagedCount}/${changesCount}` : changesCount}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
{!((currentView === 'commit-detail' && selectedCommitFile) || (isMobile && selectedFile && activeTab === 'changes')) && (
|
||||
<div className="flex border-b border-border flex-shrink-0">
|
||||
{tabs.map((tab) => {
|
||||
const Icon = tab.icon
|
||||
return (
|
||||
<button
|
||||
key={tab.id}
|
||||
className={cn(
|
||||
'flex items-center gap-1.5 px-3 py-2 text-sm transition-colors border-b-2 -mb-px',
|
||||
activeTab === tab.id
|
||||
? 'border-primary text-foreground'
|
||||
: 'border-transparent text-muted-foreground hover:text-foreground hover:bg-accent'
|
||||
)}
|
||||
onClick={() => setActiveTab(tab.id)}
|
||||
>
|
||||
<Icon className="w-4 h-4" />
|
||||
<span>{tab.label}</span>
|
||||
{tab.id === 'changes' && changesCount > 0 && (
|
||||
<span className="text-xs px-1.5 py-0.5 rounded-full bg-accent">
|
||||
{stagedCount > 0 ? `${stagedCount}/${changesCount}` : changesCount}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className={cn('flex-1 min-h-0', isMobile ? 'flex flex-col' : 'flex')}>
|
||||
<div className={cn('flex-1 min-h-0', isMobile ? 'flex flex-col gap-0' : 'flex')}>
|
||||
<div className={cn(
|
||||
'overflow-hidden',
|
||||
isMobile ? 'flex-1' : selectedFile ? 'w-[35%] border-r border-border' : 'flex-1'
|
||||
'overflow-hidden min-h-0 h-full flex flex-col',
|
||||
isMobile
|
||||
? 'flex-1'
|
||||
: currentView === 'commit-detail'
|
||||
? 'flex-1'
|
||||
: selectedFile
|
||||
? 'w-[35%] border-r border-border'
|
||||
: 'flex-1'
|
||||
)}>
|
||||
{activeTab === 'changes' && (
|
||||
<ChangesTab
|
||||
repoId={repoId}
|
||||
onFileSelect={(path, staged) => setSelectedFile({ path, staged })}
|
||||
onClearFileSelection={() => setSelectedFile(undefined)}
|
||||
selectedFile={selectedFile}
|
||||
isMobile={isMobile}
|
||||
onError={handleGitError}
|
||||
/>
|
||||
)}
|
||||
{activeTab === 'commits' && (
|
||||
<CommitsTab repoId={repoId} />
|
||||
{activeTab === 'commits' && currentView === 'default' && (
|
||||
<CommitsTab repoId={repoId} onSelectCommit={handleSelectCommit} />
|
||||
)}
|
||||
{activeTab === 'branches' && (
|
||||
{activeTab === 'branches' && currentView === 'default' && (
|
||||
<BranchesTab repoId={repoId} currentBranch={currentBranch} repoUrl={repoUrl} isRepoWorktree={isRepoWorktree} />
|
||||
)}
|
||||
|
||||
{currentView === 'commit-detail' && selectedCommit && (
|
||||
<div className="flex flex-1 min-h-0 overflow-hidden flex-col">
|
||||
<CommitDetailView
|
||||
repoId={repoId}
|
||||
commitHash={selectedCommit}
|
||||
onBack={handleBackToCommits}
|
||||
onFileSelect={setSelectedCommitFile}
|
||||
selectedFile={selectedCommitFile}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{selectedFile && !isMobile && (
|
||||
{selectedFile && !isMobile && currentView === 'default' && (
|
||||
<div className="flex-1 overflow-hidden flex flex-col">
|
||||
<div className="flex items-center justify-between px-3 py-2 border-b border-border">
|
||||
<span className="text-sm font-medium truncate">{selectedFile.path}</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 w-6 p-0"
|
||||
onClick={() => setSelectedFile(undefined)}
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex-1 overflow-auto">
|
||||
<FileDiffView repoId={repoId} filePath={selectedFile.path} includeStaged={selectedFile.staged} />
|
||||
<FileDiffView repoId={repoId} filePath={selectedFile.path} includeStaged={selectedFile.staged} onClose={() => setSelectedFile(undefined)} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -230,12 +257,12 @@ export function SourceControlPanel({
|
||||
mobileFullscreen
|
||||
hideCloseButton={isMobile}
|
||||
className={cn(
|
||||
'p-0 flex flex-col bg-card border-border',
|
||||
isMobile ? 'h-full' : 'w-[90vw] sm:max-w-6xl h-[90vh]'
|
||||
'p-0 flex flex-col bg-card border-border gap-0',
|
||||
isMobile ? 'h-full' : 'w-[90vw] sm:max-w-6xl h-[90vh] sm:pb-0'
|
||||
)}
|
||||
>
|
||||
<DialogHeader className={cn(
|
||||
'px-4 py-3 border-b border-border flex-shrink-0',
|
||||
'px-4 py-2 border-b border-border flex-shrink-0',
|
||||
isMobile && 'relative'
|
||||
)}>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
@@ -253,7 +280,7 @@ export function SourceControlPanel({
|
||||
</Button>
|
||||
)}
|
||||
</DialogHeader>
|
||||
<div className="flex-1 overflow-hidden">
|
||||
<div className="flex-1 overflow-hidden pb-0">
|
||||
{content}
|
||||
</div>
|
||||
</DialogContent>
|
||||
|
||||
Reference in new issue
Block a user