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

View file

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

View file

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

View file

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

View file

@ -6,7 +6,10 @@ export enum CollectionType {
folder = 'folder', folder = 'folder',
favorites = 'favorites', favorites = 'favorites',
album = 'album', album = 'album',
system = 'system', archive = 'archive',
trash = 'trash',
all = 'all',
shared = 'shared',
} }
export enum COLLECTION_SORT_BY { export enum COLLECTION_SORT_BY {
@ -19,3 +22,20 @@ export enum COLLECTION_SORT_BY {
export const COLLECTION_SHARE_DEFAULT_VALID_DURATION = export const COLLECTION_SHARE_DEFAULT_VALID_DURATION =
10 * 24 * 60 * 60 * 1000 * 1000; 10 * 24 * 60 * 60 * 1000 * 1000;
export const COLLECTION_SHARE_DEFAULT_DEVICE_LIMIT = 4; 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, CollectionType,
ARCHIVE_SECTION, ARCHIVE_SECTION,
TRASH_SECTION, TRASH_SECTION,
COLLECTION_SORT_ORDER,
ALL_SECTION,
} from 'constants/collection'; } from 'constants/collection';
import { UpdateMagicMetadataRequest } from 'types/magicMetadata'; import { UpdateMagicMetadataRequest } from 'types/magicMetadata';
import { EncryptionResult } from 'types/upload'; import { EncryptionResult } from 'types/upload';
@ -714,8 +716,8 @@ export function sortCollectionSummaries(
collectionSummaries: CollectionSummary[], collectionSummaries: CollectionSummary[],
sortBy: COLLECTION_SORT_BY sortBy: COLLECTION_SORT_BY
) { ) {
return moveFavCollectionToFront( return collectionSummaries
collectionSummaries.sort((a, b) => { .sort((a, b) => {
switch (sortBy) { switch (sortBy) {
case COLLECTION_SORT_BY.CREATION_TIME_DESCENDING: case COLLECTION_SORT_BY.CREATION_TIME_DESCENDING:
return compareCollectionsLatestFile( return compareCollectionsLatestFile(
@ -733,7 +735,11 @@ export function sortCollectionSummaries(
return a.name.localeCompare(b.name); 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) { 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( export function getCollectionSummaries(
collections: Collection[], collections: Collection[],
files: EnteFile[] files: EnteFile[]
@ -768,13 +764,19 @@ export function getCollectionSummaries(
collectionSummaries.set(collection.id, { collectionSummaries.set(collection.id, {
id: collection.id, id: collection.id,
name: collection.name, name: collection.name,
type: collection.type,
latestFile: collectionLatestFiles.get(collection.id), latestFile: collectionLatestFiles.get(collection.id),
fileCount: collectionFilesCount.get(collection.id) ?? 0, fileCount: collectionFilesCount.get(collection.id) ?? 0,
updationTime: collection.updationTime, 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( collectionSummaries.set(
ARCHIVE_SECTION, ARCHIVE_SECTION,
getArchivedCollectionSummaries( getArchivedCollectionSummaries(
@ -801,18 +803,31 @@ function getCollectionsFileCount(files: EnteFile[]): CollectionFilesCount {
return 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( function getArchivedCollectionSummaries(
collectionFilesCount: CollectionFilesCount, collectionFilesCount: CollectionFilesCount,
collectionsLatestFile: CollectionLatestFiles collectionsLatestFile: CollectionLatestFiles
): CollectionSummary { ): CollectionSummary {
return { return {
id: ARCHIVE_SECTION, id: ARCHIVE_SECTION,
name: constants.ARCHIVED, name: constants.ARCHIVE_SECTION_NAME,
type: CollectionType.system, type: CollectionType.archive,
latestFile: collectionsLatestFile.get(ARCHIVE_SECTION), latestFile: collectionsLatestFile.get(ARCHIVE_SECTION),
fileCount: collectionFilesCount.get(ARCHIVE_SECTION) ?? 0, fileCount: collectionFilesCount.get(ARCHIVE_SECTION) ?? 0,
updationTime: collectionsLatestFile.get(ARCHIVE_SECTION)?.updationTime, updationTime: collectionsLatestFile.get(ARCHIVE_SECTION)?.updationTime,
isSharedAlbum: false,
}; };
} }
@ -823,10 +838,9 @@ function getTrashedCollectionSummaries(
return { return {
id: TRASH_SECTION, id: TRASH_SECTION,
name: constants.TRASH, name: constants.TRASH,
type: CollectionType.system, type: CollectionType.trash,
latestFile: collectionsLatestFile.get(TRASH_SECTION), latestFile: collectionsLatestFile.get(TRASH_SECTION),
fileCount: collectionFilesCount.get(TRASH_SECTION) ?? 0, fileCount: collectionFilesCount.get(TRASH_SECTION) ?? 0,
updationTime: collectionsLatestFile.get(TRASH_SECTION)?.updationTime, updationTime: collectionsLatestFile.get(TRASH_SECTION)?.updationTime,
isSharedAlbum: false,
}; };
} }

View file

@ -95,7 +95,6 @@ export interface CollectionSummary {
latestFile: EnteFile; latestFile: EnteFile;
fileCount: number; fileCount: number;
updationTime: number; updationTime: number;
isSharedAlbum: boolean;
} }
export type CollectionSummaries = Map<number, CollectionSummary>; 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.', 'These files were uploaded, but unfortunately we could not generate the thumbnails for them.',
UPLOAD_TO_COLLECTION: 'Upload to album', UPLOAD_TO_COLLECTION: 'Upload to album',
ARCHIVE: 'Hide', ARCHIVE: 'Hide',
ARCHIVED: 'Hidden', ARCHIVE_SECTION_NAME: 'Hidden',
ALL_SECTION_NAME: 'All Photos', ALL_SECTION_NAME: 'All memories',
MOVE_TO_COLLECTION: 'Move to album', MOVE_TO_COLLECTION: 'Move to album',
UNARCHIVE: 'Unhide', UNARCHIVE: 'Unhide',
MOVE: 'Move', MOVE: 'Move',