Skip to content

Commit ba3dd34

Browse files
fix(mobile): avoid rebuilding file trees before showing folder loading
1 parent 7726824 commit ba3dd34

3 files changed

Lines changed: 25 additions & 17 deletions

File tree

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const FileTreeRow = memo(function FileTreeRow(props: {
4444
readonly expanded: boolean;
4545
readonly loaded: boolean;
4646
readonly loading: boolean;
47-
readonly onPressDirectory: (path: string) => void;
47+
readonly onPressDirectory: (path: string, expand: boolean) => void;
4848
readonly onPreviewFile?: (path: string) => void;
4949
readonly onPressFile: (path: string) => void;
5050
}) {
@@ -61,7 +61,7 @@ const FileTreeRow = memo(function FileTreeRow(props: {
6161
}}
6262
onPress={() => {
6363
if (node.kind === "directory") {
64-
props.onPressDirectory(node.path);
64+
props.onPressDirectory(node.path, !props.expanded);
6565
return;
6666
}
6767
props.onPressFile(node.path);
@@ -188,17 +188,21 @@ export function FileTreeBrowser(props: {
188188
[],
189189
);
190190

191-
const toggleDirectory = useCallback((path: string) => {
192-
setExpandedPaths((current) => {
193-
const next = new Set(current);
194-
if (next.has(path)) {
195-
next.delete(path);
196-
} else {
197-
next.add(path);
198-
}
199-
return next;
200-
});
201-
}, []);
191+
const toggleDirectory = useCallback(
192+
(path: string, expand: boolean) => {
193+
if (expand) onLoadDirectory(path);
194+
setExpandedPaths((current) => {
195+
const next = new Set(current);
196+
if (next.has(path)) {
197+
next.delete(path);
198+
} else {
199+
next.add(path);
200+
}
201+
return next;
202+
});
203+
},
204+
[onLoadDirectory],
205+
);
202206
const handleSelectFile = useCallback(
203207
(path: string) => {
204208
if (pendingSelectionTimeoutRef.current !== null) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ function freezeNode(node: MutableFileTreeNode): FileTreeNode {
7777
};
7878
}
7979

80+
const fileNameCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
81+
8082
function compareNodes(
8183
left: Pick<FileTreeNode, "kind" | "name">,
8284
right: Pick<FileTreeNode, "kind" | "name">,
8385
): number {
8486
if (left.kind !== right.kind) {
8587
return left.kind === "directory" ? -1 : 1;
8688
}
87-
return left.name.localeCompare(right.name, undefined, { numeric: true, sensitivity: "base" });
89+
return fileNameCollator.compare(left.name, right.name);
8890
}
8991

9092
export function buildFileTree(entries: ReadonlyArray<ProjectEntry>): ReadonlyArray<FileTreeNode> {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export function useFileTreeEntries(input: {
3030
})
3131
: null,
3232
);
33-
const [revision, render] = useReducer((value: number) => value + 1, 0);
33+
const [, render] = useReducer((value: number) => value + 1, 0);
34+
const [entriesRevision, entriesChanged] = useReducer((value: number) => value + 1, 0);
3435
const refreshVersion = useRef(0);
3536
const directories = useMemo(
3637
() => ({
@@ -86,6 +87,7 @@ export function useFileTreeEntries(input: {
8687
entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
8788
),
8889
);
90+
entriesChanged();
8991
} else {
9092
const error = Cause.squash(result.cause);
9193
directories.errors.set(
@@ -116,8 +118,8 @@ export function useFileTreeEntries(input: {
116118
}
117119
};
118120
visit((rootData?.entries ?? []).filter((entry) => !entry.path.includes("/")));
119-
return { revision, entries: [...merged.values()], reachableDirectories };
120-
}, [directories, revision, rootData, searchData, searching]);
121+
return { revision: entriesRevision, entries: [...merged.values()], reachableDirectories };
122+
}, [directories, entriesRevision, rootData, searchData, searching]);
121123

122124
const refresh = useCallback(() => {
123125
refreshRoot();

0 commit comments

Comments
 (0)