Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

hidden + normal - wip 2

Manav Rathi02e2abef0713
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 59942f4224..1f519def67 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -78,6 +78,7 @@ import { updateFilesVisibility } from "ente-new/photos/services/file";
import {
savedCollectionFiles,
savedCollections,
+ savedHiddenCollectionIDs,
savedTrashItems,
} from "ente-new/photos/services/photos-fdb";
import {
@@ -329,6 +330,7 @@ const Page: React.FC = () => {
user,
familyData,
collections: await savedCollections(),
+ hiddenCollectionsIDs: await savedHiddenCollectionIDs(),
collectionFiles: await savedCollectionFiles(),
trashItems: await savedTrashItems(),
});
@@ -536,8 +538,12 @@ const Page: React.FC = () => {
*/
const fileAndCollectionSyncWithRemote = useCallback(async () => {
const didUpdateFiles = await syncCollectionAndFiles({
- onSetCollections: (collections) =>
- dispatch({ type: "setCollections", collections }),
+ onSetCollections: (collections, hiddenCollectionIDs) =>
+ dispatch({
+ type: "setCollections",
+ collections,
+ hiddenCollectionIDs,
+ }),
onSetCollectionFiles: (collectionFiles) =>
dispatch({ type: "setCollectionFiles", collectionFiles }),
onAugmentCollectionFiles: (collectionFiles) =>
diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts
index f5f1927c76..4152b74193 100644
--- a/web/packages/new/photos/components/gallery/reducer.ts
+++ b/web/packages/new/photos/components/gallery/reducer.ts
@@ -118,13 +118,13 @@ export interface GalleryState {
/*--< Primary state: Files, collections, people >--*/
/**
- * The user's normal (non-hidden) collections.
+ * The user's collections.
*/
- normalCollections: Collection[];
+ collections: Collection[];
/**
- * The user's hidden collections.
+ * The collection IDs of the user's hidden collections.
*/
- hiddenCollections: Collection[];
+ hiddenCollectionsIDs: Set<number>;
/**
* The user's files, without any unsynced modifications applied to them.
*
@@ -179,13 +179,6 @@ export interface GalleryState {
* Collection IDs of default hidden collections.
*/
defaultHiddenCollectionIDs: Set<number>;
- /**
- * File IDs of the files that the user has hidden.
- *
- * Unlike archived files which can be in a mixture of normal and archived
- * albums, hidden files can only be in hidden albums.
- */
- hiddenFileIDs: Set<number>;
/**
* File IDs of the files that the user has archived.
*
@@ -410,10 +403,15 @@ export type GalleryAction =
user: User;
familyData: FamilyData;
collections: Collection[];
+ hiddenCollectionsIDs: Set<number>;
collectionFiles: EnteFile[];
trashItems: TrashItem[];
}
- | { type: "setCollections"; collections: Collection[] }
+ | {
+ type: "setCollections";
+ collections: Collection[];
+ hiddenCollectionIDs: Set<number>;
+ }
| { type: "setCollectionFiles"; collectionFiles: EnteFile[] }
| { type: "augmentCollectionFiles"; collectionFiles: EnteFile[] }
| { type: "uploadFile"; file: EnteFile }
@@ -448,15 +446,14 @@ export type GalleryAction =
const initialGalleryState: GalleryState = {
user: undefined,
familyData: undefined,
- normalCollections: [],
- hiddenCollections: [],
- lastSyncedFiles: [],
+ collections: [],
+ hiddenCollectionsIDs: new Set(),
+ lastSyncedCollectionFiles: [],
trashItems: [],
peopleState: undefined,
- files: [],
+ collectionFiles: [],
archivedCollectionIDs: new Set(),
defaultHiddenCollectionIDs: new Set(),
- hiddenFileIDs: new Set(),
archivedFileIDs: new Set(),
favoriteFileIDs: new Set(),
collectionNameByID: new Map(),
@@ -490,29 +487,22 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
if (process.env.NEXT_PUBLIC_ENTE_TRACE) console.log("dispatch", action);
switch (action.type) {
case "mount": {
- const lastSyncedFiles = sortFiles(
- action.normalFiles.concat(action.hiddenFiles),
- );
- const lastSyncedNormalFiles = sortFiles(action.normalFiles);
- const lastSyncedHiddenFiles = sortFiles(action.hiddenFiles);
+ const lastSyncedCollectionFiles = sortFiles(action.collectionFiles);
const trashItems = sortTrashItems(action.trashItems);
// During mount there are no unsynced updates, and we can directly
// use the provided files.
- const files = lastSyncedFiles;
- const normalFiles = lastSyncedNormalFiles;
- const hiddenFiles = lastSyncedHiddenFiles;
+ const collectionFiles = lastSyncedCollectionFiles;
- const [hiddenCollections, normalCollections] = splitByPredicate(
+ const [hiddenCollections, collections] = splitByPredicate(
action.collections,
isHiddenCollection,
);
- const hiddenFileIDs = deriveHiddenFileIDs(hiddenFiles);
const archivedCollectionIDs =
- deriveArchivedCollectionIDs(normalCollections);
+ deriveArchivedCollectionIDs(collections);
const archivedFileIDs = deriveArchivedFileIDs(
archivedCollectionIDs,
- normalFiles,
+ collectionFiles,
);
const view = {
type: "albums" as const,
@@ -524,24 +514,19 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
...state,
user: action.user,
familyData: action.familyData,
- normalCollections,
+ collections,
hiddenCollections,
- lastSyncedFiles,
- lastSyncedNormalFiles,
- lastSyncedHiddenFiles,
+ lastSyncedCollectionFiles,
trashItems,
- files,
- normalFiles,
- hiddenFiles,
+ collectionFiles,
archivedCollectionIDs,
defaultHiddenCollectionIDs:
deriveDefaultHiddenCollectionIDs(hiddenCollections),
- hiddenFileIDs,
archivedFileIDs,
favoriteFileIDs: deriveFavoriteFileIDs(
action.user,
- normalCollections,
- normalFiles,
+ collections,
+ collectionFiles,
state.unsyncedFavoriteUpdates,
),
collectionNameByID: createCollectionNameByID(
@@ -551,14 +536,14 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
normalCollectionSummaries: deriveNormalCollectionSummaries(
action.user,
normalCollections,
- normalFiles,
+ collectionFiles,
trashItems,
archivedFileIDs,
),
hiddenCollectionSummaries: deriveHiddenCollectionSummaries(
action.user,
hiddenCollections,
- hiddenFiles,
+ collectionFiles,
),
uncategorizedCollectionSummaryID:
deriveUncategorizedCollectionSummaryID(normalCollections),
@@ -1150,10 +1135,10 @@ const deriveHiddenFileIDs = (hiddenFiles: EnteFile[]) =>
*/
const deriveArchivedFileIDs = (
archivedCollectionIDs: GalleryState["archivedCollectionIDs"],
- files: GalleryState["normalFiles"],
+ collectionFiles: GalleryState["collectionFiles"],
) =>
new Set(
- files
+ collectionFiles
.filter(
(file) =>
isArchivedFile(file) ||
@@ -1168,7 +1153,7 @@ const deriveArchivedFileIDs = (
const deriveFavoriteFileIDs = (
user: User,
collections: Collection[],
- files: EnteFile[],
+ collectionFiles: GalleryState["collectionFiles"],
unsyncedFavoriteUpdates: GalleryState["unsyncedFavoriteUpdates"],
) => {
let favoriteFileIDs = new Set<number>();
@@ -1176,8 +1161,8 @@ const deriveFavoriteFileIDs = (
// See: [Note: User and shared favorites]
if (collection.type == "favorites" && collection.owner.id == user.id) {
favoriteFileIDs = new Set(
- files
- .filter((file) => file.collectionID === collection.id)
+ collectionFiles
+ .filter((file) => file.collectionID == collection.id)
.map((file) => file.id),
);
break;
@@ -1209,14 +1194,14 @@ const createFileCollectionIDs = (files: EnteFile[]) =>
const deriveNormalCollectionSummaries = (
user: User,
normalCollections: Collection[],
- normalFiles: EnteFile[],
+ collectionFiles: GalleryState["collectionFiles"],
trashItems: TrashItem[],
archivedFileIDs: Set<number>,
) => {
const normalCollectionSummaries = createCollectionSummaries(
user,
normalCollections,
- normalFiles,
+ collectionFiles,
);
const uncategorizedCollection = normalCollections.find(
@@ -1234,8 +1219,9 @@ const deriveNormalCollectionSummaries = (
}
const allSectionFiles = findAllSectionVisibleFiles(
- normalFiles,
+ collectionFiles,
archivedFileIDs,
+ hidden,
);
normalCollectionSummaries.set(PseudoCollectionID.all, {
...pseudoCollectionOptionsForFiles(allSectionFiles),
@@ -1289,17 +1275,17 @@ const pseudoCollectionOptionsForLatestFileAndCount = (
const deriveHiddenCollectionSummaries = (
user: User,
hiddenCollections: Collection[],
- hiddenFiles: EnteFile[],
+ collectionFiles: GalleryState["collectionFiles"],
) => {
const hiddenCollectionSummaries = createCollectionSummaries(
user,
hiddenCollections,
- hiddenFiles,
+ collectionFiles,
);
const dhcIDs = findDefaultHiddenCollectionIDs(hiddenCollections);
const defaultHiddenFiles = uniqueFilesByID(
- hiddenFiles.filter((file) => dhcIDs.has(file.collectionID)),
+ collectionFiles.filter((file) => dhcIDs.has(file.collectionID)),
);
hiddenCollectionSummaries.set(PseudoCollectionID.hiddenItems, {
...pseudoCollectionOptionsForFiles(defaultHiddenFiles),
@@ -1325,11 +1311,11 @@ const deriveUncategorizedCollectionSummaryID = (
const createCollectionSummaries = (
user: User,
collections: Collection[],
- files: EnteFile[],
+ collectionFiles: GalleryState["collectionFiles"],
) => {
const collectionSummaries = new Map<number, CollectionSummary>();
- const filesByCollection = groupFilesByCollectionID(files);
+ const filesByCollection = groupFilesByCollectionID(collectionFiles);
const coverFiles = findCoverFiles(collections, filesByCollection);
for (const collection of collections) {
@@ -1448,8 +1434,7 @@ const createCollectionSummaries = (
coverFile: coverFiles.get(collection.id),
fileCount: collectionFiles?.length ?? 0,
updationTime: collection.updationTime,
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
- order: collection.magicMetadata?.data?.order ?? 0,
+ order: collection.magicMetadata?.data.order ?? 0,
});
}
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 2e5ad3221c..4297a1d35b 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -726,7 +726,7 @@ export const findDefaultHiddenCollectionIDs = (collections: Collection[]) =>
);
export const isHiddenCollection = (collection: Collection) =>
- collection.magicMetadata?.data.visibility === ItemVisibility.hidden;
+ collection.magicMetadata?.data.visibility == ItemVisibility.hidden;
/**
* Return true if this is a collection that the user doesn't own.
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index be60fd6b1b..ee16188295 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -43,19 +43,26 @@ export const getCollectionUpdationTime = async (): Promise<number> =>
* This function uses a delta diff, pulling only changes since the timestamp
* saved by the last pull.
*
- * @returns The latest list of collections, reflecting both the state in our
- * local database and on remote.
+ * @returns
+ *
+ * 1. The latest list of collections, reflecting both the state in our local
+ * database and on remote; and
+ *
+ * 2. The IDs of the collections that are hidden.
*/
-export const pullCollections = async (): Promise<Collection[]> => {
- const localCollections = await savedCollections();
+export const pullCollections = async (): Promise<{
+ collections: Collection[];
+ hiddenCollectionIDs: Set<number>;
+}> => {
+ const collections = await savedCollections();
+ const hiddenCollectionIDs = await savedHiddenCollectionIDs();
let sinceTime = await getCollectionUpdationTime();
const changes = await getCollections(sinceTime);
- if (!changes.length) return localCollections;
- const hiddenCollectionIDs = new Set(await savedHiddenCollectionIDs());
+ if (!changes.length) return { collections, hiddenCollectionIDs };
- const collectionsByID = new Map(localCollections.map((c) => [c.id, c]));
+ const collectionsByID = new Map(collections.map((c) => [c.id, c]));
for (const { id, updationTime, collection } of changes) {
sinceTime = Math.max(sinceTime, updationTime);
let removeSyncTime = false;
@@ -78,14 +85,19 @@ export const pullCollections = async (): Promise<Collection[]> => {
}
}
- const collections = [...collectionsByID.values()];
- const updatedHiddenCollectionIDs = collections
- .filter((collection) => isHiddenCollection(collection))
- .map((collection) => collection.id);
+ const updatedCollections = [...collectionsByID.values()];
+ const updatedHiddenCollectionIDs = new Set(
+ updatedCollections
+ .filter((collection) => isHiddenCollection(collection))
+ .map((collection) => collection.id),
+ );
- await localForage.setItem(COLLECTION_TABLE, collections);
+ await localForage.setItem(COLLECTION_TABLE, updatedCollections);
await localForage.setItem(COLLECTION_UPDATION_TIME, sinceTime);
await saveHiddenCollectionIDs(updatedHiddenCollectionIDs);
- return collections;
+ return {
+ collections: updatedCollections,
+ hiddenCollectionIDs: updatedHiddenCollectionIDs,
+ };
};
diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.ts
index e095533b5d..0a0e817160 100644
--- a/web/packages/new/photos/services/photos-fdb.ts
+++ b/web/packages/new/photos/services/photos-fdb.ts
@@ -48,23 +48,24 @@ export const saveCollections = async (collections: Collection[]) => {
/**
* Return the collectionIDs of hidden collections as per our local database.
*
- * The returned value is an array for storage simplicity, but should be
- * semantically be thought of as a set.
- *
* Use {@link saveHiddenCollectionIDs} to update the database.
*/
-export const savedHiddenCollectionIDs = async (): Promise<number[]> =>
- z
- .array(z.number())
- .parse((await localForage.getItem("hidden-collection-ids")) ?? []);
+export const savedHiddenCollectionIDs = async (): Promise<Set<number>> =>
+ new Set(
+ z
+ .array(z.number())
+ .parse((await localForage.getItem("hidden-collection-ids")) ?? []),
+ );
/**
* Replace the list of hidden collectionIDs stored in our local database.
*
* This is the setter corresponding to {@link savedHiddenCollectionIDs}.
*/
-export const saveHiddenCollectionIDs = async (hs: number[]): Promise<void> => {
- await localForage.setItem("hidden-collection-ids", hs);
+export const saveHiddenCollectionIDs = async (
+ hc: Set<number>,
+): Promise<void> => {
+ await localForage.setItem("hidden-collection-ids", [...hc]);
};
const TrashItemCollectionKey = z.object({
diff --git a/web/packages/new/photos/services/sync.ts b/web/packages/new/photos/services/sync.ts
index a6cd99429c..f3739766f6 100644
--- a/web/packages/new/photos/services/sync.ts
+++ b/web/packages/new/photos/services/sync.ts
@@ -68,20 +68,23 @@ interface PullFilesOpts {
* Called when the saved collections were replaced by the given
* {@link collections}.
*
- * The callback is also passed splits of {@link collections} into normal
- * ({@link normalCollections}) and hidden ({@link hiddenCollections}).
+ * It is also passed the IDs of the collections from amongst
+ * {@link collections} that are hidden.
*/
- onSetCollections: (collections: Collection[]) => void;
+ onSetCollections: (
+ collections: Collection[],
+ hiddenCollectionIDs: Set<number>,
+ ) => void;
/**
* Called when saved collection files were replaced by the given
- * {@link files}.
+ * {@link collectionFiles}.
*/
- onSetCollectionFiles: (files: EnteFile[]) => void;
+ onSetCollectionFiles: (collectionFiles: EnteFile[]) => void;
/**
* Called when saved collection files were augmented with the given newly
- * fetched {@link files}.
+ * fetched {@link collectionFiles}.
*/
- onAugmentCollectionFiles: (files: EnteFile[]) => void;
+ onAugmentCollectionFiles: (collectionFiles: EnteFile[]) => void;
/**
* Called when saved trashed items were replaced by the given
* {@link trashItems}.
@@ -107,8 +110,8 @@ interface PullFilesOpts {
* sync.
*/
export const syncCollectionAndFiles = async (opts?: PullFilesOpts) => {
- const collections = await pullCollections();
- opts?.onSetCollections(collections);
+ const { collections, hiddenCollectionIDs } = await pullCollections();
+ opts?.onSetCollections(collections, hiddenCollectionIDs);
const didUpdateFiles = await pullCollectionFiles(
collections,
opts?.onSetCollectionFiles,