diff --git a/src/components/Sidebar/ShortcutSection.tsx b/src/components/Sidebar/ShortcutSection.tsx index 3092dfbc9..fdbaeb16b 100644 --- a/src/components/Sidebar/ShortcutSection.tsx +++ b/src/components/Sidebar/ShortcutSection.tsx @@ -1,7 +1,11 @@ import React, { useContext, useState, useEffect } from 'react'; import constants from 'utils/strings/constants'; import { GalleryContext } from 'pages/gallery'; -import { ARCHIVE_SECTION, TRASH_SECTION } from 'constants/collection'; +import { + ARCHIVE_SECTION, + DUMMY_UNCATEGORIZED_SECTION, + TRASH_SECTION, +} from 'constants/collection'; import { CollectionSummaries } from 'types/collection'; import ShortcutButton from './ShortcutButton'; import DeleteOutline from '@mui/icons-material/DeleteOutline'; @@ -22,8 +26,12 @@ export default function ShortcutSection({ useState(); useEffect(() => { const main = async () => { - const unCategorisedCollection = await getUncategorizedCollection(); - setUncategorizedCollectionID(unCategorisedCollection.id); + const unCategorizedCollection = await getUncategorizedCollection(); + if (unCategorizedCollection) { + setUncategorizedCollectionID(unCategorizedCollection.id); + } else { + setUncategorizedCollectionID(DUMMY_UNCATEGORIZED_SECTION); + } }; main(); }, []); diff --git a/src/constants/collection/index.ts b/src/constants/collection/index.ts index 30e697d4e..0d0b9eb88 100644 --- a/src/constants/collection/index.ts +++ b/src/constants/collection/index.ts @@ -1,5 +1,6 @@ export const ARCHIVE_SECTION = -1; export const TRASH_SECTION = -2; +export const DUMMY_UNCATEGORIZED_SECTION = -3; export const ALL_SECTION = 0; export enum CollectionType { folder = 'folder', diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 988494c4b..5ae4445fe 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -61,7 +61,9 @@ import { ALL_SECTION, ARCHIVE_SECTION, CollectionType, + DUMMY_UNCATEGORIZED_SECTION, TRASH_SECTION, + UNCATEGORIZED_COLLECTION_NAME, } from 'constants/collection'; import { AppContext } from 'pages/_app'; import { CustomError, ServerErrorCodes } from 'utils/error'; @@ -327,6 +329,8 @@ export default function Gallery() { collectionURL += constants.ARCHIVE; } else if (activeCollection === TRASH_SECTION) { collectionURL += constants.TRASH; + } else if (activeCollection === DUMMY_UNCATEGORIZED_SECTION) { + collectionURL += UNCATEGORIZED_COLLECTION_NAME; } else { collectionURL += activeCollection; } @@ -409,7 +413,7 @@ export default function Gallery() { const archivedCollections = getArchivedCollections(collections); setArchivedCollections(archivedCollections); - const collectionSummaries = getCollectionSummaries( + const collectionSummaries = await getCollectionSummaries( user, collections, files, diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 7fb435632..f00e0c34a 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -40,6 +40,7 @@ import { CollectionSummaryType, UNCATEGORIZED_COLLECTION_NAME, FAVORITE_COLLECTION_NAME, + DUMMY_UNCATEGORIZED_SECTION, } from 'constants/collection'; import { NEW_COLLECTION_MAGIC_METADATA, @@ -498,7 +499,10 @@ export const removeFromCollection = async ( files: EnteFile[] ) => { try { - const uncategorizedCollection = await getUncategorizedCollection(); + let uncategorizedCollection = await getUncategorizedCollection(); + if (!uncategorizedCollection) { + uncategorizedCollection = await createUnCategorizedCollection(); + } const allFiles = await getLocalFiles(); const groupiedFiles = groupFilesBasedOnID(allFiles); for (const file of files) { @@ -830,12 +834,12 @@ function compareCollectionsLatestFile(first: EnteFile, second: EnteFile) { } } -export function getCollectionSummaries( +export async function getCollectionSummaries( user: User, collections: Collection[], files: EnteFile[], archivedCollections: Set -): CollectionSummaries { +): Promise { const collectionSummaries: CollectionSummaries = new Map(); const collectionLatestFiles = getCollectionLatestFiles( files, @@ -847,12 +851,15 @@ export function getCollectionSummaries( ); for (const collection of collections) { - if (collectionFilesCount.get(collection.id)) { + if ( + collectionFilesCount.get(collection.id) || + collection.type === CollectionType.uncategorized + ) { collectionSummaries.set(collection.id, { id: collection.id, name: collection.name, latestFile: collectionLatestFiles.get(collection.id), - fileCount: collectionFilesCount.get(collection.id), + fileCount: collectionFilesCount.get(collection.id) ?? 0, updationTime: collection.updationTime, type: isIncomingShare(collection, user) ? CollectionSummaryType.incomingShare @@ -866,6 +873,16 @@ export function getCollectionSummaries( }); } } + const uncategorizedCollection = await getUncategorizedCollection( + collections + ); + + if (!uncategorizedCollection) { + collectionSummaries.set( + DUMMY_UNCATEGORIZED_SECTION, + getDummyUncategorizedCollectionSummaries() + ); + } collectionSummaries.set( ALL_SECTION, getAllCollectionSummaries(collectionFilesCount, collectionLatestFiles) @@ -933,6 +950,17 @@ function getAllCollectionSummaries( }; } +function getDummyUncategorizedCollectionSummaries(): CollectionSummary { + return { + id: ALL_SECTION, + name: UNCATEGORIZED_COLLECTION_NAME, + type: CollectionSummaryType.uncategorized, + latestFile: null, + fileCount: 0, + updationTime: 0, + }; +} + function getArchivedCollectionSummaries( collectionFilesCount: CollectionFilesCount, collectionsLatestFile: CollectionLatestFiles @@ -961,18 +989,20 @@ function getTrashedCollectionSummaries( }; } -export async function getUncategorizedCollection(): Promise { - const collections = await getLocalCollections(); - let uncategorizedCollection = collections.find( +export async function getUncategorizedCollection( + collections?: Collection[] +): Promise { + if (!collections) { + collections = await getLocalCollections(); + } + const uncategorizedCollection = collections.find( (collection) => collection.type === CollectionType.uncategorized ); - if (!uncategorizedCollection) { - uncategorizedCollection = await createUnCategorizedCollection(); - } + return uncategorizedCollection; } -async function createUnCategorizedCollection() { +export async function createUnCategorizedCollection() { return createCollection( UNCATEGORIZED_COLLECTION_NAME, CollectionType.uncategorized