From f66eaa8c7134023f3227590e44b107688f04f9fe Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 3 Jun 2022 14:48:39 +0530 Subject: [PATCH] update collection summary --- .../pages/gallery/CollectionSelector.tsx | 42 ++++++++----------- src/services/collectionService.ts | 22 +++++----- src/types/collection/index.ts | 8 ++-- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/components/pages/gallery/CollectionSelector.tsx b/src/components/pages/gallery/CollectionSelector.tsx index 14d36157f..16517fd66 100644 --- a/src/components/pages/gallery/CollectionSelector.tsx +++ b/src/components/pages/gallery/CollectionSelector.tsx @@ -1,12 +1,6 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo } from 'react'; import AddCollectionButton from './AddCollectionButton'; -import { getData, LS_KEYS } from 'utils/storage/localStorage'; -import { User } from 'types/user'; -import { - Collection, - CollectionSummaries, - CollectionSummary, -} from 'types/collection'; +import { Collection, CollectionSummaries } from 'types/collection'; import { CollectionType } from 'constants/collection'; import DialogBoxBase from 'components/DialogBox/base'; import DialogTitleWithCloseButton from 'components/DialogBox/titleWithCloseButton'; @@ -38,30 +32,28 @@ function CollectionSelector({ collections, ...props }: Props) { - const [collectionToShow, setCollectionToShow] = useState< - CollectionSummary[] - >([]); + const collectionToShow = useMemo(() => { + const personalCollectionsOtherThanFrom = [ + ...collectionSummaries.values(), + ]?.filter( + ({ type, id, isSharedAlbum }) => + id !== attributes.fromCollection && + !isSharedAlbum && + type !== CollectionType.favorites && + type !== CollectionType.system + ); + return personalCollectionsOtherThanFrom; + }, [collectionSummaries]); + useEffect(() => { if (!attributes || !props.open) { return; } - const user: User = getData(LS_KEYS.USER); - const personalCollectionsOtherThanFrom = [ - ...collectionSummaries.values(), - ]?.filter( - ({ type, id, attributes: collectionAttributes }) => - id !== attributes.fromCollection && - collectionAttributes.ownerID === user?.id && - type !== CollectionType.favorites && - type !== CollectionType.system - ); - if (personalCollectionsOtherThanFrom.length === 0) { + if (collectionToShow.length === 0) { props.onClose(); attributes.showNextModal(); - } else { - setCollectionToShow(personalCollectionsOtherThanFrom); } - }, [props.open]); + }, [collectionToShow, attributes, props.open]); if (!attributes) { return <>; diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index c7aa4d9a4..fb233a585 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -35,6 +35,7 @@ import { UpdateMagicMetadataRequest } from 'types/magicMetadata'; import { EncryptionResult } from 'types/upload'; import constants from 'utils/strings/constants'; import { IsArchived } from 'utils/magicMetadata'; +import { User } from 'types/user'; const ENDPOINT = getEndpoint(); const COLLECTION_TABLE = 'collections'; @@ -727,9 +728,7 @@ export function sortCollectionSummaries( compareCollectionsLatestFile(b.latestFile, a.latestFile) ); case COLLECTION_SORT_BY.UPDATION_TIME_DESCENDING: - return ( - b.attributes.updationTime - a.attributes.updationTime - ); + return b.updationTime - a.updationTime; case COLLECTION_SORT_BY.NAME: return a.name.localeCompare(b.name); } @@ -763,6 +762,7 @@ export function getCollectionSummaries( const collectionSummaries: CollectionSummaries = new Map(); const collectionLatestFiles = getCollectionLatestFiles(files); const collectionFilesCount = getCollectionsFileCount(files); + const user: User = getData(LS_KEYS.USER); for (const collection of collections) { collectionSummaries.set(collection.id, { @@ -771,10 +771,8 @@ export function getCollectionSummaries( type: collection.type, latestFile: collectionLatestFiles.get(collection.id), fileCount: collectionFilesCount.get(collection.id) ?? 0, - attributes: { - updationTime: collection.updationTime, - ownerID: collection.owner.id, - }, + updationTime: collection.updationTime, + isSharedAlbum: collection.owner.id !== user.id, }); } collectionSummaries.set( @@ -806,14 +804,16 @@ function getCollectionsFileCount(files: EnteFile[]): CollectionFilesCount { function getArchivedCollectionSummaries( collectionFilesCount: CollectionFilesCount, collectionsLatestFile: CollectionLatestFiles -) { +): CollectionSummary { return { - id: TRASH_SECTION, + id: ARCHIVE_SECTION, name: constants.ARCHIVE, type: CollectionType.system, latestFile: collectionsLatestFile.get(ARCHIVE_SECTION), fileCount: collectionFilesCount.get(ARCHIVE_SECTION) ?? 0, - } as CollectionSummary; + updationTime: collectionsLatestFile.get(ARCHIVE_SECTION)?.updationTime, + isSharedAlbum: false, + }; } function getTrashedCollectionSummaries( @@ -826,5 +826,7 @@ function getTrashedCollectionSummaries( type: CollectionType.system, latestFile: collectionsLatestFile.get(TRASH_SECTION), fileCount: collectionFilesCount.get(TRASH_SECTION) ?? 0, + updationTime: collectionsLatestFile.get(TRASH_SECTION)?.updationTime, + isSharedAlbum: false, }; } diff --git a/src/types/collection/index.ts b/src/types/collection/index.ts index 478991af8..795e1cfff 100644 --- a/src/types/collection/index.ts +++ b/src/types/collection/index.ts @@ -92,12 +92,10 @@ export interface CollectionSummary { id: number; name: string; type: CollectionType; - latestFile?: EnteFile; + latestFile: EnteFile; fileCount: number; - attributes?: { - ownerID?: number; - updationTime?: number; - }; + updationTime: number; + isSharedAlbum: boolean; } export type CollectionSummaries = Map;