diff --git a/frontend/hooks/useFileStorage.js b/frontend/hooks/useFileStorage.js index edb25f5e0..a96805dbd 100644 --- a/frontend/hooks/useFileStorage.js +++ b/frontend/hooks/useFileStorage.js @@ -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 + }; } diff --git a/frontend/pages/file-storage/FileList.jsx b/frontend/pages/file-storage/FileList.jsx index ee36f1a93..438d9e6ff 100644 --- a/frontend/pages/file-storage/FileList.jsx +++ b/frontend/pages/file-storage/FileList.jsx @@ -122,8 +122,8 @@ const FileList = ({ onViewDetails, currentSortKey, currentSortOrder, + children, }) => { - const EMPTY_STATE_MESSAGE = 'No files or folders found.'; const TABLE_CAPTION = ` Listing all contents for the current folder, sorted by ${currentSortKey} in ${ariaFormatSort(currentSortOrder)} order @@ -192,6 +192,7 @@ const FileList = ({
+ {children} {data.map((item) => (