Skip to content

armgit / ente

Public Imported from GitHub
0

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

Don't overload the word snapshot

Manav Rathi7f14ec6b34dd
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 4ce9eff380..0fd62f0d75 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -21,6 +21,7 @@ import {
} from "@/new/photos/components/gallery";
import type { GalleryBarMode } from "@/new/photos/components/gallery/BarImpl";
import { GalleryPeopleState } from "@/new/photos/components/gallery/PeopleHeader";
+import { usePeopleStateSnapshot } from "@/new/photos/components/utils/ml";
import { shouldShowWhatsNew } from "@/new/photos/services/changelog";
import type { CollectionSummaries } from "@/new/photos/services/collection/ui";
import { areOnlySystemCollections } from "@/new/photos/services/collection/ui";
@@ -30,7 +31,6 @@ import {
getLocalTrashedFiles,
sortFiles,
} from "@/new/photos/services/files";
-import { peopleSnapshot, peopleSubscribe } from "@/new/photos/services/ml";
import type { Person } from "@/new/photos/services/ml/people";
import {
filterSearchableFiles,
@@ -102,7 +102,6 @@ import {
useMemo,
useRef,
useState,
- useSyncExternalStore,
} from "react";
import { useDropzone } from "react-dropzone";
import {
@@ -323,10 +322,7 @@ export default function Gallery() {
// The ID of the currently selected person in the gallery bar (if any).
const [activePersonID, setActivePersonID] = useState<string | undefined>();
- const { people, visiblePeople } = useSyncExternalStore(
- peopleSubscribe,
- peopleSnapshot,
- );
+ const peopleState = usePeopleStateSnapshot();
const [isClipSearchResult, setIsClipSearchResult] =
useState<boolean>(false);
@@ -570,8 +566,8 @@ export default function Gallery() {
selectedSearchOption.suggestion,
);
} else if (barMode == "people") {
- let filteredPeople = people ?? [];
- let filteredVisiblePeople = visiblePeople ?? [];
+ let filteredPeople = peopleState?.people ?? [];
+ let filteredVisiblePeople = peopleState?.visiblePeople ?? [];
if (tempDeletedFileIds?.size ?? tempHiddenFileIds?.size) {
// Prune the in-memory temp updates from the actual state to
// obtain the UI state. Kept inside an preflight check to so
@@ -615,7 +611,7 @@ export default function Gallery() {
);
galleryPeopleState = {
activePerson,
- people: filteredPeople,
+ people: filteredVisiblePeople,
};
} else {
const baseFiles = barMode == "hidden-albums" ? hiddenFiles : files;
@@ -697,7 +693,7 @@ export default function Gallery() {
selectedSearchOption,
activeCollectionID,
archivedCollections,
- people,
+ peopleState,
activePersonID,
]);
@@ -1149,9 +1145,9 @@ export default function Gallery() {
return <div></div>;
}
- // `people` will be undefined only when ML is disabled, otherwise it'll be
- // an empty array (even if people are loading).
- const showPeopleSectionButton = people !== undefined;
+ // `peopleState` will be undefined only when ML is disabled, otherwise it'll
+ // be contain empty arrays (even if people are loading).
+ const showPeopleSectionButton = peopleState !== undefined;
return (
<GalleryContext.Provider
diff --git a/web/packages/new/photos/components/MLSettings.tsx b/web/packages/new/photos/components/MLSettings.tsx
index ceed79a90b..8959be3c6a 100644
--- a/web/packages/new/photos/components/MLSettings.tsx
+++ b/web/packages/new/photos/components/MLSettings.tsx
@@ -23,7 +23,7 @@ import React, { useEffect, useState } from "react";
import { Trans } from "react-i18next";
import { useAppContext } from "../types/context";
import { openURL } from "../utils/web";
-import { useMLStatus } from "./utils/ml";
+import { useMLStatusSnapshot } from "./utils/ml";
import { useWrapAsyncOperation } from "./utils/use-wrap-async";
export const MLSettings: React.FC<NestedDrawerVisibilityProps> = ({
@@ -31,7 +31,7 @@ export const MLSettings: React.FC<NestedDrawerVisibilityProps> = ({
onClose,
onRootClose,
}) => {
- const mlStatus = useMLStatus();
+ const mlStatus = useMLStatusSnapshot();
const [openFaceConsent, setOpenFaceConsent] = useState(false);
const handleRootClose = () => {
diff --git a/web/packages/new/photos/components/SearchBar.tsx b/web/packages/new/photos/components/SearchBar.tsx
index cfd32b61b9..6eea876114 100644
--- a/web/packages/new/photos/components/SearchBar.tsx
+++ b/web/packages/new/photos/components/SearchBar.tsx
@@ -38,7 +38,7 @@ import AsyncSelect from "react-select/async";
import { SearchPeopleList } from "./PeopleList";
import { UnstyledButton } from "./UnstyledButton";
import type { ButtonishProps } from "./mui";
-import { useMLStatus, useVisiblePeople } from "./utils/ml";
+import { useMLStatusSnapshot, usePeopleStateSnapshot } from "./utils/ml";
export interface SearchBarProps {
/**
@@ -377,8 +377,8 @@ const shouldShowEmptyState = (inputValue: string) => {
const EmptyState: React.FC<Pick<SearchBarProps, "onSelectPerson">> = ({
onSelectPerson,
}) => {
- const mlStatus = useMLStatus();
- const people = useVisiblePeople();
+ const mlStatus = useMLStatusSnapshot();
+ const people = usePeopleStateSnapshot()?.visiblePeople;
if (!mlStatus || mlStatus.phase == "disabled") {
// The preflight check should've prevented us from coming here.
diff --git a/web/packages/new/photos/components/gallery/index.tsx b/web/packages/new/photos/components/gallery/index.tsx
index fd071afbce..b4e89cb8fb 100644
--- a/web/packages/new/photos/components/gallery/index.tsx
+++ b/web/packages/new/photos/components/gallery/index.tsx
@@ -13,7 +13,7 @@ import { VerticallyCentered } from "@ente/shared/components/Container";
import { Typography } from "@mui/material";
import { t } from "i18next";
import React from "react";
-import { useMLStatus } from "../utils/ml";
+import { useMLStatusSnapshot } from "../utils/ml";
import { GalleryItemsHeaderAdapter, GalleryItemsSummary } from "./ListHeader";
/**
@@ -45,7 +45,7 @@ export const SearchResultsHeader: React.FC<SearchResultsHeaderProps> = ({
);
export const PeopleEmptyState: React.FC = () => {
- const mlStatus = useMLStatus();
+ const mlStatus = useMLStatusSnapshot();
const message =
mlStatus?.phase == "done"
diff --git a/web/packages/new/photos/components/utils/ml.ts b/web/packages/new/photos/components/utils/ml.ts
index 11054ea6b4..a3ab187c0a 100644
--- a/web/packages/new/photos/components/utils/ml.ts
+++ b/web/packages/new/photos/components/utils/ml.ts
@@ -2,20 +2,20 @@ import { useSyncExternalStore } from "react";
import {
mlStatusSnapshot,
mlStatusSubscribe,
- peopleSnapshot,
- peopleSubscribe,
+ peopleStateSnapshot,
+ peopleStateSubscribe,
} from "../../services/ml";
/**
- * A convenience hook that returns {@link mlStatusSnapshot}, subscribing to
- * updates.
+ * A convenience hook that returns {@link mlStatusSnapshot}, and also subscribes
+ * to updates.
*/
-export const useMLStatus = () =>
+export const useMLStatusSnapshot = () =>
useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);
/**
- * A convenience hook that returns visible people array from the
- * {@link peopleSnapshot}, and also subscribes to updates.
+ * A convenience hook that returns {@link peopleStateSnapshot}, and also
+ * subscribes to updates.
*/
-export const useVisiblePeople = () =>
- useSyncExternalStore(peopleSubscribe, peopleSnapshot)?.visiblePeople;
+export const usePeopleStateSnapshot = () =>
+ useSyncExternalStore(peopleStateSubscribe, peopleStateSnapshot);
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 21b45f6d39..ac44a6ecec 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -30,9 +30,9 @@ import { clearMLDB, getIndexableAndIndexedCounts, savedFaceIndex } from "./db";
import {
_applyPersonSuggestionUpdates,
filterNamedPeople,
- reconstructPeopleSnapshot,
+ reconstructPeopleState,
type CGroupPerson,
- type PeopleSnapshot,
+ type PeopleState,
type PersonSuggestionUpdates,
} from "./people";
import { MLWorker } from "./worker";
@@ -84,20 +84,20 @@ class MLState {
mlStatusSnapshot: MLStatus | undefined;
/**
- * Subscriptions to updates to the list of {@link Person}s we know about.
+ * Subscriptions to updates to the {@link PeopleState}.
*
- * See {@link peopleSubscribe}.
+ * See {@link peopleStateSubscribe}.
*/
- peopleListeners: (() => void)[] = [];
+ peopleStateListeners: (() => void)[] = [];
/**
- * Snapshot of the list of {@link Person}s and other pre-computed state. Use
- * the {@link peopleSnapshot} function to access this data.
+ * Snapshot of the {@link PeopleState}s. Use the {@link peopleStateSnapshot}
+ * function to access this data.
*
* It will be `undefined` only if ML is disabled. Otherwise, it will be an
* empty array even if the snapshot is pending its first sync.
*/
- peopleSnapshot: PeopleSnapshot | undefined;
+ peopleStateSnapshot: PeopleState | undefined;
/**
* In flight face crop regeneration promises indexed by the IDs of the files
@@ -195,7 +195,7 @@ export const isMLSupported = isDesktop;
*/
export const initML = () => {
_state.isMLEnabled = isMLEnabledLocal();
- resetPeopleSnapshot();
+ resetPeopleStateSnapshot();
};
export const logoutML = async () => {
@@ -232,7 +232,7 @@ export const enableML = async () => {
setIsMLEnabledLocal(true);
_state.isMLEnabled = true;
setInterimScheduledStatus();
- resetPeopleSnapshot();
+ resetPeopleStateSnapshot();
// Trigger updates, but don't wait for them to finish.
void updateMLStatusSnapshot().then(mlSync);
};
@@ -250,7 +250,7 @@ export const disableML = async () => {
_state.isSyncing = false;
await terminateMLWorker();
triggerStatusUpdate();
- resetPeopleSnapshot();
+ resetPeopleStateSnapshot();
};
/**
@@ -357,7 +357,7 @@ const updateClustersAndPeople = async () => {
await (await worker()).clusterFaces(masterKey);
// Update the people shown in the UI.
- await updatePeople();
+ await updatePeopleState();
};
/**
@@ -537,18 +537,18 @@ const workerDidUpdateStatus = throttled(updateMLStatusSnapshot, 2000);
/**
* A function that can be used to subscribe to updates to {@link Person}s.
*
- * This, along with {@link peopleSnapshot}, is meant to be used as arguments to
- * React's {@link useSyncExternalStore}.
+ * This, along with {@link peopleStateSnapshot}, is meant to be used as
+ * arguments to React's {@link useSyncExternalStore}.
*
* @param callback A function that will be invoked whenever the result of
- * {@link peopleSnapshot} changes.
+ * {@link peopleStateSnapshot} changes.
*
* @returns A function that can be used to clear the subscription.
*/
-export const peopleSubscribe = (onChange: () => void): (() => void) => {
- _state.peopleListeners.push(onChange);
+export const peopleStateSubscribe = (onChange: () => void): (() => void) => {
+ _state.peopleStateListeners.push(onChange);
return () => {
- _state.peopleListeners = _state.peopleListeners.filter(
+ _state.peopleStateListeners = _state.peopleStateListeners.filter(
(l) => l != onChange,
);
};
@@ -560,41 +560,41 @@ export const peopleSubscribe = (onChange: () => void): (() => void) => {
*
* Otherwise, if ML is disabled, set the people snapshot to `undefined`.
*/
-const resetPeopleSnapshot = () =>
- setPeopleSnapshot(
+const resetPeopleStateSnapshot = () =>
+ setPeopleStateSnapshot(
_state.isMLEnabled
? { people: [], visiblePeople: [], personByFaceID: new Map() }
: undefined,
);
/**
- * Return the last known, cached {@link people}.
+ * Return the last known, cached {@link PeopleState}.
*
- * This, along with {@link peopleSnapshot}, is meant to be used as arguments to
- * React's {@link useSyncExternalStore}.
+ * This, along with {@link peopleStateSubscribe}, is meant to be used as
+ * arguments to React's {@link useSyncExternalStore}.
*
* A return value of `undefined` indicates that ML is disabled. In all other
- * cases, the list will be either empty (if we're either still loading the
- * initial list of people, or if the user doesn't have any people), or, well,
- * non-empty.
+ * cases, the list of people will be either empty (if we're either still loading
+ * the initial list of people, or if the user doesn't have any people), or,
+ * well, non-empty.
*/
-export const peopleSnapshot = () => _state.peopleSnapshot;
+export const peopleStateSnapshot = () => _state.peopleStateSnapshot;
-// Update our, and the search subsystem's, snapshot of people by reconstructing
-// it from the latest local state.
-const updatePeople = async () => {
- const snapshot = await reconstructPeopleSnapshot();
+// Update our, and the search subsystem's, snapshot of people state by
+// reconstructing it from the latest local state.
+const updatePeopleState = async () => {
+ const state = await reconstructPeopleState();
// Notify the search subsystem of the update (search only uses named ones).
- setSearchPeople(filterNamedPeople(snapshot.visiblePeople));
+ setSearchPeople(filterNamedPeople(state.visiblePeople));
- // Update our in-memory list of people.
- setPeopleSnapshot(snapshot);
+ // Update our in-memory state.
+ setPeopleStateSnapshot(state);
};
-const setPeopleSnapshot = (snapshot: PeopleSnapshot | undefined) => {
- _state.peopleSnapshot = snapshot;
- _state.peopleListeners.forEach((l) => l());
+const setPeopleStateSnapshot = (snapshot: PeopleState | undefined) => {
+ _state.peopleStateSnapshot = snapshot;
+ _state.peopleStateListeners.forEach((l) => l());
};
/**
@@ -631,7 +631,7 @@ export const getAnnotatedFacesForFile = async (
const index = await savedFaceIndex(file.id);
if (!index) return [];
- const personByFaceID = _state.peopleSnapshot?.personByFaceID;
+ const personByFaceID = _state.peopleStateSnapshot?.personByFaceID;
if (!personByFaceID) return [];
const sortableFaces: [AnnotatedFaceID, number][] = [];
diff --git a/web/packages/new/photos/services/ml/people.ts b/web/packages/new/photos/services/ml/people.ts
index dc739dba06..fb9faa65cf 100644
--- a/web/packages/new/photos/services/ml/people.ts
+++ b/web/packages/new/photos/services/ml/people.ts
@@ -168,7 +168,10 @@ export interface PreviewableFace {
file: EnteFile;
}
-export interface PeopleSnapshot {
+/**
+ * Pre-computed in-memory state for UI that deals with people.
+ */
+export interface PeopleState {
/**
* List of all people.
*
@@ -196,14 +199,14 @@ export interface PeopleSnapshot {
}
/**
- * Construct an in-memory people snapshot using the data present locally,
- * ignoring faces belonging to deleted and hidden files.
+ * Construct an in-memory people state using the data present locally, ignoring
+ * faces belonging to deleted and hidden files.
*
* This function is meant to run after files, cgroups and faces have been synced
* with remote, and clustering has completed. It uses the current local state to
* construct an in-memory list of {@link Person}s on which the UI will operate.
*/
-export const reconstructPeopleSnapshot = async (): Promise<PeopleSnapshot> => {
+export const reconstructPeopleState = async (): Promise<PeopleState> => {
const files = await getLocalFiles("normal");
const fileByID = new Map(files.map((f) => [f.id, f]));