update collection summary

This commit is contained in:
Abhinav 2022-06-03 14:48:39 +05:30
parent 5652811918
commit f66eaa8c71
3 changed files with 32 additions and 40 deletions

View file

@ -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 <></>;

View file

@ -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,
},
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,
};
}

View file

@ -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<number, CollectionSummary>;