diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx
index 78e032674e..0496b338b0 100644
--- a/app/actions/definitions/documents.tsx
+++ b/app/actions/definitions/documents.tsx
@@ -1222,7 +1222,7 @@ export const deleteDocument = createActionV2({
}),
content: (
),
diff --git a/app/components/BulkDeleteDialog.tsx b/app/components/BulkDeleteDialog.tsx
deleted file mode 100644
index 018a47068c..0000000000
--- a/app/components/BulkDeleteDialog.tsx
+++ /dev/null
@@ -1,83 +0,0 @@
-import { observer } from "mobx-react";
-import * as React from "react";
-import { useTranslation, Trans } from "react-i18next";
-import { toast } from "sonner";
-import Document from "~/models/Document";
-import Button from "~/components/Button";
-import Flex from "~/components/Flex";
-import Text from "~/components/Text";
-import useStores from "~/hooks/useStores";
-
-type Props = {
- documents: Document[];
- onSubmit: () => void;
-};
-
-function BulkDeleteDialog({ documents, onSubmit }: Props) {
- const { t } = useTranslation();
- const { documents: documentsStore } = useStores();
- const [isDeleting, setDeleting] = React.useState(false);
-
- const handleSubmit = React.useCallback(
- async (ev: React.SyntheticEvent) => {
- ev.preventDefault();
- setDeleting(true);
-
- try {
- const results = await Promise.allSettled(
- documents.map((document) => document.delete())
- );
-
- const successCount = results.filter(
- (r) => r.status === "fulfilled"
- ).length;
- const errorCount = results.filter(
- (r) => r.status === "rejected"
- ).length;
-
- documentsStore.clearSelection();
-
- if (errorCount === 0) {
- toast.success(
- t("{{ count }} documents deleted", { count: successCount })
- );
- } else {
- toast.warning(
- t("{{ successCount }} deleted, {{ errorCount }} failed", {
- successCount,
- errorCount,
- })
- );
- }
-
- onSubmit();
- } catch (err) {
- toast.error(err.message);
- } finally {
- setDeleting(false);
- }
- },
- [onSubmit, documents, documentsStore, t]
- );
-
- return (
-
- );
-}
-
-export default observer(BulkDeleteDialog);
diff --git a/app/components/BulkSelectionToolbar.tsx b/app/components/BulkSelectionToolbar.tsx
index 120decb266..5cd81b2030 100644
--- a/app/components/BulkSelectionToolbar.tsx
+++ b/app/components/BulkSelectionToolbar.tsx
@@ -8,9 +8,9 @@ import { depths, s } from "@shared/styles";
import NudeButton from "~/components/NudeButton";
import Tooltip from "~/components/Tooltip";
import useStores from "~/hooks/useStores";
-import BulkDeleteDialog from "./BulkDeleteDialog";
import BulkArchiveDialog from "./BulkArchiveDialog";
import DocumentMove from "~/scenes/DocumentMove";
+import DocumentDelete from "~/scenes/DocumentDelete";
function BulkSelectionToolbar() {
const { t } = useTranslation();
@@ -61,14 +61,17 @@ function BulkSelectionToolbar() {
dialogs.openModal({
title: t("Delete {{ count }} documents", { count: selectedCount }),
content: (
- {
+ dialogs.closeAllModals();
+ documents.clearSelection();
+ }}
/>
),
});
},
- [dialogs, selectedCount, selectedDocuments, t]
+ [dialogs, selectedCount, selectedDocuments, t, documents]
);
const handleMove = React.useCallback(
@@ -77,10 +80,15 @@ function BulkSelectionToolbar() {
ev.stopPropagation();
dialogs.openModal({
title: t("Move {{ count }} documents", { count: selectedCount }),
- content: ,
+ content: (
+
+ ),
});
},
- [dialogs, selectedCount, selectedDocuments, t]
+ [dialogs, selectedCount, selectedDocuments, t, documents]
);
const sidebarWidth = ui.sidebarWidth;
diff --git a/app/components/Sidebar/components/TrashLink.tsx b/app/components/Sidebar/components/TrashLink.tsx
index 6cd7aedd2c..3e0c19d5ae 100644
--- a/app/components/Sidebar/components/TrashLink.tsx
+++ b/app/components/Sidebar/components/TrashLink.tsx
@@ -26,7 +26,7 @@ function TrashLink() {
}),
content: (
),
diff --git a/app/scenes/DocumentDelete.tsx b/app/scenes/DocumentDelete.tsx
index e0e899cb41..9b6c268780 100644
--- a/app/scenes/DocumentDelete.tsx
+++ b/app/scenes/DocumentDelete.tsx
@@ -15,70 +15,117 @@ import {
} from "~/utils/routeHelpers";
type Props = {
- document: Document;
+ documents: Document[];
onSubmit: () => void;
};
-function DocumentDelete({ document, onSubmit }: Props) {
+function DocumentDelete({ documents, onSubmit }: Props) {
const { t } = useTranslation();
- const { ui, documents, collections, userMemberships, groupMemberships } =
- useStores();
+ const {
+ ui,
+ documents: documentsStore,
+ collections: collectionsStore,
+ userMemberships,
+ groupMemberships,
+ } = useStores();
const history = useHistory();
const [isDeleting, setDeleting] = React.useState(false);
const [isArchiving, setArchiving] = React.useState(false);
- const canArchive =
- !document.isDraft && !document.isArchived && !document.template;
- const collection = document.collectionId
- ? collections.get(document.collectionId)
- : undefined;
- const nestedDocumentsCount = collection
- ? collection.getChildrenForDocument(document.id).length
- : 0;
+ const isBulkAction = documents.length > 1;
+ const canArchiveAll = documents.every(
+ (doc) => !doc.isDraft && !doc.isArchived && !doc.template
+ );
+
+ const nestedDocumentsCount = React.useMemo(
+ () =>
+ documents.reduce((total, doc) => {
+ const childrenCount = doc.childDocuments.length;
+ return total + childrenCount;
+ }, 0),
+ [documents]
+ );
+
+ // const collections = document.collectionId
+ // ? collectionsStore.get(document.collectionId)
+ // : undefined;
+
const handleSubmit = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
setDeleting(true);
try {
- await document.delete();
+ const failedIds: string[] = [];
+ let successCount = 0;
- userMemberships
- .getByDocumentId(document.id)
- ?.removeDocument(document.id);
- groupMemberships
- .getByDocumentId(document.id)
- ?.removeDocument(document.id);
-
- // only redirect if we're currently viewing the document that's deleted
- if (ui.activeDocumentId === document.id) {
- // If the document has a parent and it's available in the store then
- // redirect to it
- if (document.parentDocumentId) {
- const parent = documents.get(document.parentDocumentId);
-
- if (parent) {
- history.push(documentPath(parent));
- onSubmit();
- return;
- }
+ // Delete documents
+ for (const document of documents) {
+ try {
+ await document.delete();
+ userMemberships
+ .getByDocumentId(document.id)
+ ?.removeDocument(document.id);
+ groupMemberships
+ .getByDocumentId(document.id)
+ ?.removeDocument(document.id);
+ successCount++;
+ } catch {
+ failedIds.push(document.id);
}
-
- // If template, redirect to the template settings.
- // Otherwise redirect to the collection (or) home.
- const path = document.template
- ? settingsPath("templates")
- : collectionPath(collection?.path || "/");
- history.push(path);
}
- onSubmit();
+ // Show toast messages
+ if (isBulkAction) {
+ const message = failedIds.length
+ ? t("{{ successCount }} deleted, {{ errorCount }} failed", {
+ successCount,
+ errorCount: failedIds.length,
+ })
+ : t("{{ count }} documents deleted", { count: successCount });
+ failedIds.length ? toast.warning(message) : toast.success(message);
+ } else {
+ toast.success(t("Document deleted."));
+ }
+
+ // only redirect if we're currently viewing one of the documents that have been deleted
+ const activeDocument = documents.find(
+ (doc) => ui.activeDocumentId === doc.id
+ );
+ if (activeDocument && !failedIds.includes(activeDocument.id)) {
+ const parent = activeDocument.parentDocumentId
+ ? documentsStore.get(activeDocument.parentDocumentId)
+ : null;
+
+ const path = parent
+ ? documentPath(parent)
+ : activeDocument.template
+ ? settingsPath("templates")
+ : collectionPath(
+ collectionsStore.get(activeDocument.collectionId || "")
+ ?.path || "/"
+ );
+
+ history.push(path);
+ }
} catch (err) {
toast.error(err.message);
} finally {
+ onSubmit();
setDeleting(false);
}
},
- [onSubmit, ui, document, documents, history, collection]
+ [
+ documents,
+ userMemberships,
+ groupMemberships,
+ ui,
+ documentsStore,
+ collectionsStore,
+ history,
+ onSubmit,
+ isBulkAction,
+ t,
+ ]
);
const handleArchive = React.useCallback(
@@ -87,68 +134,123 @@ function DocumentDelete({ document, onSubmit }: Props) {
setArchiving(true);
try {
- await document.archive();
- onSubmit();
+ const results = await Promise.allSettled(
+ documents.map((doc) => doc.archive())
+ );
+
+ // Show toast messages
+ if (isBulkAction) {
+ const successCount = results.filter(
+ (r) => r.status === "fulfilled"
+ ).length;
+ const errorCount = results.filter(
+ (r) => r.status === "rejected"
+ ).length;
+
+ const message = errorCount
+ ? t("{{ successCount }} archived, {{ errorCount }} failed", {
+ successCount,
+ errorCount,
+ })
+ : t("{{ count }} documents archived", { count: successCount });
+ errorCount ? toast.warning(message) : toast.success(message);
+ } else {
+ toast.success(t("Document archived."));
+ }
} catch (err) {
toast.error(err.message);
} finally {
+ onSubmit();
setArchiving(false);
}
},
- [onSubmit, document]
+ [documents, onSubmit, isBulkAction, t]
);
+ const NoChildBody = () =>
+ isBulkAction ? (
+ }}
+ />
+ ) : (
+ ,
+ }}
+ />
+ );
+
+ const HasChildBody = () =>
+ isBulkAction ? (
+ ,
+ }}
+ />
+ ) : (
+ ,
+ }}
+ />
+ );
+
+ const ArchiveInsteadBody = () =>
+ isBulkAction ? (
+
+ If you’d like the option of referencing or restoring these documents in
+ the future, consider archiving them instead.
+
+ ) : (
+
+ If you’d like the option of referencing or restoring the document in the
+ future, consider archiving it instead.
+
+ );
+
return (