forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitStatusSection.tsx
More file actions
195 lines (179 loc) · 6.52 KB
/
GitStatusSection.tsx
File metadata and controls
195 lines (179 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use client";
import { useState, useCallback } from "react";
import { GitBranch, GitCommit, CloudArrowUp, ArrowUp, ArrowLeft, Circle } from "@/components/ui/icon";
import { Button } from "@/components/ui/button";
import { useTranslation } from "@/hooks/useTranslation";
import { usePanel } from "@/hooks/usePanel";
import { showToast } from "@/hooks/useToast";
import { CommitDialog } from "./CommitDialog";
import type { GitStatus, GitChangedFile } from "@/types";
interface GitStatusSectionProps {
status: GitStatus;
}
export function GitStatusSection({ status }: GitStatusSectionProps) {
const { t } = useTranslation();
const { workingDirectory } = usePanel();
const [commitDialogOpen, setCommitDialogOpen] = useState(false);
const [pushing, setPushing] = useState(false);
const handlePush = useCallback(async () => {
if (!workingDirectory || pushing) return;
setPushing(true);
try {
const res = await fetch('/api/git/push', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cwd: workingDirectory }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({ error: 'Push failed' }));
showToast({ type: 'error', message: data.error || 'Push failed' });
return;
}
showToast({ type: 'success', message: t('git.pushSuccess') });
window.dispatchEvent(new CustomEvent('git-refresh'));
} catch (err) {
showToast({ type: 'error', message: err instanceof Error ? err.message : 'Push failed' });
} finally {
setPushing(false);
}
}, [workingDirectory, pushing, t]);
const handleCommitSuccess = useCallback(() => {
window.dispatchEvent(new CustomEvent('git-refresh'));
}, []);
if (!status.isRepo) {
return (
<div className="px-3 py-4 text-sm text-muted-foreground text-center">
{t('git.notARepo')}
</div>
);
}
return (
<div className="space-y-3">
{/* Branch + upstream */}
<div className="flex items-center gap-2 px-3">
<GitBranch size={14} className="text-muted-foreground shrink-0" />
<span className="text-sm font-medium truncate">{status.branch || t('git.noBranch')}</span>
{status.upstream && (
<span className="text-[11px] text-muted-foreground truncate">
→ {status.upstream}
</span>
)}
</div>
{/* Ahead / behind */}
{(status.ahead > 0 || status.behind > 0) && (
<div className="flex items-center gap-3 px-3">
{status.ahead > 0 && (
<span className="flex items-center gap-1 text-[11px] text-green-600 dark:text-green-400">
<ArrowUp size={12} />
{t('git.ahead', { count: String(status.ahead) })}
</span>
)}
{status.behind > 0 && (
<span className="flex items-center gap-1 text-[11px] text-orange-600 dark:text-orange-400">
<ArrowLeft size={12} />
{t('git.behind', { count: String(status.behind) })}
</span>
)}
</div>
)}
{/* Changed files — show tracked changes first, untracked separately */}
{(() => {
const tracked = status.changedFiles.filter(f => f.status !== 'untracked');
const untracked = status.changedFiles.filter(f => f.status === 'untracked');
if (tracked.length === 0 && untracked.length === 0) {
return (
<div className="px-3 py-2 text-xs text-muted-foreground">
{t('git.allCommitted')}
</div>
);
}
return (
<div className="space-y-2">
{tracked.length > 0 && (
<div className="space-y-1">
<div className="px-3 text-[11px] font-semibold uppercase tracking-wider text-muted-foreground">
{t('git.dirty', { count: String(tracked.length) })}
</div>
<div className="max-h-[200px] overflow-y-auto">
{tracked.map((file, i) => (
<FileChangeItem key={`${file.path}-${file.staged}-${i}`} file={file} />
))}
</div>
</div>
)}
{untracked.length > 0 && (
<div className="space-y-1">
<div className="px-3 text-[11px] font-semibold uppercase tracking-wider text-muted-foreground">
{t('git.untracked', { count: String(untracked.length) })}
</div>
<div className="max-h-[120px] overflow-y-auto">
{untracked.map((file, i) => (
<FileChangeItem key={`${file.path}-untracked-${i}`} file={file} />
))}
</div>
</div>
)}
</div>
);
})()}
{/* Commit & Push actions */}
<div className="flex items-center gap-2 px-3 pt-2">
<Button
size="sm"
variant="outline"
className="h-7 text-xs gap-1.5 flex-1"
onClick={() => setCommitDialogOpen(true)}
disabled={!status.dirty}
>
<GitCommit size={14} />
{t('topBar.commit')}
</Button>
<Button
size="sm"
variant="outline"
className="h-7 text-xs gap-1.5 flex-1"
onClick={handlePush}
disabled={pushing}
>
<CloudArrowUp size={14} />
{pushing ? t('git.loading') : t('topBar.push')}
</Button>
</div>
<CommitDialog
cwd={workingDirectory}
open={commitDialogOpen}
onClose={() => setCommitDialogOpen(false)}
onSuccess={handleCommitSuccess}
/>
</div>
);
}
function FileChangeItem({ file }: { file: GitChangedFile }) {
const statusColors: Record<string, string> = {
modified: 'text-amber-500',
added: 'text-green-500',
deleted: 'text-red-500',
renamed: 'text-blue-500',
copied: 'text-blue-500',
untracked: 'text-muted-foreground',
};
const statusLetters: Record<string, string> = {
modified: 'M',
added: 'A',
deleted: 'D',
renamed: 'R',
copied: 'C',
untracked: '?',
};
return (
<div className="flex items-center gap-2 px-3 py-0.5 text-[12px] hover:bg-muted/50">
<span className={`shrink-0 font-mono ${statusColors[file.status] || 'text-muted-foreground'}`}>
{statusLetters[file.status] || '?'}
</span>
{file.staged && (
<Circle size={6} weight="fill" className="text-green-500 shrink-0" />
)}
<span className="truncate text-foreground/80">{file.path}</span>
</div>
);
}