Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web] Give gallery deletion its own TrashFilesDialog component

Aswin Asoke97d62437442
diff --git a/web/apps/photos/src/components/gallery/warning-red.png b/web/apps/photos/public/images/warning-red.png
similarity index 100%
rename from web/apps/photos/src/components/gallery/warning-red.png
rename to web/apps/photos/public/images/warning-red.png
diff --git a/web/apps/photos/src/components/SelectedFileOptions.tsx b/web/apps/photos/src/components/SelectedFileOptions.tsx
index 2580303a50..f4e31004b4 100644
--- a/web/apps/photos/src/components/SelectedFileOptions.tsx
+++ b/web/apps/photos/src/components/SelectedFileOptions.tsx
@@ -1,6 +1,5 @@
import type { CollectionSelectorAttributes } from "@/components/CollectionSelector";
import type { GalleryBarMode } from "@/components/gallery/reducer";
-import { trashFilesDialogAttributes } from "@/components/gallery/trash-files-dialog";
import {
getAvailableFileActions,
type FileContextAction,
@@ -59,6 +58,7 @@ interface SelectedFileOptionsProps {
selectedOwnFileCount: number;
selectedFavoriteCount: number;
onClearSelection: () => void;
+ onDelete: () => void;
onRemoveFilesFromCollection: (collection: Collection) => void;
onOpenCollectionSelector: (
attributes: CollectionSelectorAttributes,
@@ -83,6 +83,7 @@ export const SelectedFileOptions: React.FC<SelectedFileOptionsProps> = ({
selectedOwnFileCount,
selectedFavoriteCount,
onClearSelection,
+ onDelete,
onRemoveFilesFromCollection,
onOpenCollectionSelector,
createOnCreateForCollectionOp,
@@ -103,16 +104,6 @@ export const SelectedFileOptions: React.FC<SelectedFileOptionsProps> = ({
const handleArchive = createFileOpHandler("archive");
const handleUnarchive = createFileOpHandler("unarchive");
- const handleDelete = () =>
- showMiniDialog({
- ...trashFilesDialogAttributes(selectedOwnFileCount),
- continue: {
- text: t("move_to_trash"),
- color: "critical",
- action: createFileOpHandler("trash"),
- },
- });
-
const handleRestore = () =>
onOpenCollectionSelector({
action: "restore",
@@ -264,7 +255,7 @@ export const SelectedFileOptions: React.FC<SelectedFileOptionsProps> = ({
case "unhide":
return <UnhideButton key={action} onClick={handleUnhide} />;
case "trash":
- return <DeleteButton key={action} onClick={handleDelete} />;
+ return <DeleteButton key={action} onClick={onDelete} />;
case "deletePermanently":
return (
<DeletePermanentlyButton
diff --git a/web/apps/photos/src/components/gallery/TrashFilesDialog.tsx b/web/apps/photos/src/components/gallery/TrashFilesDialog.tsx
new file mode 100644
index 0000000000..9e43f8d46a
--- /dev/null
+++ b/web/apps/photos/src/components/gallery/TrashFilesDialog.tsx
@@ -0,0 +1,149 @@
+import CloseIcon from "@mui/icons-material/Close";
+import {
+ Box,
+ Dialog,
+ DialogContent,
+ DialogTitle,
+ IconButton,
+ Stack,
+ Typography,
+} from "@mui/material";
+import { InlineErrorIndicator } from "ente-base/components/ErrorIndicator";
+import { FocusVisibleButton } from "ente-base/components/mui/FocusVisibleButton";
+import { LoadingButton } from "ente-base/components/mui/LoadingButton";
+import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
+import log from "ente-base/log";
+import { t } from "i18next";
+import { useId, useState } from "react";
+
+type TrashFilesDialogProps = ModalVisibilityProps & {
+ count: number;
+ onConfirm: () => void | Promise<void>;
+};
+
+export const TrashFilesDialog: React.FC<TrashFilesDialogProps> = ({
+ open,
+ onClose,
+ count,
+ onConfirm,
+}) => {
+ const [phase, setPhase] = useState<"loading" | "failed" | undefined>();
+ const titleID = useId();
+ const messageID = useId();
+
+ const resetAndClose = () => {
+ setPhase(undefined);
+ onClose();
+ };
+
+ const handleClose = () => {
+ if (phase !== "loading") resetAndClose();
+ };
+
+ const handleConfirm = async () => {
+ setPhase("loading");
+ try {
+ await onConfirm();
+ resetAndClose();
+ } catch (e) {
+ log.error(e);
+ setPhase("failed");
+ }
+ };
+
+ return (
+ <Dialog
+ open={open}
+ onClose={handleClose}
+ aria-labelledby={titleID}
+ aria-describedby={messageID}
+ fullWidth
+ slotProps={{
+ paper: {
+ sx: {
+ maxWidth: "440px",
+ borderRadius: "24px",
+ position: "relative",
+ textAlign: "center",
+ "& .MuiButton-root": {
+ fontWeight: 500,
+ minHeight: "48px",
+ padding: "14px 24px",
+ borderRadius: "20px",
+ },
+ },
+ },
+ }}
+ >
+ <IconButton
+ aria-label={t("close")}
+ disabled={phase === "loading"}
+ onClick={handleClose}
+ sx={{
+ position: "absolute",
+ top: 16,
+ right: 16,
+ width: 36,
+ height: 36,
+ backgroundColor: "fill.faint",
+ }}
+ >
+ <CloseIcon sx={{ fontSize: 20 }} />
+ </IconButton>
+ <Stack sx={{ alignItems: "center", pt: "38px", px: 3.5 }}>
+ <Box
+ component="img"
+ src="/images/warning-red.png"
+ alt=""
+ sx={{ width: 68, height: 64, objectFit: "contain" }}
+ />
+ <DialogTitle
+ id={titleID}
+ sx={{ "&&&": { p: 0, mt: 3 }, fontSize: 20 }}
+ >
+ {t("trash_items_title", {
+ count,
+ fallbackLng: false,
+ defaultValue: t(
+ count === 1
+ ? "trash_file_title"
+ : "trash_files_title",
+ ),
+ })}
+ </DialogTitle>
+ </Stack>
+ <DialogContent sx={{ padding: "16px 28px 24px" }}>
+ <Typography id={messageID} sx={{ color: "text.muted" }}>
+ {t("trash_items_message", {
+ count,
+ fallbackLng: false,
+ defaultValue: t(
+ count === 1
+ ? "trash_file_message"
+ : "trash_files_message",
+ ),
+ })}
+ </Typography>
+ <Stack sx={{ pt: 3, gap: 1 }}>
+ {phase === "failed" && <InlineErrorIndicator />}
+ <LoadingButton
+ loading={phase === "loading"}
+ fullWidth
+ color="critical"
+ onClick={handleConfirm}
+ >
+ {t("move_to_trash")}
+ </LoadingButton>
+ <FocusVisibleButton
+ fullWidth
+ color="secondary"
+ disabled={phase === "loading"}
+ onClick={handleClose}
+ >
+ {t("cancel")}
+ </FocusVisibleButton>
+ </Stack>
+ </DialogContent>
+ </Dialog>
+ );
+};
diff --git a/web/apps/photos/src/components/gallery/trash-files-dialog.tsx b/web/apps/photos/src/components/gallery/trash-files-dialog.tsx
deleted file mode 100644
index 6f999724ad..0000000000
--- a/web/apps/photos/src/components/gallery/trash-files-dialog.tsx
+++ /dev/null
@@ -1,31 +0,0 @@
-/// <reference types="next/image-types/global" />
-
-import { Box } from "@mui/material";
-import type { MiniDialogAttributes } from "ente-base/components/MiniDialog";
-import { t } from "i18next";
-import warningRed from "./warning-red.png";
-
-export const trashFilesDialogAttributes = (
- count: number,
-): Pick<MiniDialogAttributes, "title" | "message" | "illustration"> => ({
- title: t("trash_items_title", {
- count,
- fallbackLng: false,
- defaultValue: t(count === 1 ? "trash_file_title" : "trash_files_title"),
- }),
- message: t("trash_items_message", {
- count,
- fallbackLng: false,
- defaultValue: t(
- count === 1 ? "trash_file_message" : "trash_files_message",
- ),
- }),
- illustration: (
- <Box
- component="img"
- src={warningRed.src}
- alt=""
- sx={{ width: 68, height: 64, objectFit: "contain" }}
- />
- ),
-});
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index b27477acc1..f76eca7c04 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -39,6 +39,7 @@ import {
NormalNavbarContents,
UploadButton,
} from "@/components/gallery/NormalNavbarContents";
+import { TrashFilesDialog } from "@/components/gallery/TrashFilesDialog";
import {
findCollectionCreatingIfNeeded,
performCollectionOp,
@@ -47,7 +48,6 @@ import {
useGalleryReducer,
type GalleryBarMode,
} from "@/components/gallery/reducer";
-import { trashFilesDialogAttributes } from "@/components/gallery/trash-files-dialog";
import { Sidebar } from "@/components/sidebar/Sidebar";
import {
notifyOthersFilesDialogAttributes,
@@ -192,6 +192,14 @@ const Page: React.FC = () => {
const [isFirstLoad, setIsFirstLoad] = useState(false);
const [isContextMenuOpen, setIsContextMenuOpen] = useState(false);
+ const [trashFilesDialog, setTrashFilesDialog] = useState<{
+ count: number;
+ onConfirm: () => void;
+ }>();
+ const {
+ show: showTrashFilesDialog,
+ props: trashFilesDialogVisibilityProps,
+ } = useModalVisibility();
const [suppressContextSelectionBar, setSuppressContextSelectionBar] =
useState(false);
const [selected, setSelected] = useState<SelectedState>({
@@ -1559,6 +1567,19 @@ const Page: React.FC = () => {
const selectedCount = selected.count;
const selectedOwnCount = selected.ownCount;
+ const handleDeleteFiles = useCallback(
+ (suppressSelectionBar = false) => {
+ if (selectedOwnCount === 0) return;
+ setTrashFilesDialog({
+ count: selectedOwnCount,
+ onConfirm: createFileOpHandler("trash", {
+ suppressSelectionBar,
+ }),
+ });
+ showTrashFilesDialog();
+ },
+ [selectedOwnCount, createFileOpHandler, showTrashFilesDialog],
+ );
const selectedFavoriteCount = useMemo(
() => countSelectedFavorites(selected, favoriteFileIDs),
[favoriteFileIDs, selected],
@@ -1636,16 +1657,7 @@ const Page: React.FC = () => {
createFileOpHandler("fixTime", { suppressSelectionBar })();
break;
case "trash":
- showMiniDialog({
- ...trashFilesDialogAttributes(selectedOwnCount),
- continue: {
- text: t("move_to_trash"),
- color: "critical",
- action: createFileOpHandler("trash", {
- suppressSelectionBar,
- }),
- },
- });
+ handleDeleteFiles(suppressSelectionBar);
break;
case "deletePermanently":
showMiniDialog({
@@ -1773,6 +1785,7 @@ const Page: React.FC = () => {
createOnSelectForCollectionOp,
handleOpenCollectionSelector,
handleRemoveFilesFromCollection,
+ handleDeleteFiles,
showMiniDialog,
showContextMenuAssignPerson,
showEditLocation,
@@ -1916,6 +1929,12 @@ const Page: React.FC = () => {
files={fixCreationTimeFiles}
onRemotePull={remotePull}
/>
+ {trashFilesDialog && (
+ <TrashFilesDialog
+ {...trashFilesDialogVisibilityProps}
+ {...trashFilesDialog}
+ />
+ )}
<NavbarBase
sx={[
{
@@ -1940,6 +1959,7 @@ const Page: React.FC = () => {
selectedOwnFileCount={selected.ownCount}
selectedFavoriteCount={selectedFavoriteCount}
onClearSelection={clearSelection}
+ onDelete={() => handleDeleteFiles()}
onRemoveFilesFromCollection={
handleRemoveFilesFromCollection
}
diff --git a/web/packages/base/components/MiniDialog.tsx b/web/packages/base/components/MiniDialog.tsx
index d2347556f0..2506167e9e 100644
--- a/web/packages/base/components/MiniDialog.tsx
+++ b/web/packages/base/components/MiniDialog.tsx
@@ -1,11 +1,9 @@
-import CloseIcon from "@mui/icons-material/Close";
import type { ButtonProps, ModalProps } from "@mui/material";
import {
Box,
Dialog,
DialogContent,
DialogTitle,
- IconButton,
Stack,
Typography,
type DialogProps,
@@ -20,7 +18,6 @@ import { InlineErrorIndicator } from "./ErrorIndicator";
export interface MiniDialogAttributes {
title?: React.ReactNode;
icon?: React.ReactNode;
- illustration?: React.ReactNode;
message?: React.ReactNode;
nonClosable?: boolean;
nonReplaceable?: boolean;
@@ -143,56 +140,9 @@ export const AttributedMiniDialog: React.FC<
{...{ open, sx }}
onClose={handleClose}
fullWidth
- slotProps={{
- paper: {
- sx: attributes.illustration
- ? {
- maxWidth: "440px",
- borderRadius: "24px",
- position: "relative",
- textAlign: "center",
- "& .MuiDialogContent-root": {
- padding: "16px 28px 24px",
- },
- "& .MuiButton-root": {
- fontWeight: 500,
- minHeight: "48px",
- padding: "14px 24px",
- borderRadius: "20px",
- },
- }
- : { maxWidth: "360px" },
- },
- }}
+ slotProps={{ paper: { sx: { maxWidth: "360px" } } }}
>
- {attributes.illustration ? (
- <Stack sx={{ alignItems: "center", pt: "38px", px: 3.5 }}>
- {!attributes.nonClosable && (
- <IconButton
- aria-label={t("close")}
- disabled={
- phase == "loading" ||
- phase == "secondary-loading"
- }
- onClick={resetPhaseAndClose}
- sx={{
- position: "absolute",
- top: 16,
- right: 16,
- width: 36,
- height: 36,
- backgroundColor: "fill.faint",
- }}
- >
- <CloseIcon sx={{ fontSize: 20 }} />
- </IconButton>
- )}
- {attributes.illustration}
- <DialogTitle sx={{ "&&&": { p: 0, mt: 3 }, fontSize: 20 }}>
- {attributes.title}
- </DialogTitle>
- </Stack>
- ) : (attributes.icon ?? attributes.title) ? (
+ {(attributes.icon ?? attributes.title) ? (
<Stack
direction="row"
sx={[