Skip to content

Commit c5332bf

Browse files
fix(mobile): stabilize composer search debounce targets (#24272)
1 parent d0b9e9d commit c5332bf

3 files changed

Lines changed: 112 additions & 6 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { AuthFilesystemReadScope, EnvironmentId } from "@t3tools/contracts";
2+
import { act } from "react";
3+
import { createRoot } from "react-dom/client";
4+
import { expect, it, vi } from "vite-plus/test";
5+
6+
const mocks = vi.hoisted(() => ({
7+
sessionAtom: {},
8+
searchEntries: vi.fn(() => null),
9+
}));
10+
11+
vi.mock("./session", () => ({
12+
environmentSession: { sessionStateAtom: () => mocks.sessionAtom },
13+
}));
14+
vi.mock("./presentation", () => ({
15+
useEnvironmentPresentation: () => ({
16+
isReady: true,
17+
presentation: { connection: { phase: "connected", error: null } },
18+
}),
19+
}));
20+
vi.mock("./pull-requests", () => ({ composerPullRequests: {} }));
21+
vi.mock("./projects", () => ({
22+
projectEnvironment: { searchEntries: mocks.searchEntries },
23+
}));
24+
vi.mock("./atom-registry", () => ({ appAtomRegistry: {} }));
25+
vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} }));
26+
vi.mock("./vcs", () => ({ vcsEnvironment: {} }));
27+
vi.mock("./query", () => ({
28+
useEnvironmentQuery: (atom: unknown) => ({
29+
data:
30+
atom === mocks.sessionAtom
31+
? { authenticated: true, scopes: [AuthFilesystemReadScope] }
32+
: null,
33+
error: null,
34+
isPending: false,
35+
refresh: () => {},
36+
}),
37+
}));
38+
39+
import { useComposerPathSearch, type ComposerPathSearchTarget } from "./queries";
40+
41+
function setup() {
42+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
43+
vi.useFakeTimers();
44+
mocks.searchEntries.mockClear();
45+
const root = createRoot(document.createElement("div"));
46+
function Probe({ target }: { readonly target: ComposerPathSearchTarget }) {
47+
useComposerPathSearch(target);
48+
return null;
49+
}
50+
51+
return {
52+
render(target: ComposerPathSearchTarget) {
53+
act(() => root.render(<Probe target={{ ...target }} />));
54+
},
55+
[Symbol.dispose]() {
56+
act(() => root.unmount());
57+
vi.useRealTimers();
58+
},
59+
};
60+
}
61+
62+
const target = {
63+
environmentId: EnvironmentId.make("test-environment"),
64+
cwd: "/repo",
65+
query: "src",
66+
};
67+
68+
it("keeps the original search deadline when rerenders have equivalent targets", () => {
69+
using harness = setup();
70+
harness.render({ ...target, query: null });
71+
harness.render({ ...target, query: " src " });
72+
act(() => vi.advanceTimersByTime(100));
73+
harness.render(target);
74+
act(() => vi.advanceTimersByTime(99));
75+
expect(mocks.searchEntries).not.toHaveBeenCalled();
76+
77+
act(() => vi.advanceTimersByTime(1));
78+
expect(mocks.searchEntries).toHaveBeenCalledExactlyOnceWith({
79+
environmentId: target.environmentId,
80+
input: { cwd: target.cwd, query: target.query, limit: 20 },
81+
});
82+
});
83+
84+
it.each([
85+
{ field: "environment", changed: { ...target, environmentId: EnvironmentId.make("other") } },
86+
{ field: "project directory", changed: { ...target, cwd: "/other" } },
87+
{ field: "query", changed: { ...target, query: "test" } },
88+
])("restarts the search deadline when the $field changes", ({ changed }) => {
89+
using harness = setup();
90+
harness.render({ ...target, query: null });
91+
harness.render(target);
92+
act(() => vi.advanceTimersByTime(100));
93+
harness.render(changed);
94+
95+
act(() => vi.advanceTimersByTime(100));
96+
expect(mocks.searchEntries).not.toHaveBeenCalled();
97+
act(() => vi.advanceTimersByTime(99));
98+
expect(mocks.searchEntries).not.toHaveBeenCalled();
99+
100+
act(() => vi.advanceTimersByTime(1));
101+
expect(mocks.searchEntries).toHaveBeenCalledExactlyOnceWith({
102+
environmentId: changed.environmentId,
103+
input: { cwd: changed.cwd, query: changed.query, limit: 20 },
104+
});
105+
});

‎apps/mobile/src/state/queries.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as Cause from "effect/Cause";
3030
import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail";
3131
import * as Option from "effect/Option";
3232
import { AsyncResult, Atom } from "effect/reactivity";
33-
import { useEffect, useState } from "react";
33+
import { useEffect, useMemo, useState } from "react";
3434

3535
import { appAtomRegistry } from "./atom-registry";
3636
import { orchestrationEnvironment } from "./orchestration";
@@ -370,11 +370,11 @@ export function usePaginatedBranches(target: VcsRefTarget) {
370370
}
371371

372372
export function useComposerPathSearch(target: ComposerPathSearchTarget) {
373-
const normalizedTarget = {
374-
environmentId: target.environmentId,
375-
cwd: target.cwd,
376-
query: normalizeComposerPathSearchQuery(target.query),
377-
};
373+
const query = normalizeComposerPathSearchQuery(target.query);
374+
const normalizedTarget = useMemo(
375+
() => ({ environmentId: target.environmentId, cwd: target.cwd, query }),
376+
[target.environmentId, target.cwd, query],
377+
);
378378
const debouncedTarget = useDebouncedValue(normalizedTarget, COMPOSER_PATH_SEARCH_DEBOUNCE_MS);
379379
const fileAccessSession = useEnvironmentQuery(
380380
debouncedTarget.environmentId === null

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const reactTests = [
1010
"src/native/NativeHeaderToolbar.test.tsx",
1111
"src/native/useRestingTopOffset.test.tsx",
1212
"src/native/useRetryingImageSource.test.tsx",
13+
"src/state/queries.debounce.test.tsx",
1314
"src/state/use-selected-thread-git-actions.test.tsx",
1415
];
1516

0 commit comments

Comments
 (0)