handle special collections better

This commit is contained in:
Abhinav 2022-06-10 12:33:09 +05:30
parent 126d8ff443
commit 221f3a756f
8 changed files with 69 additions and 43 deletions

View file

@ -1,6 +1,9 @@
import React, { useMemo } from 'react';
import Divider from '@mui/material/Divider';
import { CollectionType, COLLECTION_SORT_BY } from 'constants/collection';
import {
COLLECTION_SORT_BY,
SPECIAL_COLLECTION_TYPES,
} from 'constants/collection';
import { sortCollectionSummaries } from 'services/collectionService';
import {
Transition,
@ -35,7 +38,7 @@ export default function AllCollections(props: Iprops) {
() =>
sortCollectionSummaries(
[...collectionSummaries.values()].filter(
(x) => x.type !== CollectionType.system
(x) => !SPECIAL_COLLECTION_TYPES.has(x.type)
),
collectionSortBy
),

View file

@ -96,13 +96,6 @@ export default function CollectionBar(props: IProps) {
/>
)}
<ScrollContainer ref={componentRef}>
<CollectionCardWithActiveIndicator
latestFile={null}
active={activeCollection === ALL_SECTION}
onClick={clickHandler(ALL_SECTION)}
collectionName={constants.ALL_SECTION_NAME}
/>
{sortedCollectionSummary.map((item) => (
<CollectionCardWithActiveIndicator
key={item.id}

View file

@ -4,7 +4,7 @@ import { Collection, CollectionSummary } from 'types/collection';
import { CollectionSectionWrapper } from 'components/Collections/styledComponents';
import CollectionOptions from 'components/Collections/CollectionOptions';
import { SetCollectionNamerAttributes } from 'components/Collections/CollectionNamer';
import { CollectionType } from 'constants/collection';
import { SPECIAL_COLLECTION_TYPES } from 'constants/collection';
import { SpaceBetweenFlex } from 'components/Container';
interface Iprops {
@ -28,10 +28,9 @@ export default function collectionInfoWithOptions({
<CollectionSectionWrapper>
<SpaceBetweenFlex>
<CollectionInfo name={name} fileCount={fileCount} />
{type !== CollectionType.system &&
type !== CollectionType.favorites && (
<CollectionOptions {...props} />
)}
{!SPECIAL_COLLECTION_TYPES.has(type) && (
<CollectionOptions {...props} />
)}
</SpaceBetweenFlex>
</CollectionSectionWrapper>
);

View file

@ -1,7 +1,7 @@
import React, { useEffect, useMemo } from 'react';
import AddCollectionButton from './AddCollectionButton';
import { Collection, CollectionSummaries } from 'types/collection';
import { CollectionType } from 'constants/collection';
import { SPECIAL_COLLECTION_TYPES } from 'constants/collection';
import DialogBoxBase from 'components/DialogBox/base';
import DialogTitleWithCloseButton from 'components/DialogBox/titleWithCloseButton';
import { DialogContent } from '@mui/material';
@ -36,11 +36,9 @@ function CollectionSelector({
const personalCollectionsOtherThanFrom = [
...collectionSummaries.values(),
]?.filter(
({ type, id, isSharedAlbum }) =>
({ type, id }) =>
id !== attributes?.fromCollection &&
!isSharedAlbum &&
type !== CollectionType.favorites &&
type !== CollectionType.system
!SPECIAL_COLLECTION_TYPES.has(type)
);
return personalCollectionsOtherThanFrom;
}, [collectionSummaries, attributes]);

View file

@ -6,7 +6,10 @@ export enum CollectionType {
folder = 'folder',
favorites = 'favorites',
album = 'album',
system = 'system',
archive = 'archive',
trash = 'trash',
all = 'all',
shared = 'shared',
}
export enum COLLECTION_SORT_BY {
@ -19,3 +22,20 @@ export enum COLLECTION_SORT_BY {
export const COLLECTION_SHARE_DEFAULT_VALID_DURATION =
10 * 24 * 60 * 60 * 1000 * 1000;
export const COLLECTION_SHARE_DEFAULT_DEVICE_LIMIT = 4;
export const COLLECTION_SORT_ORDER = new Map([
[CollectionType.all, 0],
[CollectionType.favorites, 1],
[CollectionType.album, 2],
[CollectionType.folder, 2],
[CollectionType.shared, 2],
[CollectionType.archive, 3],
[CollectionType.trash, 4],
]);
export const SPECIAL_COLLECTION_TYPES = new Set([
CollectionType.all,
CollectionType.archive,
CollectionType.trash,
CollectionType.shared,
]);

View file

@ -30,6 +30,8 @@ import {
CollectionType,
ARCHIVE_SECTION,
TRASH_SECTION,
COLLECTION_SORT_ORDER,
ALL_SECTION,
} from 'constants/collection';
import { UpdateMagicMetadataRequest } from 'types/magicMetadata';
import { EncryptionResult } from 'types/upload';
@ -714,8 +716,8 @@ export function sortCollectionSummaries(
collectionSummaries: CollectionSummary[],
sortBy: COLLECTION_SORT_BY
) {
return moveFavCollectionToFront(
collectionSummaries.sort((a, b) => {
return collectionSummaries
.sort((a, b) => {
switch (sortBy) {
case COLLECTION_SORT_BY.CREATION_TIME_DESCENDING:
return compareCollectionsLatestFile(
@ -733,7 +735,11 @@ export function sortCollectionSummaries(
return a.name.localeCompare(b.name);
}
})
);
.sort(
(a, b) =>
COLLECTION_SORT_ORDER.get(a.type) -
COLLECTION_SORT_ORDER.get(b.type)
);
}
function compareCollectionsLatestFile(first: EnteFile, second: EnteFile) {
@ -745,16 +751,6 @@ function compareCollectionsLatestFile(first: EnteFile, second: EnteFile) {
}
}
function moveFavCollectionToFront(collectionSummaries: CollectionSummary[]) {
return collectionSummaries.sort((a, b) =>
a.type === CollectionType.favorites
? -1
: b.type === CollectionType.favorites
? 1
: 0
);
}
export function getCollectionSummaries(
collections: Collection[],
files: EnteFile[]
@ -768,13 +764,19 @@ export function getCollectionSummaries(
collectionSummaries.set(collection.id, {
id: collection.id,
name: collection.name,
type: collection.type,
latestFile: collectionLatestFiles.get(collection.id),
fileCount: collectionFilesCount.get(collection.id) ?? 0,
updationTime: collection.updationTime,
isSharedAlbum: collection.owner.id !== user.id,
type:
collection.owner.id !== user.id
? CollectionType.shared
: collection.type,
});
}
collectionSummaries.set(
ALL_SECTION,
getAllCollectionSummaries(files.length, files[0])
);
collectionSummaries.set(
ARCHIVE_SECTION,
getArchivedCollectionSummaries(
@ -801,18 +803,31 @@ function getCollectionsFileCount(files: EnteFile[]): CollectionFilesCount {
return collectionFilesCount;
}
function getAllCollectionSummaries(
allFilesCount: number,
latestFile: EnteFile
): CollectionSummary {
return {
id: ALL_SECTION,
name: constants.ALL_SECTION_NAME,
type: CollectionType.all,
latestFile: latestFile,
fileCount: allFilesCount,
updationTime: latestFile?.updationTime,
};
}
function getArchivedCollectionSummaries(
collectionFilesCount: CollectionFilesCount,
collectionsLatestFile: CollectionLatestFiles
): CollectionSummary {
return {
id: ARCHIVE_SECTION,
name: constants.ARCHIVED,
type: CollectionType.system,
name: constants.ARCHIVE_SECTION_NAME,
type: CollectionType.archive,
latestFile: collectionsLatestFile.get(ARCHIVE_SECTION),
fileCount: collectionFilesCount.get(ARCHIVE_SECTION) ?? 0,
updationTime: collectionsLatestFile.get(ARCHIVE_SECTION)?.updationTime,
isSharedAlbum: false,
};
}
@ -823,10 +838,9 @@ function getTrashedCollectionSummaries(
return {
id: TRASH_SECTION,
name: constants.TRASH,
type: CollectionType.system,
type: CollectionType.trash,
latestFile: collectionsLatestFile.get(TRASH_SECTION),
fileCount: collectionFilesCount.get(TRASH_SECTION) ?? 0,
updationTime: collectionsLatestFile.get(TRASH_SECTION)?.updationTime,
isSharedAlbum: false,
};
}

View file

@ -95,7 +95,6 @@ export interface CollectionSummary {
latestFile: EnteFile;
fileCount: number;
updationTime: number;
isSharedAlbum: boolean;
}
export type CollectionSummaries = Map<number, CollectionSummary>;

View file

@ -582,8 +582,8 @@ const englishConstants = {
'These files were uploaded, but unfortunately we could not generate the thumbnails for them.',
UPLOAD_TO_COLLECTION: 'Upload to album',
ARCHIVE: 'Hide',
ARCHIVED: 'Hidden',
ALL_SECTION_NAME: 'All Photos',
ARCHIVE_SECTION_NAME: 'Hidden',
ALL_SECTION_NAME: 'All memories',
MOVE_TO_COLLECTION: 'Move to album',
UNARCHIVE: 'Unhide',
MOVE: 'Move',