Skip to content

armgit / ente

Public Imported from GitHub
0

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

new-ify

Manav Rathib97c2ccedb36
diff --git a/web/apps/photos/src/components/FilesDownloadProgress.tsx b/web/apps/photos/src/components/FilesDownloadProgress.tsx
index b063921be6..56fab9ba82 100644
--- a/web/apps/photos/src/components/FilesDownloadProgress.tsx
+++ b/web/apps/photos/src/components/FilesDownloadProgress.tsx
@@ -1,6 +1,6 @@
+import { Notification } from "@/new/photos/components/Notification";
import { photosDialogZIndex } from "@/new/photos/components/utils/z-index";
import { useAppContext } from "@/new/photos/types/context";
-import { Notification } from "components/Notification";
import { t } from "i18next";
import { GalleryContext } from "pages/gallery";
import { useContext } from "react";
diff --git a/web/apps/photos/src/components/utils/hooks-app.ts b/web/apps/photos/src/components/utils/hooks-app.ts
new file mode 100644
index 0000000000..dde3222e1c
--- /dev/null
+++ b/web/apps/photos/src/components/utils/hooks-app.ts
@@ -0,0 +1,30 @@
+import { type NotificationAttributes } from "@/new/photos/components/Notification";
+import { useCallback, useState } from "react";
+
+/**
+ * A React hook for simplifying the provisioning of a {@link showMiniDialog}
+ * function to inject in app contexts, and of the other props that are needed to
+ * be passed to the {@link AttributedMiniDialog}.
+ */
+export const useNotification = () => {
+ const [attributes, setAttributes] = useState<
+ NotificationAttributes | undefined
+ >(undefined);
+
+ const [open, setOpen] = useState(false);
+
+ const showNotification = useCallback(
+ (attributes: NotificationAttributes) => {
+ setAttributes(attributes);
+ setOpen(true);
+ },
+ [],
+ );
+
+ const handleClose = useCallback(() => setOpen(false), []);
+
+ return {
+ showNotification,
+ notificationProps: { open, onClose: handleClose, attributes },
+ };
+};
diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx
index 9eb90d0cac..d87c2a09a2 100644
--- a/web/apps/photos/src/pages/_app.tsx
+++ b/web/apps/photos/src/pages/_app.tsx
@@ -7,11 +7,11 @@ import {
useAttributedMiniDialog,
} from "@/base/components/utils/dialog";
import { useSetupI18n, useSetupLogs } from "@/base/components/utils/hooks-app";
-import { useModalVisibility } from "@/base/components/utils/modal";
import { THEME_COLOR, getTheme } from "@/base/components/utils/theme";
import log from "@/base/log";
import { logStartupBanner } from "@/base/log-web";
import { AppUpdate } from "@/base/types/ipc";
+import { Notification } from "@/new/photos/components/Notification";
import {
updateAvailableForDownloadDialogAttributes,
updateReadyToInstallDialogAttributes,
@@ -34,10 +34,7 @@ import "@fontsource-variable/inter";
import ArrowForwardIcon from "@mui/icons-material/ArrowForward";
import { CssBaseline } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
-import {
- Notification,
- type NotificationAttributes,
-} from "components/Notification";
+import { useNotification } from "components/utils/hooks-app";
import { t } from "i18next";
import type { AppProps } from "next/app";
import { useRouter } from "next/router";
@@ -55,18 +52,13 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
const isI18nReady = useSetupI18n();
const router = useRouter();
const { showMiniDialog, miniDialogProps } = useAttributedMiniDialog();
+ const { showNotification, notificationProps } = useNotification();
const { loadingBarRef, showLoadingBar, hideLoadingBar } = useLoadingBar();
const [themeColor, setThemeColor] = useLocalState(
LS_KEYS.THEME,
THEME_COLOR.DARK,
);
- const { show: showNotification, props: notificationVisibilityProps } =
- useModalVisibility();
- const [notificationAttributes, setNotificationAttributes] = useState<
- NotificationAttributes | undefined
- >(undefined);
-
const [loading, setLoading] = useState(false);
const [watchFolderView, setWatchFolderView] = useState(false);
const [watchFolderFiles, setWatchFolderFiles] = useState<FileList>(null);
@@ -96,7 +88,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
if (update.autoUpdatable) {
showMiniDialog(updateReadyToInstallDialogAttributes(update));
} else {
- setNotificationAttributes({
+ showNotification({
endIcon: <ArrowForwardIcon />,
variant: "secondary",
message: t("update_available"),
@@ -153,11 +145,6 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
});
}, []);
- useEffect(
- () => notificationAttributes && showNotification(),
- [notificationAttributes],
- );
-
const onGenericError = useCallback((e: unknown) => {
log.error(e);
// The generic error handler is sometimes called in the context of
@@ -180,10 +167,10 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
setWatchFolderView,
watchFolderFiles,
setWatchFolderFiles,
- setNotificationAttributes,
themeColor,
setThemeColor,
showMiniDialog,
+ showNotification,
onGenericError,
logout,
}),
@@ -194,6 +181,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
watchFolderFiles,
themeColor,
showMiniDialog,
+ showNotification,
onGenericError,
logout,
],
@@ -214,10 +202,7 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
{...miniDialogProps}
/>
- <Notification
- {...notificationVisibilityProps}
- attributes={notificationAttributes}
- />
+ <Notification {...notificationProps} />
<AppContext.Provider value={appContext}>
{(loading || !isI18nReady) && <LoadingOverlay />}
diff --git a/web/packages/base/components/utils/dialog.tsx b/web/packages/base/components/utils/dialog.tsx
index 36745e38ec..a8a4689db3 100644
--- a/web/packages/base/components/utils/dialog.tsx
+++ b/web/packages/base/components/utils/dialog.tsx
@@ -5,13 +5,13 @@ import type { MiniDialogAttributes } from "../MiniDialog";
/**
* A React hook for simplifying the provisioning of a {@link showMiniDialog}
- * function to inject in app contexts, and the other props that are needed for
- * to pass on to the {@link AttributedMiniDialog}.
+ * function to inject in app contexts, and of the other props that are needed to
+ * be passed to the {@link AttributedMiniDialog}.
*/
export const useAttributedMiniDialog = () => {
const [attributes, setAttributes] = useState<
MiniDialogAttributes | undefined
- >();
+ >(undefined);
const [open, setOpen] = useState(false);
@@ -20,15 +20,11 @@ export const useAttributedMiniDialog = () => {
setOpen(true);
}, []);
- const onCloseMiniDialog = useCallback(() => setOpen(false), []);
+ const handleClose = useCallback(() => setOpen(false), []);
return {
showMiniDialog,
- miniDialogProps: {
- open: open,
- onClose: onCloseMiniDialog,
- attributes: attributes,
- },
+ miniDialogProps: { open, onClose: handleClose, attributes },
};
};
diff --git a/web/apps/photos/src/components/Notification.tsx b/web/packages/new/photos/components/Notification.tsx
similarity index 95%
rename from web/apps/photos/src/components/Notification.tsx
rename to web/packages/new/photos/components/Notification.tsx
index e65c8b4305..01b889f6a0 100644
--- a/web/apps/photos/src/components/Notification.tsx
+++ b/web/packages/new/photos/components/Notification.tsx
@@ -1,3 +1,4 @@
+/* eslint-disable @typescript-eslint/no-unnecessary-condition */
import { EllipsizedTypography } from "@/base/components/Typography";
import { FilledIconButton } from "@/base/components/mui";
import type { ModalVisibilityProps } from "@/base/components/utils/modal";
@@ -9,11 +10,11 @@ import {
Button,
Snackbar,
Stack,
- SxProps,
- Theme,
type ButtonProps,
+ type SxProps,
+ type Theme,
} from "@mui/material";
-import React, { ReactNode } from "react";
+import React from "react";
/**
* Customize the contents of an {@link Notification}.
@@ -23,25 +24,25 @@ export type NotificationAttributes =
| TitleCaptionNotificationAttributes;
interface MessageSubTextNotificationAttributes {
- startIcon?: ReactNode;
+ startIcon?: React.ReactNode;
variant: ButtonProps["color"];
message?: React.JSX.Element | string;
subtext?: React.JSX.Element | string;
title?: never;
caption?: never;
onClick: () => void;
- endIcon?: ReactNode;
+ endIcon?: React.ReactNode;
}
interface TitleCaptionNotificationAttributes {
- startIcon?: ReactNode;
+ startIcon?: React.ReactNode;
variant: ButtonProps["color"];
title?: React.JSX.Element | string;
caption?: React.JSX.Element | string;
message?: never;
subtext?: never;
onClick: () => void;
- endIcon?: ReactNode;
+ endIcon?: React.ReactNode;
}
type NotificationProps = ModalVisibilityProps & {
diff --git a/web/packages/new/photos/types/context.ts b/web/packages/new/photos/types/context.ts
index e7d4b57e29..90eef58794 100644
--- a/web/packages/new/photos/types/context.ts
+++ b/web/packages/new/photos/types/context.ts
@@ -1,7 +1,7 @@
import type { AccountsContextT } from "@/accounts/types/context";
import { THEME_COLOR } from "@/base/components/utils/theme";
+import { type NotificationAttributes } from "@/new/photos/components/Notification";
import { createContext, useContext } from "react";
-import type { SetNotificationAttributes } from "./notification";
/**
* The type of the React context available to all pages in the photos app.
@@ -16,11 +16,15 @@ export type AppContextT = AccountsContextT & {
* Hide the global activity indicator bar.
*/
hideLoadingBar: () => void;
+ /**
+ * Show a {@link Notification}, customizing its contents and click behaviour
+ * using the provided {@link NotificationAttributes}.
+ */
+ showNotification: (attributes: NotificationAttributes) => void;
/**
* Show a generic error dialog, and log the given error.
*/
onGenericError: (error: unknown) => void;
- setNotificationAttributes: SetNotificationAttributes;
watchFolderView: boolean;
setWatchFolderView: (isOpen: boolean) => void;
watchFolderFiles: FileList;