import { useState } from 'react' import { FetchError } from '@opencode-manager/shared' import { useGitStatus, getApiErrorMessage } from '@/api/git' 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' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { GIT_UI_COLORS } from '@/lib/git-status-styles' import { Loader2, GitBranch, FileCode, History, Upload, ArrowUp, ArrowDown, RefreshCw, ArrowDownFromLine, X, } from 'lucide-react' import { cn } from '@/lib/utils' import { useMobile } from '@/hooks/useMobile' interface SourceControlPanelProps { repoId: number isOpen: boolean onClose: () => void currentBranch: string repoUrl?: string | null isRepoWorktree?: boolean repoName?: string } type Tab = 'changes' | 'commits' | 'branches' type View = 'default' | 'commit-detail' export function SourceControlPanel({ repoId, isOpen, onClose, currentBranch, repoUrl, isRepoWorktree, repoName, }: SourceControlPanelProps) { const [activeTab, setActiveTab] = useState('changes') const [selectedFile, setSelectedFile] = useState<{path: string, staged: boolean} | undefined>() const [currentView, setCurrentView] = useState('default') const [selectedCommit, setSelectedCommit] = useState() const [selectedCommitFile, setSelectedCommitFile] = useState() const [gitError, setGitError] = useState<{ summary: string; detail?: string } | null>(null) const { data: status } = useGitStatus(repoId) const isMobile = useMobile() const handleGitError = (error: unknown) => { if (error instanceof FetchError) { setGitError({ summary: getApiErrorMessage(error), detail: error.detail }) } else { setGitError({ summary: getApiErrorMessage(error) }) } } const git = useGit(repoId, handleGitError) const handleGitAction = async (action: () => Promise) => { try { setGitError(null) await action() } catch { // error already handled by useGit's onError -> handleGitError } } 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 }, { id: 'branches', label: 'Branches', icon: GitBranch }, ] const changesCount = status?.files.length || 0 const stagedCount = status?.files.filter(f => f.staged).length || 0 const content = (
{currentBranch}
{status && (status.ahead > 0 || status.behind > 0) && (
{status.ahead > 0 && ( {status.ahead} )} {status.behind > 0 && ( {status.behind} )}
)}
{gitError && ( setGitError(null)} /> )} {!((currentView === 'commit-detail' && selectedCommitFile) || (isMobile && selectedFile && activeTab === 'changes')) && (
{tabs.map((tab) => { const Icon = tab.icon return ( ) })}
)}
{activeTab === 'changes' && ( setSelectedFile({ path, staged })} onClearFileSelection={() => setSelectedFile(undefined)} selectedFile={selectedFile} isMobile={isMobile} onError={handleGitError} /> )} {activeTab === 'commits' && currentView === 'default' && ( )} {activeTab === 'branches' && currentView === 'default' && ( )} {currentView === 'commit-detail' && selectedCommit && (
)}
{selectedFile && !isMobile && currentView === 'default' && (
setSelectedFile(undefined)} />
)}
) return ( !open && onClose()}> {isMobile && repoName ? repoName : 'Source Control'} {isMobile && ( )}
{content}
) }