diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index b44685329..6ca87f7b8 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -129,20 +129,20 @@ export default function Gallery(props) { setCollections(collections); setLoading(false); setProgress(80); - await loadData(); + await syncWithRemote(); setProgress(100); }; main(); props.setUploadButtonView(true); }, []); - const loadData = async () => { + const syncWithRemote = async () => { const token = getToken(); const encryptionKey = await getActualKey(); const updatedCollections = await fetchUpdatedCollections(token, encryptionKey); const data = await fetchData(token, updatedCollections); const collections = await getLocalCollections(); - const collectionLatestFile = await getCollectionLatestFile(collections, token); + const collectionLatestFile = await getCollectionLatestFile(collections, data); const favItemIds = await getFavItemIds(data); if (updatedCollections.length > 0) { setCollections(collections); @@ -150,7 +150,7 @@ export default function Gallery(props) { } setCollectionLatestFile(collectionLatestFile); setFavItemIds(favItemIds); - + props.setUploadButtonView(true); } if (!data || loading) { @@ -206,7 +206,7 @@ export default function Gallery(props) { const handleClose = () => { setOpen(false); - // setReload(Math.random()); + // syncWithRemote(); }; const onThumbnailClick = (index: number) => () => { @@ -322,7 +322,7 @@ export default function Gallery(props) { closeUploadModal={props.closeUploadModal} showUploadModal={props.showUploadModal} collectionLatestFile={collectionLatestFile} - refetchData={loadData} + refetchData={syncWithRemote} /> {filteredData.length ? ( diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 0598e42f5..eb2300e88 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -128,21 +128,25 @@ export const fetchUpdatedCollections = async (token: string, key: string) => { return updatedCollections; }; -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> => {