Skip to content
TypeScript

perf(mobile): remove unused Home project sorting (#16177)

Julius Marminge6eed953a0602
diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx
index 48f074a3438..245c5509d84 100644
--- a/apps/mobile/src/features/home/HomeScreen.tsx
+++ b/apps/mobile/src/features/home/HomeScreen.tsx
@@ -67,7 +67,7 @@ import { useThreadListV2ShelfPreferences } from "../threads/use-thread-list-v2-s
import type { HomeListFilterMenuEnvironment } from "./home-list-filter-menu";
import {
buildHomeProjectScopes,
- sortHomeProjectScopes,
+ findHomeProjectScope,
type HomeProjectSortOrder,
} from "./homeThreadList";
import { createSwipeRowActivation } from "./swipe-row-activation";
@@ -386,42 +386,14 @@ export function HomeScreen(props: HomeScreenProps) {
}, [props.projects]);
const v2ProjectScopeKey = props.selectedProjectKey;
- const v2ScopeProjects = useMemo(
- () =>
- sortHomeProjectScopes({
- scopes: projectScopes,
- threads: props.threads,
- pendingTasks: props.pendingTasks,
- projectSortOrder: props.projectSortOrder,
- }),
- [
- props.pendingTasks,
- props.projects,
- props.projectSortOrder,
- props.selectedEnvironmentId,
- props.threads,
- projectScopes,
- ],
- );
const v2ScopedProjectGroup = useMemo(
- () =>
- v2ProjectScopeKey === null
- ? null
- : (v2ScopeProjects.find(
- (scope) =>
- scope.key === v2ProjectScopeKey ||
- scope.projectRefs.some(
- (projectRef) =>
- scopedProjectKey(projectRef.environmentId, projectRef.projectId) ===
- v2ProjectScopeKey,
- ),
- ) ?? null),
- [v2ProjectScopeKey, v2ScopeProjects],
+ () => findHomeProjectScope(projectScopes, v2ProjectScopeKey),
+ [v2ProjectScopeKey, projectScopes],
);
const v2ProjectTitleByProjectKey = useMemo(
() =>
new Map(
- v2ScopeProjects.flatMap((scope) =>
+ projectScopes.flatMap((scope) =>
scope.projectRefs.map(
(projectRef) =>
[
@@ -431,7 +403,7 @@ export function HomeScreen(props: HomeScreenProps) {
),
),
),
- [v2ScopeProjects],
+ [projectScopes],
);
const v2ScopedProjectKeys = useMemo(
() =>
diff --git a/apps/mobile/src/features/home/homeThreadList.test.ts b/apps/mobile/src/features/home/homeThreadList.test.ts
index 33e5f5fda2d..fc134c9d74e 100644
--- a/apps/mobile/src/features/home/homeThreadList.test.ts
+++ b/apps/mobile/src/features/home/homeThreadList.test.ts
@@ -5,7 +5,11 @@ import type {
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
-import { buildHomeProjectScopes, sortHomeProjectScopes } from "./homeThreadList";
+import {
+ buildHomeProjectScopes,
+ findHomeProjectScope,
+ sortHomeProjectScopes,
+} from "./homeThreadList";
import { makeThreadShellFixture } from "../../test-fixtures";
function makeProject(
@@ -282,4 +286,35 @@ describe("home project scopes", () => {
}),
).toHaveLength(2);
});
+
+ it("prefers an exact scope key over a project ID that collides with it", () => {
+ const environmentId = EnvironmentId.make("environment-local");
+ // project.create accepts caller-supplied IDs, so one project's scoped ref
+ // key can equal another project's normalized workspace-root scope key.
+ const collidingProject = makeProject({
+ environmentId,
+ id: ProjectId.make("/workspaces/target"),
+ title: "Colliding",
+ workspaceRoot: "/workspaces/colliding",
+ });
+ const targetProject = makeProject({
+ environmentId,
+ id: ProjectId.make("project-target"),
+ title: "Target",
+ workspaceRoot: "/workspaces/target",
+ });
+ const scopes = buildHomeProjectScopes({
+ projects: [collidingProject, targetProject],
+ environmentId: null,
+ projectGroupingMode: "separate",
+ });
+ const targetScope = scopes.find((scope) => scope.title === "Target");
+ expect(scopes.map((scope) => scope.title)).toEqual(["Colliding", "Target"]);
+ expect(targetScope?.key).toBe("environment-local:/workspaces/target");
+
+ expect(findHomeProjectScope(scopes, "environment-local:/workspaces/target")).toBe(targetScope);
+ expect(findHomeProjectScope(scopes, "environment-local:project-target")).toBe(targetScope);
+ expect(findHomeProjectScope(scopes, "environment-local:missing")).toBeNull();
+ expect(findHomeProjectScope(scopes, null)).toBeNull();
+ });
});
diff --git a/apps/mobile/src/features/home/homeThreadList.ts b/apps/mobile/src/features/home/homeThreadList.ts
index 0c85972e43e..165278857c1 100644
--- a/apps/mobile/src/features/home/homeThreadList.ts
+++ b/apps/mobile/src/features/home/homeThreadList.ts
@@ -65,6 +65,26 @@ export function buildHomeProjectScopes(input: {
});
}
+// Prefers an exact scope key so a caller-supplied project ID that equals another
+// project's normalized workspace root cannot select the wrong group.
+export function findHomeProjectScope(
+ scopes: ReadonlyArray<HomeProjectScope>,
+ key: string | null,
+): HomeProjectScope | null {
+ if (key === null) {
+ return null;
+ }
+ return (
+ scopes.find((scope) => scope.key === key) ??
+ scopes.find((scope) =>
+ scope.projectRefs.some(
+ (projectRef) => scopedProjectKey(projectRef.environmentId, projectRef.projectId) === key,
+ ),
+ ) ??
+ null
+ );
+}
+
export function sortHomeProjectScopes(input: {
readonly scopes: ReadonlyArray<HomeProjectScope>;
readonly threads: ReadonlyArray<EnvironmentThreadShell>;