Skip to content

armgit / ente

Public Imported from GitHub
0

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

refactor(photos):extract-gallery-authentication-hook

Aswin Asok0490119635ed
diff --git a/web/apps/photos/src/components/gallery/hooks/use-gallery-authentication.ts b/web/apps/photos/src/components/gallery/hooks/use-gallery-authentication.ts
new file mode 100644
index 0000000000..e6d367fb2e
--- /dev/null
+++ b/web/apps/photos/src/components/gallery/hooks/use-gallery-authentication.ts
@@ -0,0 +1,58 @@
+import { isDesktop } from "ente-base/app";
+import { useModalVisibility } from "ente-base/components/utils/modal";
+import { reauthenticateWithAppLock } from "ente-new/photos/services/app-lock";
+import { useCallback, useRef } from "react";
+
+export const useGalleryAuthentication = () => {
+ const {
+ show: showAuthenticateUser,
+ props: authenticateUserVisibilityProps,
+ } = useModalVisibility();
+
+ const onAuthenticateCallback = useRef<
+ ((didAuthenticate: boolean) => void) | undefined
+ >(undefined);
+
+ const authenticateUserWithPasswordModal = useCallback(
+ () =>
+ new Promise<boolean>((resolve) => {
+ onAuthenticateCallback.current = resolve;
+ showAuthenticateUser();
+ }),
+ [],
+ );
+
+ const authenticateUser = useCallback(async () => {
+ if (!isDesktop) return authenticateUserWithPasswordModal();
+
+ const reauthResult = await reauthenticateWithAppLock();
+ if (reauthResult === "authenticated") return true;
+ if (reauthResult === "cancelled") return false;
+
+ return authenticateUserWithPasswordModal();
+ }, [authenticateUserWithPasswordModal]);
+
+ const handleCloseAuthenticateUser = useCallback(() => {
+ authenticateUserVisibilityProps.onClose();
+ if (onAuthenticateCallback.current) {
+ onAuthenticateCallback.current(false);
+ onAuthenticateCallback.current = undefined;
+ }
+ }, [authenticateUserVisibilityProps.onClose]);
+
+ const handleAuthenticate = useCallback(() => {
+ if (onAuthenticateCallback.current) {
+ onAuthenticateCallback.current(true);
+ onAuthenticateCallback.current = undefined;
+ }
+ }, []);
+
+ return {
+ authenticateUser,
+ authenticateUserDialogProps: {
+ open: authenticateUserVisibilityProps.open,
+ onClose: handleCloseAuthenticateUser,
+ onAuthenticate: handleAuthenticate,
+ },
+ };
+};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index bdcabf5ce5..791eb25e9b 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -47,6 +47,7 @@ import {
performCollectionOp,
showCollectionSummaryWithAuthentication,
} from "@/components/gallery/helpers";
+import { useGalleryAuthentication } from "@/components/gallery/hooks/use-gallery-authentication";
import { useSubscriptionCompletionRedirect } from "@/components/gallery/hooks/use-subscription-completion-redirect";
import {
useGalleryReducer,
@@ -78,7 +79,6 @@ import {
import { stashRedirect } from "ente-accounts/services/redirect";
import { isSessionInvalid } from "ente-accounts/services/session";
import { ensureLocalUser } from "ente-accounts/services/user";
-import { isDesktop } from "ente-base/app";
import { Notification } from "ente-base/components/Notification";
import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
import { TranslucentLoadingOverlay } from "ente-base/components/loaders";
@@ -118,7 +118,6 @@ import {
useUserDetailsSnapshot,
} from "ente-new/photos/components/utils/use-snapshot";
import { masterKeyFromSession } from "ente-new/photos/services/account-keys";
-import { reauthenticateWithAppLock } from "ente-new/photos/services/app-lock";
import {
addToCollection,
addToFavoritesCollection,
@@ -297,10 +296,8 @@ const Page: React.FC = () => {
);
const { show: showFixCreationTime, props: fixCreationTimeVisibilityProps } =
useModalVisibility();
- const {
- show: showAuthenticateUser,
- props: authenticateUserVisibilityProps,
- } = useModalVisibility();
+ const { authenticateUser, authenticateUserDialogProps } =
+ useGalleryAuthentication();
const { show: showAlbumNameInput, props: albumNameInputVisibilityProps } =
useModalVisibility();
const { show: showEditLocation, props: editLocationVisibilityProps } =
@@ -337,44 +334,6 @@ const Page: React.FC = () => {
url?: string;
}>({ open: false });
- const onAuthenticateCallback = useRef<
- ((didAuthenticate: boolean) => void) | undefined
- >(undefined);
-
- const authenticateUserWithPasswordModal = useCallback(
- () =>
- new Promise<boolean>((resolve) => {
- onAuthenticateCallback.current = resolve;
- showAuthenticateUser();
- }),
- [],
- );
-
- const authenticateUser = useCallback(async () => {
- if (!isDesktop) return authenticateUserWithPasswordModal();
-
- const reauthResult = await reauthenticateWithAppLock();
- if (reauthResult === "authenticated") return true;
- if (reauthResult === "cancelled") return false;
-
- return authenticateUserWithPasswordModal();
- }, [authenticateUserWithPasswordModal]);
-
- const handleCloseAuthenticateUser = useCallback(() => {
- authenticateUserVisibilityProps.onClose();
- if (onAuthenticateCallback.current) {
- onAuthenticateCallback.current(false);
- onAuthenticateCallback.current = undefined;
- }
- }, [authenticateUserVisibilityProps.onClose]);
-
- const handleAuthenticate = useCallback(() => {
- if (onAuthenticateCallback.current) {
- onAuthenticateCallback.current(true);
- onAuthenticateCallback.current = undefined;
- }
- }, []);
-
const {
user,
favoriteFileIDs,
@@ -844,7 +803,7 @@ const Page: React.FC = () => {
uploadTypeSelectorView ||
openCollectionSelector ||
fixCreationTimeVisibilityProps.open ||
- authenticateUserVisibilityProps.open ||
+ authenticateUserDialogProps.open ||
albumNameInputVisibilityProps.open ||
editAlbumDetailsVisibilityProps.open ||
isFileViewerOpen ||
@@ -2533,11 +2492,7 @@ const Page: React.FC = () => {
onSubmit={handleEditAlbumDetails}
/>
)}
- <AuthenticateUser
- open={authenticateUserVisibilityProps.open}
- onClose={handleCloseAuthenticateUser}
- onAuthenticate={handleAuthenticate}
- />
+ <AuthenticateUser {...authenticateUserDialogProps} />
</FullScreenDropZone>
<SingleInputDialog
{...albumNameInputVisibilityProps}