Skip to content

Commit a62452a

Browse files
authored
perf(mobile): text files open in half the time on a remote host (#27850)
1 parent aa33734 commit a62452a

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
isMarkdownPreviewFile,
6969
isSvgImagePreviewFile,
7070
isVideoPreviewFile,
71+
needsWorkspaceFileContents,
7172
} from "./filePath";
7273
import { useWorkspaceFileAssetUrlState } from "./workspaceFileAssetUrl";
7374

@@ -707,15 +708,12 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
707708
const handleRetryPreview = () => {
708709
void assetPreview.refresh().finally(() => setPreviewRevision((current) => current + 1));
709710
};
710-
const needsFileContents =
711-
!metadataPending &&
712-
directoryPath === null &&
713-
relativePath !== null &&
714-
!isVideoFile &&
715-
!isAudioFile &&
716-
(resolvedActiveMode === "source" ||
717-
isMarkdownPreviewFile(relativePath) ||
718-
filePreviewDelimiter({ name: relativePath }) !== null);
711+
// Metadata is needed only to distinguish directories; file contents can load concurrently.
712+
const needsFileContents = needsWorkspaceFileContents({
713+
relativePath,
714+
metadataKind: metadata.data?.kind,
715+
activeMode: resolvedActiveMode,
716+
});
719717
const fileQuery = useEnvironmentQuery(
720718
canReadFiles &&
721719
environmentId !== null &&

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
fileRoutePathSegments,
5+
needsWorkspaceFileContents,
56
resolveFileLinkPath,
67
resolveWorkspaceRelativeFilePath,
78
} from "./filePath";
@@ -49,3 +50,11 @@ describe("resolveWorkspaceRelativeFilePath", () => {
4950
expect(resolveWorkspaceRelativeFilePath(null, "/repo/main.ts")).toBeNull();
5051
});
5152
});
53+
54+
describe("needsWorkspaceFileContents", () => {
55+
it("reads a text file while its metadata is still pending, but not a directory", () => {
56+
const input = { relativePath: "package.json", activeMode: "source" } as const;
57+
expect(needsWorkspaceFileContents({ ...input, metadataKind: undefined })).toBe(true);
58+
expect(needsWorkspaceFileContents({ ...input, metadataKind: "directory" })).toBe(false);
59+
});
60+
});

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { FilesystemEntryMetadata } from "@t3tools/contracts";
2+
import { filePreviewDelimiter } from "@t3tools/shared/delimitedPreview";
13
import { joinPath, resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
24
import {
35
isWorkspaceAudioPreviewPath,
@@ -109,6 +111,23 @@ export function isMarkdownPreviewFile(path: string): boolean {
109111
return /\.(?:md|mdx)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
110112
}
111113

114+
export function needsWorkspaceFileContents(input: {
115+
readonly relativePath: string | null;
116+
readonly metadataKind: FilesystemEntryMetadata["kind"] | undefined;
117+
readonly activeMode: "preview" | "source";
118+
}): boolean {
119+
const path = input.relativePath;
120+
return (
121+
input.metadataKind !== "directory" &&
122+
path !== null &&
123+
!isVideoPreviewFile(path) &&
124+
!isAudioPreviewFile(path) &&
125+
(input.activeMode === "source" ||
126+
isMarkdownPreviewFile(path) ||
127+
filePreviewDelimiter({ name: path }) !== null)
128+
);
129+
}
130+
112131
export function fileHeaderSubtitle(projectName: string, relativePath: string): string {
113132
const parentDir = relativePath.slice(
114133
0,

0 commit comments

Comments
 (0)