-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-panel.tsx
More file actions
202 lines (185 loc) · 5.86 KB
/
Copy pathgit-panel.tsx
File metadata and controls
202 lines (185 loc) · 5.86 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
196
197
198
199
200
201
202
import { useCallback, useEffect } from 'react'
import { useGitStore } from '~/stores/git-store'
import {
fetchGitStatus,
fetchFileDiff,
stageFile,
stageHunks,
commitChanges,
pushChanges,
pullChanges,
refreshOpenDiffs,
} from '~/services/git-service'
import { showInfoToast, showSuccessToast, showErrorToast } from '~/components/toast'
import useEditorTabStore from '~/stores/editor-tab-store'
import { logApiError } from '~/utils/logger'
import GitToolbar from './git-toolbar'
import GitChanges from './git-changes'
import GitCommitBox from './git-commit-box'
interface GitPanelProps {
projectName: string
hasStoredToken: boolean
}
export default function GitPanel({ projectName, hasStoredToken }: GitPanelProps) {
const {
status,
selectedFile,
fileHunkStates,
commitMessage,
isLoading,
token,
setStatus,
setSelectedFile,
setFileDiff,
initFileHunks,
selectAllFileHunks,
clearFileHunks,
setCommitMessage,
setIsLoading,
setToken,
} = useGitStore()
const refreshStatus = useCallback(async () => {
try {
const newStatus = await fetchGitStatus(projectName)
setStatus(newStatus)
} catch (error) {
logApiError('Failed to fetch git status', error as Error)
}
}, [projectName, setStatus])
useEffect(() => {
refreshStatus()
}, [refreshStatus])
const handleSelectFile = useCallback(
async (file: string) => {
setSelectedFile(file)
try {
const diff = await fetchFileDiff(projectName, file)
setFileDiff(diff)
initFileHunks(file, diff.hunks.length)
const tabId = `diff:${file}`
const fileName = file.split('/').pop() || file
const editorTabStore = useEditorTabStore.getState()
editorTabStore.setTabData(tabId, {
name: `${fileName} (diff)`,
configurationPath: file,
type: 'diff',
diffData: {
oldContent: diff.oldContent,
newContent: diff.newContent,
filePath: diff.filePath,
hunks: diff.hunks,
},
})
editorTabStore.setActiveTab(tabId)
} catch (error) {
logApiError('Failed to load diff', error as Error)
}
},
[projectName, setSelectedFile, setFileDiff, initFileHunks],
)
const handleToggleFile = useCallback(
async (filePath: string) => {
const hunkState = useGitStore.getState().fileHunkStates[filePath]
if (!hunkState || hunkState.totalHunks === 0) {
await handleSelectFile(filePath)
}
const updatedState = useGitStore.getState().fileHunkStates[filePath]
if (updatedState.selectedHunks.size === updatedState.totalHunks) {
clearFileHunks(filePath)
} else {
selectAllFileHunks(filePath)
}
},
[handleSelectFile, clearFileHunks, selectAllFileHunks],
)
const handleCommit = useCallback(async () => {
if (!commitMessage.trim()) return
setIsLoading(true)
try {
const allHunkStates = useGitStore.getState().fileHunkStates
for (const [filePath, hunkState] of Object.entries(allHunkStates)) {
if (hunkState.selectedHunks.size === 0) continue
await (hunkState.selectedHunks.size === hunkState.totalHunks
? stageFile(projectName, filePath)
: stageHunks(projectName, filePath, [...hunkState.selectedHunks]))
}
const result = await commitChanges(projectName, commitMessage)
setCommitMessage('')
for (const filePath of Object.keys(allHunkStates)) {
clearFileHunks(filePath)
}
await refreshStatus()
await refreshOpenDiffs(projectName)
showSuccessToast(`Committed: ${result.commitId.slice(0, 7)}`)
} catch (error) {
logApiError('Failed to commit', error as Error)
} finally {
setIsLoading(false)
}
}, [projectName, commitMessage, setIsLoading, setCommitMessage, refreshStatus, clearFileHunks])
const handlePush = useCallback(async () => {
if ((status?.ahead ?? 0) === 0) {
showInfoToast('Nothing to push')
return
}
try {
const result = await pushChanges(projectName, token || undefined)
if (result.success) {
showSuccessToast(result.message)
} else {
showErrorToast(result.message)
}
await refreshStatus()
} catch (error) {
logApiError('Failed to push', error as Error)
}
}, [projectName, token, refreshStatus, status?.ahead])
const handlePull = useCallback(async () => {
try {
const result = await pullChanges(projectName, token || undefined)
if (result.success) {
const isUpToDate = result.message?.toLowerCase().includes('up to date')
if (isUpToDate) {
showInfoToast(result.message)
} else {
showSuccessToast(result.message)
useEditorTabStore.getState().refreshAllTabs()
}
} else {
showErrorToast(result.message)
}
await refreshStatus()
await refreshOpenDiffs(projectName)
} catch (error) {
logApiError('Failed to pull', error as Error)
}
}, [projectName, token, refreshStatus])
const hasSelectedChunks = Object.values(fileHunkStates).some((s) => s.selectedHunks.size > 0)
return (
<div className="flex h-full flex-col overflow-hidden">
<GitToolbar
status={status}
onRefresh={refreshStatus}
onPush={handlePush}
onPull={handlePull}
token={token}
onTokenChange={setToken}
hasStoredToken={hasStoredToken}
/>
<GitChanges
status={status}
selectedFile={selectedFile}
fileHunkStates={fileHunkStates}
onSelectFile={handleSelectFile}
onToggleFile={handleToggleFile}
/>
<GitCommitBox
commitMessage={commitMessage}
onMessageChange={setCommitMessage}
onCommit={handleCommit}
hasSelectedChunks={hasSelectedChunks}
isLoading={isLoading}
/>
</div>
)
}