diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index c58607c51..76fa368a5 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -119,7 +119,7 @@ export default function Gallery(props) { const encryptionKey = await getActualKey(); const collections = await fetchCollections(token, encryptionKey); const data = await fetchData(token, collections); - const collectionLatestFile = await getCollectionLatestFile(collections, token); + const collectionLatestFile = await getCollectionLatestFile(collections, data); const favItemIds = await getFavItemIds(data); setCollections(collections); setData(data); @@ -296,7 +296,7 @@ export default function Gallery(props) { showUploadModal={props.showUploadModal} collectionLatestFile={collectionLatestFile} refetchData={() => setReload(Math.random())} - + /> {filteredData.length ? ( diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 29b3b78f1..aab38f90e 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -108,21 +108,25 @@ export const fetchCollections = async (token: string, key: string) => { return collections; }; -export const getCollectionLatestFile = async ( +export const getCollectionLatestFile = ( collections: collection[], - token -): Promise => { - return Promise.all( - collections.map(async collection => { - const sinceTime: string = (Number(await localForage.getItem(`${collection.id}-time`)) - 1).toString(); - const files: file[] = await getFiles([collection], sinceTime, "1", token) || []; + files: file[] +): collectionLatestFile[] => { + const latestFile = new Map(); + const collectionMap = new Map(); - return { - file: files[0], - collection, - } - })) -}; + collections.forEach(collection => collectionMap.set(Number(collection.id), collection)); + files.forEach(file => { + if (!latestFile.has(file.collectionID)) { + latestFile.set(file.collectionID, file) + } + }); + let allCollectionLatestFile: collectionLatestFile[] = []; + for (const [collectionID, file] of latestFile) { + allCollectionLatestFile.push({ collection: collectionMap.get(collectionID), file }); + } + return allCollectionLatestFile; +} export const getFavItemIds = async (files: file[]): Promise> => {