From ddcc80419c529ee44d6d22c017b9590481f3378a Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Mon, 15 Mar 2021 23:00:49 +0530 Subject: [PATCH] remove empty collections --- src/pages/gallery/index.tsx | 4 +++- src/services/collectionService.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 3a7253d51..88acdd701 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -20,6 +20,7 @@ import { getFavItemIds, getLocalCollections, getCollectionUpdationTime, + getNonEmptyCollections, } from 'services/collectionService'; import constants from 'utils/strings/constants'; import AlertBanner from './components/AlertBanner'; @@ -160,12 +161,13 @@ export default function Gallery(props) { const encryptionKey = await getActualKey(); const collections = await syncCollections(token, encryptionKey); const { data, isUpdated } = await syncData(token, collections); + const nonEmptyCollections = getNonEmptyCollections(collections, data); const collectionAndItsLatestFile = await getCollectionAndItsLatestFile( collections, data ); const favItemIds = await getFavItemIds(data); - setCollections(collections); + setCollections(nonEmptyCollections); if (isUpdated) { setData(data); } diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 9c5366798..98c22172a 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -362,3 +362,16 @@ const setLocalFavoriteCollection = async (collections: collection[]) => { await localForage.setItem(FAV_COLLECTION, favCollection[0]); } }; + +export const getNonEmptyCollections = ( + collections: collection[], + files: file[] +) => { + const nonEmptyCollectionsIds = new Set(); + for (let file of files) { + nonEmptyCollectionsIds.add(file.collectionID); + } + return collections.filter((collection) => + nonEmptyCollectionsIds.has(collection.id) + ); +};