Skip to content

Commit 18869d8

Browse files
saphidcodex
andcommitted
fix(mobile): preserve server matches in file search
Keep server result paths when flattening the Files tree so typo-tolerant matches remain visible and cached entries cannot become extra search results. Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 2c8e85e commit 18869d8

6 files changed

Lines changed: 41 additions & 102 deletions

File tree

‎apps/mobile/src/features/files/FileTreeBrowser.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export function FileTreeBrowser(props: {
109109
readonly isPending: boolean;
110110
readonly isRefreshing: boolean;
111111
readonly searchQuery: string;
112+
readonly searchResultPaths: ReadonlySet<string> | null;
112113
readonly searchTruncated: boolean;
113114
readonly selectedPath: string | null;
114115
readonly loadingDirectories: ReadonlySet<string>;
@@ -146,9 +147,9 @@ export function FileTreeBrowser(props: {
146147
flattenFileTree({
147148
nodes: tree,
148149
expanded: expandedPaths,
149-
searchQuery: props.searchQuery,
150+
searchResultPaths: props.searchResultPaths,
150151
}),
151-
[expandedPaths, props.searchQuery, tree],
152+
[expandedPaths, props.searchResultPaths, tree],
152153
);
153154

154155
useEffect(() => {

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export function ThreadFilesTreeScreen(props: ThreadFilesRouteScreenProps) {
573573
isPending={entriesQuery.isPending}
574574
isRefreshing={entriesQuery.isRefreshing}
575575
searchQuery={searchQuery}
576+
searchResultPaths={entriesQuery.searchResultPaths}
576577
searchTruncated={entriesQuery.searchTruncated}
577578
selectedPath={null}
578579
onPreviewFile={handlePreviewFile}

‎apps/mobile/src/features/files/fileTree.test.ts‎

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,31 @@ describe("mobile file tree helpers", () => {
5151
flattenFileTree({
5252
nodes: tree,
5353
expanded: new Set(),
54-
searchQuery: "app",
54+
searchResultPaths: new Set(["src/components/App.tsx"]),
5555
}).map((item) => item.node.path),
5656
).toEqual(["src", "src/components", "src/components/App.tsx"]);
5757
});
5858

59-
it("supports fuzzy, whitespace-separated path queries", () => {
60-
const tree = buildFileTree([
61-
{
62-
kind: "file",
63-
path: "docs/internals/workspace-layout.md",
64-
},
65-
{
66-
kind: "file",
67-
path: ".repos/alchemy-effect/examples/aws-lambda/src/JobNotifications.ts",
68-
},
69-
{ kind: "directory", path: "apps/web/src/components/chat" },
70-
{ kind: "file", path: "apps/web/src/components/chat/ChatHeader.test.ts" },
71-
{ kind: "file", path: "apps/web/src/components/chat/ChatHeader.tsx" },
72-
{ kind: "file", path: "apps/web/src/components/chat/Composer.tsx" },
73-
]);
74-
75-
const expectedPaths = [
76-
"apps",
77-
"apps/web",
78-
"apps/web/src",
79-
"apps/web/src/components",
80-
"apps/web/src/components/chat",
81-
"apps/web/src/components/chat/ChatHeader.test.ts",
82-
"apps/web/src/components/chat/ChatHeader.tsx",
83-
];
59+
it("excludes cached children of matched directories", () => {
60+
expect(
61+
flattenFileTree({
62+
nodes: buildFileTree(entries),
63+
expanded: new Set(["src", "src/components"]),
64+
searchResultPaths: new Set(["src/components"]),
65+
}).map((item) => item.node.path),
66+
).toEqual(["src", "src/components"]);
67+
});
8468

85-
for (const searchQuery of ["chat hea", "cht hdr"]) {
86-
expect(
87-
flattenFileTree({
88-
nodes: tree,
89-
expanded: new Set(),
90-
searchQuery,
91-
}).map((item) => item.node.path),
92-
).toEqual(expectedPaths);
93-
}
69+
it("keeps an empty search empty and restores expansion when search clears", () => {
70+
const tree = buildFileTree(entries);
71+
expect(
72+
flattenFileTree({ nodes: tree, expanded: new Set(["src"]), searchResultPaths: new Set() }),
73+
).toEqual([]);
74+
expect(
75+
flattenFileTree({ nodes: tree, expanded: new Set(), searchResultPaths: null }).map(
76+
(item) => item.node.path,
77+
),
78+
).toEqual(["src", "package.json", "README.md"]);
9479
});
9580

9681
it("expands top-level directories by default", () => {

‎apps/mobile/src/features/files/fileTree.ts‎

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import type { ProjectEntry } from "@t3tools/contracts";
2-
import { normalizeSearchQuery, scoreQueryMatch } from "@t3tools/shared/searchRanking";
32

43
export interface FileTreeNode {
54
readonly path: string;
65
readonly name: string;
76
readonly kind: ProjectEntry["kind"];
87
readonly ignored?: boolean;
98
readonly children: ReadonlyArray<FileTreeNode>;
10-
readonly searchSegments: ReadonlyArray<string>;
11-
readonly searchWords: ReadonlyArray<string>;
129
}
1310

1411
export interface VisibleFileTreeNode {
@@ -37,43 +34,13 @@ function createMutableNode(
3734
};
3835
}
3936

40-
function splitSearchWords(value: string): ReadonlyArray<string> {
41-
return value
42-
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
43-
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
44-
.split(/[^A-Za-z0-9]+/)
45-
.filter(Boolean)
46-
.map((word) => word.toLowerCase());
47-
}
48-
49-
function buildNodeSearchTerms(path: string): {
50-
readonly segments: ReadonlyArray<string>;
51-
readonly words: ReadonlyArray<string>;
52-
} {
53-
const segments: string[] = [];
54-
const words: string[] = [];
55-
56-
for (const segment of path.split("/")) {
57-
if (!segment) {
58-
continue;
59-
}
60-
segments.push(segment.toLowerCase());
61-
words.push(...splitSearchWords(segment));
62-
}
63-
64-
return { segments, words };
65-
}
66-
6737
function freezeNode(node: MutableFileTreeNode): FileTreeNode {
68-
const searchTerms = buildNodeSearchTerms(node.path);
6938
return {
7039
path: node.path,
7140
name: node.name,
7241
kind: node.kind,
7342
...(node.ignored ? { ignored: true } : {}),
7443
children: [...node.children.values()].sort(compareNodes).map(freezeNode),
75-
searchSegments: searchTerms.segments,
76-
searchWords: searchTerms.words,
7744
};
7845
}
7946

@@ -133,44 +100,21 @@ export function defaultExpandedTreePaths(nodes: ReadonlyArray<FileTreeNode>): Re
133100
return expanded;
134101
}
135102

136-
function valueMatchesSearchToken(value: string, token: string, fuzzy: boolean): boolean {
137-
return (
138-
scoreQueryMatch({
139-
value,
140-
query: token,
141-
exactBase: 0,
142-
prefixBase: 2,
143-
boundaryBase: 4,
144-
includesBase: 6,
145-
...(fuzzy ? { fuzzyBase: 100 } : {}),
146-
boundaryMarkers: ["/", "-", "_", "."],
147-
}) !== null
148-
);
149-
}
150-
151-
function nodeMatchesSearch(node: FileTreeNode, tokens: ReadonlyArray<string>): boolean {
152-
return tokens.every(
153-
(token) =>
154-
node.searchSegments.some((segment) => valueMatchesSearchToken(segment, token, false)) ||
155-
node.searchWords.some((word) => valueMatchesSearchToken(word, token, true)),
156-
);
157-
}
158-
159103
function flattenNode(
160104
output: VisibleFileTreeNode[],
161105
node: FileTreeNode,
162106
depth: number,
163107
expanded: ReadonlySet<string>,
164-
searchTokens: ReadonlyArray<string>,
108+
searchResultPaths: ReadonlySet<string> | null,
165109
): boolean {
166-
const isSearching = searchTokens.length > 0;
167-
const matches = isSearching && nodeMatchesSearch(node, searchTokens);
110+
const isSearching = searchResultPaths !== null;
111+
const matches = searchResultPaths?.has(node.path) ?? false;
168112
let descendantMatches = false;
169113
const childOutput: VisibleFileTreeNode[] = [];
170114

171115
if (node.kind === "directory" && (expanded.has(node.path) || isSearching)) {
172116
for (const child of node.children) {
173-
if (flattenNode(childOutput, child, depth + 1, expanded, searchTokens)) {
117+
if (flattenNode(childOutput, child, depth + 1, expanded, searchResultPaths)) {
174118
descendantMatches = true;
175119
}
176120
}
@@ -189,13 +133,12 @@ function flattenNode(
189133
export function flattenFileTree(input: {
190134
readonly nodes: ReadonlyArray<FileTreeNode>;
191135
readonly expanded: ReadonlySet<string>;
192-
readonly searchQuery?: string;
136+
// Null restores normal directory expansion; an empty set is a search with no results.
137+
readonly searchResultPaths?: ReadonlySet<string> | null;
193138
}): ReadonlyArray<VisibleFileTreeNode> {
194139
const output: VisibleFileTreeNode[] = [];
195-
const normalizedSearch = normalizeSearchQuery(input.searchQuery ?? "");
196-
const searchTokens = normalizedSearch.split(/[\s/\\._-]+/).filter(Boolean);
197140
for (const node of input.nodes) {
198-
flattenNode(output, node, 0, input.expanded, searchTokens);
141+
flattenNode(output, node, 0, input.expanded, input.searchResultPaths ?? null);
199142
}
200143
return output;
201144
}

‎apps/mobile/src/features/files/thread-file-navigator-pane.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function ThreadFileNavigatorPane(props: {
9999
isPending={fileAccess.isPending || entriesQuery.isPending}
100100
isRefreshing={entriesQuery.isRefreshing}
101101
searchQuery={searchQuery}
102+
searchResultPaths={entriesQuery.searchResultPaths}
102103
searchTruncated={entriesQuery.searchTruncated}
103104
selectedPath={props.selectedPath}
104105
onPreviewFile={handlePreviewFile}

‎apps/mobile/src/features/files/useFileTreeEntries.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ export function useFileTreeEntries(input: {
118118
}
119119
};
120120
visit((rootData?.entries ?? []).filter((entry) => !entry.path.includes("/")));
121-
return { revision: entriesRevision, entries: [...merged.values()], reachableDirectories };
121+
return {
122+
revision: entriesRevision,
123+
entries: [...merged.values()],
124+
reachableDirectories,
125+
searchResultPaths: searching
126+
? new Set((searchData?.entries ?? []).map((entry) => entry.path))
127+
: null,
128+
};
122129
}, [directories, entriesRevision, rootData, searchData, searching]);
123130

124131
const refresh = useCallback(() => {
@@ -178,6 +185,7 @@ export function useFileTreeEntries(input: {
178185

179186
return {
180187
entries: snapshot.entries,
188+
searchResultPaths: snapshot.searchResultPaths,
181189
isRefreshing: refreshingDirectories === directories,
182190
error:
183191
root.error ??

0 commit comments

Comments
 (0)