33import { useQueryClient } from "@tanstack/react-query" ;
44import { useDomain } from "./useDomain" ;
55import { unwrapServiceError } from "@/lib/utils" ;
6- import { useCallback } from "react" ;
76import { 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