Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reducer 1

Manav Rathi36f41f526726
diff --git a/web/packages/new/photos/components/gallery/PeopleHeader.tsx b/web/packages/new/photos/components/gallery/PeopleHeader.tsx
index a53269d162..f62f365d8c 100644
--- a/web/packages/new/photos/components/gallery/PeopleHeader.tsx
+++ b/web/packages/new/photos/components/gallery/PeopleHeader.tsx
@@ -74,27 +74,6 @@ import { useWrapAsyncOperation } from "../utils/use-wrap-async";
import type { GalleryBarImplProps } from "./BarImpl";
import { GalleryItemsHeaderAdapter, GalleryItemsSummary } from "./ListHeader";
-/**
- * Derived UI state backing the gallery when it is in "people" mode.
- *
- * This may be different from the actual underlying state since there might be
- * unsynced data (hidden or deleted that have not yet been synced with remote)
- * that should be taken into account for the UI state.
- */
-export interface GalleryPeopleState {
- /**
- * The currently selected person, if any.
- *
- * Whenever this is present, it is guaranteed to be one of the items from
- * within {@link people}.
- */
- activePerson: Person | undefined;
- /**
- * The list of people to show.
- */
- people: Person[];
-}
-
type PeopleHeaderProps = Pick<
GalleryBarImplProps,
"people" | "onSelectPerson"
@@ -483,10 +462,10 @@ const initialSuggestionsDialogState: SuggestionsDialogState = {
updates: new Map(),
};
-const suggestionsDialogReducer = (
- state: SuggestionsDialogState,
- action: SuggestionsDialogAction,
-): SuggestionsDialogState => {
+const suggestionsDialogReducer: React.Reducer<
+ SuggestionsDialogState,
+ SuggestionsDialogAction
+> = (state, action) => {
switch (action.type) {
case "fetch":
return {
diff --git a/web/packages/new/photos/components/gallery/reducer.tsx b/web/packages/new/photos/components/gallery/reducer.tsx
new file mode 100644
index 0000000000..4348979391
--- /dev/null
+++ b/web/packages/new/photos/components/gallery/reducer.tsx
@@ -0,0 +1,72 @@
+/**
+ * @file code that really belongs to pages/gallery.tsx itself (or related
+ * files), but it written here in a separate file so that we can write in this
+ * package that has TypeScript strict mode enabled.
+ *
+ * Once the original gallery.tsx is strict mode, this code can be inlined back
+ * there.
+ */
+
+import type { EnteFile } from "@/media/file";
+import React, { useReducer } from "react";
+import type { Person } from "../../services/ml/people";
+
+/**
+ * Derived UI state backing the gallery.
+ *
+ * This might be different from the actual different from the actual underlying
+ * state since there might be unsynced data (hidden or deleted that have not yet
+ * been synced with remote) that should be temporarily taken into account for
+ * the UI state until the operation completes.
+ */
+export interface GalleryState {
+ filteredData: EnteFile[];
+ /**
+ * The currently selected person, if any.
+ *
+ * Whenever this is present, it is guaranteed to be one of the items from
+ * within {@link people}.
+ */
+ activePerson: Person | undefined;
+ /**
+ * The list of people to show.
+ */
+ people: Person[] | undefined;
+}
+
+// TODO: dummy actions for gradual migration to reducers
+export type GalleryAction =
+ | {
+ type: "set";
+ filteredData: EnteFile[];
+ galleryPeopleState:
+ | { activePerson: Person | undefined; people: Person[] }
+ | undefined;
+ }
+ | { type: "dummy" };
+
+const initialGalleryState: GalleryState = {
+ filteredData: [],
+ activePerson: undefined,
+ people: [],
+};
+
+const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
+ state,
+ action,
+) => {
+ switch (action.type) {
+ case "dummy":
+ return state;
+ case "set":
+ return {
+ ...state,
+ filteredData: action.filteredData,
+ activePerson: action.galleryPeopleState?.activePerson,
+ people: action.galleryPeopleState?.people,
+ };
+ }
+};
+
+export const useGalleryReducer = () =>
+ useReducer(galleryReducer, initialGalleryState);