Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/main/frontend/app/components/git/git-changes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ function FileSection({
const dirPath = file.includes('/') ? file.slice(0, file.lastIndexOf('/')) : ''

const hunkState = fileHunkStates[file]
let checkboxChecked = false
let checkboxIndeterminate = false
const totalHunks = hunkState?.totalHunks ?? 0
const selectedCount = hunkState?.selectedHunks.size ?? 0
const allHunksSelected = totalHunks > 0 && selectedCount === totalHunks
const someHunksSelected = totalHunks > 0 && selectedCount > 0
const fileSelectedWithNoHunks = totalHunks === 0 && (hunkState?.selected ?? false)

if (hunkState && hunkState.totalHunks > 0) {
const selectedCount = hunkState.selectedHunks.size
if (selectedCount === hunkState.totalHunks) {
checkboxChecked = true
} else if (selectedCount > 0) {
checkboxIndeterminate = true
}
}
const checkboxChecked = allHunksSelected || fileSelectedWithNoHunks
const checkboxIndeterminate = someHunksSelected && !allHunksSelected

return (
<div
Expand Down
31 changes: 26 additions & 5 deletions src/main/frontend/app/components/git/git-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,34 @@ export default function GitPanel({ projectName, hasStoredToken }: GitPanelProps)
const handleToggleFile = useCallback(
async (filePath: string) => {
const hunkState = useGitStore.getState().fileHunkStates[filePath]
if (!hunkState || hunkState.totalHunks === 0) {
if (!hunkState) {
await handleSelectFile(filePath)
}

const updatedState = useGitStore.getState().fileHunkStates[filePath]

if (!updatedState) {
initFileHunks(filePath, 0)
selectAllFileHunks(filePath)
return
}

if (updatedState.totalHunks === 0) {
if (updatedState.selected) {
clearFileHunks(filePath)
} else {
selectAllFileHunks(filePath)
}
return
}

if (updatedState.selectedHunks.size === updatedState.totalHunks) {
clearFileHunks(filePath)
} else {
selectAllFileHunks(filePath)
}
},
[handleSelectFile, clearFileHunks, selectAllFileHunks],
[handleSelectFile, initFileHunks, clearFileHunks, selectAllFileHunks],
)

const handleCommit = useCallback(async () => {
Expand All @@ -113,8 +129,11 @@ export default function GitPanel({ projectName, hasStoredToken }: GitPanelProps)
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
const isZeroHunkSelected = hunkState.totalHunks === 0 && hunkState.selected

if (hunkState.selectedHunks.size === 0 && !isZeroHunkSelected) continue

await (isZeroHunkSelected || hunkState.selectedHunks.size === hunkState.totalHunks
? stageFile(projectName, filePath)
: stageHunks(projectName, filePath, [...hunkState.selectedHunks]))
}
Expand Down Expand Up @@ -175,7 +194,9 @@ export default function GitPanel({ projectName, hasStoredToken }: GitPanelProps)
}
}, [projectName, token, refreshStatus])

const hasSelectedChunks = Object.values(fileHunkStates).some((s) => s.selectedHunks.size > 0)
const hasSelectedChunks = Object.values(fileHunkStates).some(
(state) => state.selectedHunks.size > 0 || (state.totalHunks === 0 && state.selected),
)

return (
<div className="flex h-full flex-col overflow-hidden">
Expand Down
6 changes: 3 additions & 3 deletions src/main/frontend/app/stores/git-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const useGitStore = create<GitStoreState>((set, get) => ({
set((state) => ({
fileHunkStates: {
...state.fileHunkStates,
[file]: { selectedHunks: new Set(), totalHunks },
[file]: { selectedHunks: new Set(), totalHunks, selected: false },
},
}))
},
Expand Down Expand Up @@ -73,7 +73,7 @@ export const useGitStore = create<GitStoreState>((set, get) => ({
set((s) => ({
fileHunkStates: {
...s.fileHunkStates,
[file]: { ...state, selectedHunks: all },
[file]: { ...state, selectedHunks: all, selected: true },
},
}))
},
Expand All @@ -84,7 +84,7 @@ export const useGitStore = create<GitStoreState>((set, get) => ({
set((s) => ({
fileHunkStates: {
...s.fileHunkStates,
[file]: { ...state, selectedHunks: new Set() },
[file]: { ...state, selectedHunks: new Set(), selected: false },
},
}))
},
Expand Down
1 change: 1 addition & 0 deletions src/main/frontend/app/types/git.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface GitPullResult {
export interface FileHunkState {
selectedHunks: Set<number>
totalHunks: number
selected: boolean
}
Loading