Skip to content

Commit 5ebd07f

Browse files
fix: Add default debounce & staleTime to file/folder prefetching (#346)
1 parent 65d6e92 commit 5ebd07f

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Text highlighting clarity. [#342](https://github.com/sourcebot-dev/sourcebot/pull/342)
1616
- Fixed repo list column header styling. [#344](https://github.com/sourcebot-dev/sourcebot/pull/344)
1717
- Clean up successful and failed jobs in Redis queues. [#343](https://github.com/sourcebot-dev/sourcebot/pull/343)
18+
- Fixed issue with files occasionally not loading after moving the cursor rapidly over the file browser. [#346](https://github.com/sourcebot-dev/sourcebot/pull/346)
1819

1920
## [4.2.0] - 2025-06-09
2021

packages/web/src/hooks/usePrefetchFileSource.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ import { useQueryClient } from "@tanstack/react-query";
44
import { useDomain } from "./useDomain";
55
import { unwrapServiceError } from "@/lib/utils";
66
import { getFileSource } from "@/features/search/fileSourceApi";
7-
import { useCallback } from "react";
7+
import { useDebounceCallback } from "usehooks-ts";
88

9-
export const usePrefetchFileSource = () => {
9+
interface UsePrefetchFileSourceProps {
10+
debounceDelay?: number;
11+
staleTime?: number;
12+
}
13+
14+
export const usePrefetchFileSource = ({
15+
debounceDelay = 200,
16+
staleTime = 5 * 60 * 1000, // 5 minutes
17+
}: UsePrefetchFileSourceProps = {}) => {
1018
const queryClient = useQueryClient();
1119
const domain = useDomain();
1220

13-
const prefetchFileSource = useCallback((repoName: string, revisionName: string, path: string) => {
21+
const prefetchFileSource = useDebounceCallback((repoName: string, revisionName: string, path: string) => {
1422
queryClient.prefetchQuery({
1523
queryKey: ['fileSource', repoName, revisionName, path, domain],
1624
queryFn: () => unwrapServiceError(getFileSource({
1725
fileName: path,
1826
repository: repoName,
1927
branch: revisionName,
2028
}, domain)),
29+
staleTime,
2130
});
22-
}, [queryClient, domain]);
31+
}, debounceDelay);
2332

2433
return { prefetchFileSource };
2534
}

packages/web/src/hooks/usePrefetchFolderContents.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
import { useQueryClient } from "@tanstack/react-query";
44
import { useDomain } from "./useDomain";
55
import { unwrapServiceError } from "@/lib/utils";
6-
import { useCallback } from "react";
76
import { getFolderContents } from "@/features/fileTree/actions";
7+
import { useDebounceCallback } from "usehooks-ts";
88

9-
export const usePrefetchFolderContents = () => {
9+
interface UsePrefetchFolderContentsProps {
10+
debounceDelay?: number;
11+
staleTime?: number;
12+
}
13+
14+
export const usePrefetchFolderContents = ({
15+
debounceDelay = 200,
16+
staleTime = 5 * 60 * 1000, // 5 minutes
17+
}: UsePrefetchFolderContentsProps = {}) => {
1018
const queryClient = useQueryClient();
1119
const domain = useDomain();
1220

13-
const prefetchFolderContents = useCallback((repoName: string, revisionName: string, path: string) => {
21+
const prefetchFolderContents = useDebounceCallback((repoName: string, revisionName: string, path: string) => {
1422
queryClient.prefetchQuery({
1523
queryKey: ['tree', repoName, revisionName, path, domain],
1624
queryFn: () => unwrapServiceError(
@@ -20,8 +28,9 @@ export const usePrefetchFolderContents = () => {
2028
path,
2129
}, domain)
2230
),
31+
staleTime,
2332
});
24-
}, [queryClient, domain]);
33+
}, debounceDelay);
2534

2635
return { prefetchFolderContents };
2736
}

0 commit comments

Comments
 (0)