Skip to content

Commit fbb9943

Browse files
fix(mobile): prefer exact Home project scope matches
1 parent cb8b899 commit fbb9943

3 files changed

Lines changed: 62 additions & 14 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ import {
5959
} from "../threads/threadListV2";
6060
import { useThreadListV2ShelfPreferences } from "../threads/use-thread-list-v2-shelf-preferences";
6161
import type { HomeListFilterMenuEnvironment } from "./home-list-filter-menu";
62-
import { buildHomeProjectScopes, type HomeProjectSortOrder } from "./homeThreadList";
62+
import {
63+
buildHomeProjectScopes,
64+
findHomeProjectScope,
65+
type HomeProjectSortOrder,
66+
} from "./homeThreadList";
6367
import { createSwipeRowActivation } from "./swipe-row-activation";
6468
import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "./thread-swipe-actions";
6569
import { useMaterialFabScroll } from "./MaterialFabScrollContext";
@@ -334,18 +338,7 @@ export function HomeScreen(props: HomeScreenProps) {
334338

335339
const v2ProjectScopeKey = props.selectedProjectKey;
336340
const v2ScopedProjectGroup = useMemo(
337-
() =>
338-
v2ProjectScopeKey === null
339-
? null
340-
: (projectScopes.find(
341-
(scope) =>
342-
scope.key === v2ProjectScopeKey ||
343-
scope.projectRefs.some(
344-
(projectRef) =>
345-
scopedProjectKey(projectRef.environmentId, projectRef.projectId) ===
346-
v2ProjectScopeKey,
347-
),
348-
) ?? null),
341+
() => findHomeProjectScope(projectScopes, v2ProjectScopeKey),
349342
[v2ProjectScopeKey, projectScopes],
350343
);
351344
const v2ProjectTitleByProjectKey = useMemo(

‎apps/mobile/src/features/home/homeThreadList.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import type {
55
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
66
import { describe, expect, it } from "vite-plus/test";
77

8-
import { buildHomeProjectScopes, sortHomeProjectScopes } from "./homeThreadList";
8+
import {
9+
buildHomeProjectScopes,
10+
findHomeProjectScope,
11+
sortHomeProjectScopes,
12+
} from "./homeThreadList";
913
import { makeThreadShellFixture } from "../../test-fixtures";
1014

1115
function makeProject(
@@ -282,4 +286,35 @@ describe("home project scopes", () => {
282286
}),
283287
).toHaveLength(2);
284288
});
289+
290+
it("prefers an exact scope key over a project ID that collides with it", () => {
291+
const environmentId = EnvironmentId.make("environment-local");
292+
// project.create accepts caller-supplied IDs, so one project's scoped ref
293+
// key can equal another project's normalized workspace-root scope key.
294+
const collidingProject = makeProject({
295+
environmentId,
296+
id: ProjectId.make("/workspaces/target"),
297+
title: "Colliding",
298+
workspaceRoot: "/workspaces/colliding",
299+
});
300+
const targetProject = makeProject({
301+
environmentId,
302+
id: ProjectId.make("project-target"),
303+
title: "Target",
304+
workspaceRoot: "/workspaces/target",
305+
});
306+
const scopes = buildHomeProjectScopes({
307+
projects: [collidingProject, targetProject],
308+
environmentId: null,
309+
projectGroupingMode: "separate",
310+
});
311+
const targetScope = scopes.find((scope) => scope.title === "Target");
312+
expect(scopes.map((scope) => scope.title)).toEqual(["Colliding", "Target"]);
313+
expect(targetScope?.key).toBe("environment-local:/workspaces/target");
314+
315+
expect(findHomeProjectScope(scopes, "environment-local:/workspaces/target")).toBe(targetScope);
316+
expect(findHomeProjectScope(scopes, "environment-local:project-target")).toBe(targetScope);
317+
expect(findHomeProjectScope(scopes, "environment-local:missing")).toBeNull();
318+
expect(findHomeProjectScope(scopes, null)).toBeNull();
319+
});
285320
});

‎apps/mobile/src/features/home/homeThreadList.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ export function buildHomeProjectScopes(input: {
6565
});
6666
}
6767

68+
// Prefers an exact scope key so a caller-supplied project ID that equals another
69+
// project's normalized workspace root cannot select the wrong group.
70+
export function findHomeProjectScope(
71+
scopes: ReadonlyArray<HomeProjectScope>,
72+
key: string | null,
73+
): HomeProjectScope | null {
74+
if (key === null) {
75+
return null;
76+
}
77+
return (
78+
scopes.find((scope) => scope.key === key) ??
79+
scopes.find((scope) =>
80+
scope.projectRefs.some(
81+
(projectRef) => scopedProjectKey(projectRef.environmentId, projectRef.projectId) === key,
82+
),
83+
) ??
84+
null
85+
);
86+
}
87+
6888
export function sortHomeProjectScopes(input: {
6989
readonly scopes: ReadonlyArray<HomeProjectScope>;
7090
readonly threads: ReadonlyArray<EnvironmentThreadShell>;

0 commit comments

Comments
 (0)