Skip to content

Commit 636980a

Browse files
committed
Infer default from outline
1 parent 9355acd commit 636980a

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

apps/lite/ui/src/routes/project/$id/-state/selection.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getCommonBaseCommitId } from "#ui/domain/RefInfo.ts";
22
import { type RefInfo, type WorktreeChanges } from "@gitbutler/but-sdk";
33
import { Match } from "effect";
4-
import { changesSectionItem, type Item } from "../workspace/-Item.ts";
4+
import { type Item } from "../workspace/-Item.ts";
55

66
export type WorkspaceSelectionState = {
77
item: Item | null;
@@ -86,16 +86,18 @@ const normalizeSelectedItem = (
8686

8787
export const resolveSelectedWorkspaceItem = ({
8888
workspaceSelection,
89-
headInfo,
9089
worktreeChanges,
90+
headInfo,
91+
defaultItem,
9192
}: {
9293
workspaceSelection: WorkspaceSelectionState;
93-
headInfo: RefInfo;
9494
worktreeChanges: WorktreeChanges;
95+
headInfo: RefInfo;
96+
defaultItem: Item;
9597
}): Item =>
9698
(workspaceSelection.item
9799
? normalizeSelectedItem(workspaceSelection.item, headInfo, worktreeChanges)
98-
: null) ?? changesSectionItem(null);
100+
: null) ?? defaultItem;
99101

100102
export const normalizeSelectedFile = ({
101103
paths,

apps/lite/ui/src/routes/project/$id/workspace/-WorkspaceOutline.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Segment, type HunkAssignment, type RefInfo, type TreeChange } from "@gitbutler/but-sdk";
2+
import { type NonEmptyArray } from "effect/Array";
23
import {
34
baseCommitItem,
45
changeItem,
@@ -28,7 +29,7 @@ type WorkspaceSection = {
2829
items: Array<Item>;
2930
};
3031

31-
type WorkspaceOutline = Array<WorkspaceSection>;
32+
type WorkspaceOutline = NonEmptyArray<WorkspaceSection>;
3233

3334
type BuildWorkspaceOutlineArgs = {
3435
headInfo: RefInfo;
@@ -37,7 +38,7 @@ type BuildWorkspaceOutlineArgs = {
3738
commonBaseCommitId?: string;
3839
};
3940

40-
const buildWorkspaceOutline = ({
41+
export const buildWorkspaceOutline = ({
4142
headInfo,
4243
changes,
4344
assignments,
@@ -96,7 +97,7 @@ export type NavigationIndex = {
9697
indexByKey: Map<string, number>;
9798
};
9899

99-
const fromOutline = (outline: WorkspaceOutline): NavigationIndex => {
100+
export const buildNavigationIndex = (outline: WorkspaceOutline): NavigationIndex => {
100101
const model: NavigationIndex = {
101102
items: [],
102103
sectionStartIndexes: [],
@@ -121,9 +122,6 @@ const fromOutline = (outline: WorkspaceOutline): NavigationIndex => {
121122
return model;
122123
};
123124

124-
export const buildNavigationIndex = (args: BuildWorkspaceOutlineArgs): NavigationIndex =>
125-
fromOutline(buildWorkspaceOutline(args));
126-
127125
export const getAdjacentItem = (
128126
index: NavigationIndex,
129127
selection: Item | null,

apps/lite/ui/src/routes/project/$id/workspace/route.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ import {
111111
type SegmentMode,
112112
segmentItem,
113113
} from "./-Item.ts";
114-
import { buildNavigationIndex } from "./-WorkspaceOutline.ts";
114+
import { buildNavigationIndex, buildWorkspaceOutline } from "./-WorkspaceOutline.ts";
115115
import {
116116
renameBranchBindings,
117117
handleRenameBranchKeyDown,
@@ -1905,17 +1905,19 @@ const ProjectPage: FC = () => {
19051905
const { data: worktreeChanges } = useSuspenseQuery(changesInWorktreeQueryOptions(projectId));
19061906

19071907
const commonBaseCommitId = getCommonBaseCommitId(headInfo);
1908-
const navigationIndex = buildNavigationIndex({
1908+
const workspaceOutline = buildWorkspaceOutline({
19091909
headInfo,
19101910
changes: worktreeChanges.changes,
19111911
assignments: worktreeChanges.assignments,
19121912
commonBaseCommitId,
19131913
});
1914+
const navigationIndex = buildNavigationIndex(workspaceOutline);
19141915

19151916
const selectedItem = resolveSelectedWorkspaceItem({
19161917
workspaceSelection,
19171918
headInfo,
19181919
worktreeChanges,
1920+
defaultItem: workspaceOutline[0].section,
19191921
});
19201922
const selectItem = (nextSelectedItem: Item | null) => {
19211923
dispatchProjectState({ _tag: "SelectItem", item: nextSelectedItem });

0 commit comments

Comments
 (0)