-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sort and don't flash when updating query
- Loading branch information
Showing
4 changed files
with
68 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,53 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useRef, useEffect } from 'react'; | ||
import federalist from '@util/federalistApi'; | ||
|
||
export default function useFileStorage(fileStorageId, path = '') { | ||
export default function useFileStorage( | ||
fileStorageId, | ||
path = '', | ||
sortKey = null, | ||
sortOrder = null | ||
) { | ||
// Create a ref to store previous data. (Declare it first!) | ||
const previousData = useRef(); | ||
|
||
const fetchPublicFiles = async () => { | ||
const response = await federalist.fetchPublicFiles(fileStorageId, path); | ||
const response = await federalist.fetchPublicFiles( | ||
fileStorageId, | ||
path, | ||
sortKey, | ||
sortOrder | ||
); | ||
if (response.error) { | ||
throw new Error(response.error); | ||
} | ||
return response.data; | ||
}; | ||
|
||
const { data = [], isLoading, error, refetch } = useQuery({ | ||
queryKey: ['fileStorage', fileStorageId, path], | ||
// Use placeholderData: previousData.current if available. | ||
const { data, isLoading, isFetching, error, refetch } = useQuery({ | ||
queryKey: ['fileStorage', fileStorageId, path, sortKey, sortOrder], | ||
queryFn: fetchPublicFiles, | ||
enabled: !!fileStorageId, | ||
keepPreviousData: true, | ||
staleTime: 2000, | ||
placeholderData: previousData.current || undefined, | ||
onError: (err) => console.error('Error fetching public files:', err), | ||
}); | ||
|
||
// Update previousData whenever new data is available. | ||
useEffect(() => { | ||
if (data !== undefined) { | ||
previousData.current = data; | ||
} | ||
}, [data]); | ||
|
||
return { data, isLoading, error, refetch }; | ||
return { | ||
data, | ||
previousData: previousData.current, | ||
isLoading, | ||
isFetching, | ||
error, | ||
refetch | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters