Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add convenience hooks

Manav Rathi7506a35f1fa4
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 73b1b435f1..a5a99e0bb6 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -20,6 +20,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 { usePeople } 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";
@@ -29,7 +30,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,
@@ -103,7 +103,6 @@ import {
useMemo,
useRef,
useState,
- useSyncExternalStore,
} from "react";
import { useDropzone } from "react-dropzone";
import {
@@ -324,7 +323,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 = useSyncExternalStore(peopleSubscribe, peopleSnapshot);
+ const people = usePeople();
const [isClipSearchResult, setIsClipSearchResult] =
useState<boolean>(false);
diff --git a/web/packages/new/photos/components/MLSettings.tsx b/web/packages/new/photos/components/MLSettings.tsx
index 4099962db3..db2e6d5935 100644
--- a/web/packages/new/photos/components/MLSettings.tsx
+++ b/web/packages/new/photos/components/MLSettings.tsx
@@ -3,13 +3,7 @@ import { MenuItemGroup } from "@/base/components/Menu";
import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator";
import { Titlebar } from "@/base/components/Titlebar";
import type { NestedDrawerVisibilityProps } from "@/base/components/utils/modal";
-import {
- disableML,
- enableML,
- mlStatusSnapshot,
- mlStatusSubscribe,
- type MLStatus,
-} from "@/new/photos/services/ml";
+import { disableML, enableML, type MLStatus } from "@/new/photos/services/ml";
import { EnteMenuItem } from "@ente/shared/components/Menu/EnteMenuItem";
import {
Box,
@@ -25,18 +19,19 @@ import {
type DialogProps,
} from "@mui/material";
import { t } from "i18next";
-import React, { useEffect, useState, useSyncExternalStore } from "react";
+import React, { useEffect, useState } from "react";
import { Trans } from "react-i18next";
import { useAppContext } from "../types/context";
import { openURL } from "../utils/web";
import { useWrapAsyncOperation } from "./use-wrap-async";
+import { useMLStatus } from "./utils/ml";
export const MLSettings: React.FC<NestedDrawerVisibilityProps> = ({
open,
onClose,
onRootClose,
}) => {
- const mlStatus = useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);
+ const mlStatus = useMLStatus();
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 9414ef388c..d290feb903 100644
--- a/web/packages/new/photos/components/SearchBar.tsx
+++ b/web/packages/new/photos/components/SearchBar.tsx
@@ -1,13 +1,7 @@
import { assertionFailed } from "@/base/assert";
import { useIsSmallWidth } from "@/base/hooks";
import { ItemCard, PreviewItemTile } from "@/new/photos/components/Tiles";
-import {
- isMLSupported,
- mlStatusSnapshot,
- mlStatusSubscribe,
- peopleSnapshot,
- peopleSubscribe,
-} from "@/new/photos/services/ml";
+import { isMLSupported, mlStatusSnapshot } from "@/new/photos/services/ml";
import type { Person } from "@/new/photos/services/ml/people";
import { searchOptionsForString } from "@/new/photos/services/search";
import type { SearchOption } from "@/new/photos/services/search/types";
@@ -30,7 +24,7 @@ import {
} from "@mui/material";
import { t } from "i18next";
import pDebounce from "p-debounce";
-import React, { useMemo, useRef, useState, useSyncExternalStore } from "react";
+import React, { useMemo, useRef, useState } from "react";
import {
components as SelectComponents,
type ControlProps,
@@ -44,6 +38,7 @@ import AsyncSelect from "react-select/async";
import { SearchPeopleList } from "./PeopleList";
import { UnstyledButton } from "./UnstyledButton";
import type { ButtonishProps } from "./mui";
+import { useMLStatus, usePeople } from "./utils/ml";
export interface SearchBarProps {
/**
@@ -382,8 +377,8 @@ const shouldShowEmptyState = (inputValue: string) => {
const EmptyState: React.FC<Pick<SearchBarProps, "onSelectPerson">> = ({
onSelectPerson,
}) => {
- const mlStatus = useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);
- const people = useSyncExternalStore(peopleSubscribe, peopleSnapshot);
+ const mlStatus = useMLStatus();
+ const people = usePeople();
if (!mlStatus || mlStatus.phase == "disabled") {
// The preflight check should've prevented us from coming here.
diff --git a/web/packages/new/photos/components/gallery/PeopleHeader.tsx b/web/packages/new/photos/components/gallery/PeopleHeader.tsx
index 0c4b660987..b3fc5dee9f 100644
--- a/web/packages/new/photos/components/gallery/PeopleHeader.tsx
+++ b/web/packages/new/photos/components/gallery/PeopleHeader.tsx
@@ -31,6 +31,7 @@ import { useAppContext } from "../../types/context";
import { AddPersonDialog } from "../AddPersonDialog";
import { SpaceBetweenFlex } from "../mui";
import { SingleInputDialog } from "../SingleInputForm";
+import { usePeople } from "../utils/ml";
import type { GalleryBarImplProps } from "./BarImpl";
import { GalleryItemsHeaderAdapter, GalleryItemsSummary } from "./ListHeader";
@@ -236,9 +237,10 @@ const SuggestionsDialog: React.FC<SuggestionsDialogProps> = ({
person,
...rest
}) => {
+ const people = usePeople();
const isSmallWidth = useIsSmallWidth();
- console.log(person);
+ console.log({ open: rest.open, person, people });
return (
<Dialog
{...rest}
diff --git a/web/packages/new/photos/components/gallery/index.tsx b/web/packages/new/photos/components/gallery/index.tsx
index 84486b4839..ba2fe7ecc3 100644
--- a/web/packages/new/photos/components/gallery/index.tsx
+++ b/web/packages/new/photos/components/gallery/index.tsx
@@ -12,8 +12,8 @@ import type { SearchOption } from "@/new/photos/services/search/types";
import { VerticallyCentered } from "@ente/shared/components/Container";
import { Typography } from "@mui/material";
import { t } from "i18next";
-import React, { useSyncExternalStore } from "react";
-import { mlStatusSnapshot, mlStatusSubscribe } from "../../services/ml";
+import React from "react";
+import { useMLStatus } 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 = useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);
+ const mlStatus = useMLStatus();
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
new file mode 100644
index 0000000000..ced31eac62
--- /dev/null
+++ b/web/packages/new/photos/components/utils/ml.ts
@@ -0,0 +1,21 @@
+import { useSyncExternalStore } from "react";
+import {
+ mlStatusSnapshot,
+ mlStatusSubscribe,
+ peopleSnapshot,
+ peopleSubscribe,
+} from "../../services/ml";
+
+/**
+ * A convenience hook that returns {@link mlStatusSnapshot}, subscribing to
+ * updates.
+ */
+export const useMLStatus = () =>
+ useSyncExternalStore(mlStatusSubscribe, mlStatusSnapshot);
+
+/**
+ * A convenience hook that returns {@link peopleSnapshot}, subscribing to
+ * updates.
+ */
+export const usePeople = () =>
+ useSyncExternalStore(peopleSubscribe, peopleSnapshot);