Skip to content

Commit 961f723

Browse files
committed
Auto-select first file in changed files
1 parent 5794c84 commit 961f723

File tree

9 files changed

+38
-90
lines changed

9 files changed

+38
-90
lines changed

apps/desktop/src/components/ChangedFiles.svelte

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import Resizer from '$components/Resizer.svelte';
66
import emptyFolderSvg from '$lib/assets/empty-state/empty-folder.svg?raw';
77
import { IntelligentScrollingService } from '$lib/intelligentScrolling/service';
8+
import { IdSelection } from '$lib/selection/idSelection.svelte';
89
import { inject } from '@gitbutler/shared/context';
910
import Badge from '@gitbutler/ui/Badge.svelte';
1011
import EmptyStatePlaceholder from '@gitbutler/ui/EmptyStatePlaceholder.svelte';
12+
import { untrack, type ComponentProps } from 'svelte';
1113
import type { ConflictEntriesObj } from '$lib/files/conflicts';
1214
import type { TreeChange } from '$lib/hunks/change';
1315
import type { SelectionId } from '$lib/selection/key';
14-
import type { ComponentProps } from 'svelte';
1516
1617
type Props = {
1718
projectId: string;
@@ -25,6 +26,7 @@
2526
grow?: boolean;
2627
noshrink?: boolean;
2728
resizer?: Partial<ComponentProps<typeof Resizer>>;
29+
autoselect?: boolean;
2830
ontoggle?: (collapsed: boolean) => void;
2931
};
3032
@@ -39,13 +41,26 @@
3941
draggableFiles,
4042
grow,
4143
noshrink,
42-
ontoggle,
43-
resizer
44+
resizer,
45+
autoselect,
46+
ontoggle
4447
}: Props = $props();
4548
46-
const [intelligentScrollingService] = inject(IntelligentScrollingService);
49+
const [idSelection, intelligentScrollingService] = inject(
50+
IdSelection,
51+
IntelligentScrollingService
52+
);
4753
4854
let listMode: 'list' | 'tree' = $state('tree');
55+
56+
$effect(() => {
57+
if (autoselect && selectionId) {
58+
// Prevent effect from running when `changes` updates.
59+
untrack(() => {
60+
idSelection.set(changes[0]!.path, selectionId, 0);
61+
});
62+
}
63+
});
4964
</script>
5065

5166
<Drawer {grow} {ontoggle} {resizer} {noshrink}>

apps/desktop/src/components/FileList.svelte

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { AIService } from '$lib/ai/service';
88
import { projectAiGenEnabled } from '$lib/config/config';
99
import { conflictEntryHint } from '$lib/conflictEntryPresence';
10-
import { abbreviateFolders, changesToFileTree, sortLikeFileTree } from '$lib/files/filetreeV3';
10+
import { abbreviateFolders, changesToFileTree } from '$lib/files/filetreeV3';
1111
import { type TreeChange, type Modification } from '$lib/hunks/change';
1212
import { showToast } from '$lib/notifications/toasts';
1313
import { IdSelection } from '$lib/selection/idSelection.svelte';
@@ -28,6 +28,7 @@
2828
active?: boolean;
2929
conflictEntries?: ConflictEntriesObj;
3030
draggableFiles?: boolean;
31+
allowUnselect?: boolean;
3132
onselect?: () => void;
3233
};
3334
@@ -41,6 +42,7 @@
4142
stackId,
4243
conflictEntries,
4344
draggableFiles,
45+
allowUnselect,
4446
onselect
4547
}: Props = $props();
4648
@@ -50,8 +52,7 @@
5052
const [branchChanges] = actionService.branchChanges;
5153
let currentDisplayIndex = $state(0);
5254
53-
const sortedChanges = $derived(sortLikeFileTree(changes));
54-
const fileChunks: TreeChange[][] = $derived(chunk(sortedChanges, 100));
55+
const fileChunks: TreeChange[][] = $derived(chunk(changes, 100));
5556
const visibleFiles: TreeChange[] = $derived(fileChunks.slice(0, currentDisplayIndex + 1).flat());
5657
let aiConfigurationValid = $state(false);
5758
@@ -145,7 +146,7 @@
145146
shiftKey: e.shiftKey,
146147
key: e.key,
147148
targetElement: e.currentTarget as HTMLElement,
148-
files: sortedChanges,
149+
files: changes,
149150
selectedFileIds: idSelection.values(selectionId),
150151
fileIdSelection: idSelection,
151152
selectionId: selectionId,
@@ -185,7 +186,7 @@
185186
isLast={idx === visibleFiles.length - 1}
186187
selected={idSelection.has(change.path, selectionId)}
187188
onclick={(e) => {
188-
selectFilesInList(e, change, sortedChanges, idSelection, true, idx, selectionId);
189+
selectFilesInList(e, change, changes, idSelection, true, idx, selectionId, allowUnselect);
189190
onselect?.();
190191
}}
191192
{conflictEntries}
@@ -207,15 +208,8 @@
207208
{#if listMode === 'tree'}
208209
<!-- We need to use sortedChanges here because otherwise we will end up
209210
with incorrect indexes -->
210-
{@const node = abbreviateFolders(changesToFileTree(sortedChanges))}
211-
<FileTreeNode
212-
isRoot
213-
{stackId}
214-
{node}
215-
{showCheckboxes}
216-
changes={sortedChanges}
217-
{fileTemplate}
218-
/>
211+
{@const node = abbreviateFolders(changesToFileTree(changes))}
212+
<FileTreeNode isRoot {stackId} {node} {showCheckboxes} {changes} {fileTemplate} />
219213
{:else}
220214
<LazyloadContainer
221215
minTriggerCount={80}

apps/desktop/src/components/StackView.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
order: 1,
343343
unsetMaxHeight: previewKey ? unsetMaxHeight : undefined
344344
}}
345+
autoselect
345346
/>
346347
{/snippet}
347348
</ReduxResult>

apps/desktop/src/components/UnappliedBranchView.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
projectId={env.projectId}
117117
stackId={env.stackId}
118118
active
119+
autoselect
119120
{selectionId}
120121
{changes}
121122
/>

apps/desktop/src/components/UnappliedCommitView.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<ChangedFiles
6767
title="Changed files"
6868
active
69+
autoselect
6970
{projectId}
7071
selectionId={{ type: 'commit', commitId }}
7172
changes={changes.changes}

apps/desktop/src/components/WorktreeChanges.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<div data-testid={TestId.UncommittedChanges_FileList} class="uncommitted-changes">
109109
<FileList
110110
draggableFiles
111+
allowUnselect
111112
selectionId={{ type: 'worktree', stackId }}
112113
showCheckboxes={isCommitting}
113114
changes={changes.current}

apps/desktop/src/lib/selection/idSelectionUtils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export function selectFilesInList(
194194
idSelection: IdSelection,
195195
allowMultiple: boolean,
196196
index: number,
197-
selectionId: SelectionId
197+
selectionId: SelectionId,
198+
allowUnselect?: boolean
198199
) {
199200
// e.stopPropagation();
200201
const isAlreadySelected = idSelection.has(change.path, selectionId);
@@ -216,7 +217,9 @@ export function selectFilesInList(
216217
} else {
217218
// if only one file is selected and it is already selected, unselect it
218219
if (isTheOnlyOneSelected) {
219-
idSelection.clear(selectionId);
220+
if (allowUnselect) {
221+
idSelection.clear(selectionId);
222+
}
220223
} else {
221224
idSelection.set(change.path, selectionId, index);
222225
}

apps/desktop/src/lib/stacks/stackService.svelte.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StackOrder } from '$lib/branches/branch';
22
import { ConflictEntries, type ConflictEntriesObj } from '$lib/files/conflicts';
3+
import { sortLikeFileTree } from '$lib/files/filetreeV3';
34
import { showToast } from '$lib/notifications/toasts';
45
import { hasTauriExtra } from '$lib/state/backendQuery';
56
import { ClientState, type BackendApi } from '$lib/state/clientState.svelte';
@@ -452,7 +453,7 @@ export class StackService {
452453
{ projectId, commitId },
453454
{
454455
transform: (result) => ({
455-
changes: changesSelectors.selectAll(result.changes),
456+
changes: sortLikeFileTree(changesSelectors.selectAll(result.changes)),
456457
conflictEntries: result.conflictEntries
457458
})
458459
}

apps/desktop/src/lib/utils/selectFilesInList.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)