Skip to content

Commit 233f5c9

Browse files
fix(mobile): home list scope updates in one event each build on the last
updateOptions built the saved scope from the render's options while the state update used a functional updater, so two setter calls in one event saved a stale scope. Options now live in a per-provider atom that updates read and write directly, so state and the saved scope come from the same snapshot. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c3a8c19 commit 233f5c9

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { EnvironmentId } from "@t3tools/contracts";
2+
import { act, useLayoutEffect } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
const harness = vi.hoisted(() => ({ saved: [] as unknown[] }));
7+
8+
vi.mock("../../state/preferences", () => ({ updateMobilePreferencesAtom: {} }));
9+
vi.mock("@effect/atom-react", async (importOriginal) => ({
10+
...(await importOriginal<typeof import("@effect/atom-react")>()),
11+
useAtomSet: () => (value: unknown) => harness.saved.push(value),
12+
}));
13+
14+
import { HomeListOptionsProvider, useHomeListOptions } from "./home-list-options";
15+
16+
const ENVIRONMENT = "env-1" as EnvironmentId;
17+
const AVAILABLE = new Set([ENVIRONMENT]);
18+
19+
it("saves the scope from every update made in one event", async () => {
20+
harness.saved = [];
21+
let controls: ReturnType<typeof useHomeListOptions> | undefined;
22+
function Probe() {
23+
const result = useHomeListOptions(AVAILABLE);
24+
useLayoutEffect(() => {
25+
controls = result;
26+
});
27+
return null;
28+
}
29+
30+
const root = createRoot(document.createElement("div"));
31+
await act(async () =>
32+
root.render(
33+
<HomeListOptionsProvider projectGroupingMode="repository">
34+
<Probe />
35+
</HomeListOptionsProvider>,
36+
),
37+
);
38+
act(() => {
39+
controls!.setSelectedEnvironmentId(ENVIRONMENT);
40+
controls!.setSelectedProjectKey("env-1:project-1");
41+
});
42+
43+
expect(harness.saved.at(-1)).toEqual({
44+
threadListFilters: { environmentId: ENVIRONMENT, projectKey: "env-1:project-1" },
45+
});
46+
expect(controls!.options.selectedEnvironmentId).toBe(ENVIRONMENT);
47+
expect(controls!.options.selectedProjectKey).toBe("env-1:project-1");
48+
act(() => root.unmount());
49+
});

‎apps/mobile/src/features/home/home-list-options.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { EnvironmentId, SidebarProjectGroupingMode } from "@t3tools/contracts";
22
import { DEFAULT_SIDEBAR_PROJECT_SORT_ORDER } from "@t3tools/contracts";
3-
import { useAtomSet } from "@effect/atom-react";
3+
import { RegistryContext, useAtomSet, useAtomValue } from "@effect/atom-react";
4+
import { Atom } from "effect/reactivity";
45
import { createContext, createElement, useContext, useState, type PropsWithChildren } from "react";
56

67
import type { ThreadListFilters } from "../../persistence/mobile-preferences";
@@ -44,15 +45,19 @@ export function HomeListOptionsProvider({
4445
readonly projectGroupingMode: SidebarProjectGroupingMode;
4546
readonly savedFilters?: ThreadListFilters | undefined;
4647
}>) {
47-
const [options, setOptions] = useState(() => defaultHomeListOptions(savedFilters));
48+
const registry = useContext(RegistryContext);
49+
const [optionsAtom] = useState(() => Atom.make(defaultHomeListOptions(savedFilters)));
50+
const options = useAtomValue(optionsAtom);
4851
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
49-
// Only the scope persists; the sort order is per session.
52+
// Only the scope persists; the sort order is per session. Updates read the
53+
// registry, so several in one event each build on the last and save it.
5054
const updateOptions = (patch: Partial<HomeListOptions>) => {
51-
const next = { ...options, ...patch };
52-
setOptions((current) => ({ ...current, ...patch }));
55+
const current = registry.get(optionsAtom);
56+
const next = { ...current, ...patch };
57+
registry.set(optionsAtom, next);
5358
if (
54-
next.selectedEnvironmentId !== options.selectedEnvironmentId ||
55-
next.selectedProjectKey !== options.selectedProjectKey
59+
next.selectedEnvironmentId !== current.selectedEnvironmentId ||
60+
next.selectedProjectKey !== current.selectedProjectKey
5661
) {
5762
savePreferences({
5863
threadListFilters: {

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const reactTests = [
2525
"src/state/queries.debounce.test.tsx",
2626
"src/state/use-selected-thread-git-actions.test.tsx",
2727
"src/features/terminal/useTerminalExitNavigation.test.tsx",
28+
"src/features/home/home-list-options.test.tsx",
2829
];
2930

3031
const sourceRoot = `${normalizePath(NodeURL.fileURLToPath(new URL("./src", import.meta.url)))}/`;

0 commit comments

Comments
 (0)