use DocumentDelete component for bulk and single delete

This commit is contained in:
Salihu committed 2025-12-03 23:16:09 +01:00
1 parent 4704404105
commit 101a3f5137
7 files changed
+211 -180

No files matched your search

+1 -1
View File
@@ -1222,7 +1222,7 @@ export const deleteDocument = createActionV2({
}),
content: (
<DocumentDelete
document={document}
documents={[document]}
onSubmit={stores.dialogs.closeAllModals}
/>
),
-83
View File
@@ -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 (
<form onSubmit={handleSubmit}>
<Text as="p" type="secondary">
<Trans
count={documents.length}
defaults="Are you sure you want to delete <em>{{ count }} documents</em>? This action will move them to the trash."
values={{ count: documents.length }}
components={{ em: <strong /> }}
/>
</Text>
<Flex justify="flex-end" gap={8}>
<Button type="submit" danger>
{isDeleting ? `${t("Deleting")}…` : t("I'm sure – Delete")}
</Button>
</Flex>
</form>
);
}
export default observer(BulkDeleteDialog);
+14 -6
View File
@@ -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: (
<BulkDeleteDialog
<DocumentDelete
documents={selectedDocuments}
onSubmit={dialogs.closeAllModals}
onSubmit={() => {
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: <DocumentMove documents={selectedDocuments} />,
content: (
<DocumentMove
documents={selectedDocuments}
onSubmit={documents.clearSelection}
/>
),
});
},
[dialogs, selectedCount, selectedDocuments, t]
[dialogs, selectedCount, selectedDocuments, t, documents]
);
const sidebarWidth = ui.sidebarWidth;
@@ -26,7 +26,7 @@ function TrashLink() {
}),
content: (
<DocumentDelete
document={document}
documents={[document]}
onSubmit={dialogs.closeAllModals}
/>
),
+177 -75
View File
@@ -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 ? (
<Trans
count={documents.length}
defaults="Are you sure you want to delete these <em>{{ count }} documents</em>? This action will delete all their history."
values={{ count: documents.length }}
components={{ em: <strong /> }}
/>
) : (
<Trans
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>."
values={{
documentTitle: documents[0].titleWithDefault,
}}
components={{
em: <strong />,
}}
/>
);
const HasChildBody = () =>
isBulkAction ? (
<Trans
count={documents.length}
defaults="Are you sure about that? Deleting these <em>{{ count }} documents</em> will delete all their history and their combined <em>{{ any }} nested documents.</em>."
values={{ count: documents.length, any: nestedDocumentsCount }}
components={{
em: <strong />,
}}
/>
) : (
<Trans
count={nestedDocumentsCount}
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>."
values={{
documentTitle: documents[0].titleWithDefault,
any: nestedDocumentsCount,
}}
components={{
em: <strong />,
}}
/>
);
const ArchiveInsteadBody = () =>
isBulkAction ? (
<Trans>
If you’d like the option of referencing or restoring these documents in
the future, consider archiving them instead.
</Trans>
) : (
<Trans>
If you’d like the option of referencing or restoring the document in the
future, consider archiving it instead.
</Trans>
);
return (
<form onSubmit={handleSubmit}>
<Text as="p" type="secondary">
{document.isTemplate ? (
{!isBulkAction && documents[0].isTemplate ? (
<Trans
defaults="Are you sure you want to delete the <em>{{ documentTitle }}</em> template?"
values={{
documentTitle: document.titleWithDefault,
documentTitle: documents[0].titleWithDefault,
}}
components={{
em: <strong />,
}}
/>
) : nestedDocumentsCount < 1 ? (
<Trans
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>."
values={{
documentTitle: document.titleWithDefault,
}}
components={{
em: <strong />,
}}
/>
<NoChildBody />
) : (
<Trans
count={nestedDocumentsCount}
defaults="Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>."
values={{
documentTitle: document.titleWithDefault,
any: nestedDocumentsCount,
}}
components={{
em: <strong />,
}}
/>
<HasChildBody />
)}
</Text>
{canArchive && (
{canArchiveAll && (
<Text as="p" type="secondary">
<Trans>
If you’d like the option of referencing or restoring the{" "}
{{
noun: document.noun,
}}{" "}
in the future, consider archiving it instead.
</Trans>
<ArchiveInsteadBody />
</Text>
)}
<Flex justify="flex-end" gap={8}>
{canArchive && (
{canArchiveAll && (
<Button type="button" onClick={handleArchive} neutral>
{isArchiving ? `${t("Archiving")}…` : t("Archive")}
</Button>
+5 -5
View File
@@ -15,10 +15,11 @@ import useStores from "~/hooks/useStores";
type Props = {
documents: Document[];
onSubmit?: () => void;
};
function DocumentMove({ documents }: Props) {
const { dialogs, policies, documents: documentsStore } = useStores();
function DocumentMove({ documents, onSubmit }: Props) {
const { dialogs, policies } = useStores();
const { t } = useTranslation();
const collectionTrees = useCollectionTrees();
const [selectedPath, selectPath] = useState<NavigationNode | null>(null);
@@ -66,7 +67,7 @@ function DocumentMove({ documents }: Props) {
.map((node) => ({ ...node, children: [] }));
}
return nodes;
}, [policies, collectionTrees, documentIds, documents]);
}, [policies, collectionTrees, documentIds, documents, isBulkAction]);
const move = async () => {
if (!selectedPath) {
@@ -96,8 +97,7 @@ function DocumentMove({ documents }: Props) {
}
}
documentsStore.clearSelection();
onSubmit?.();
if (!isBulkAction) {
toast.success(t("Document moved"));
} else {
+13 -9
View File
@@ -188,13 +188,6 @@
"{{ successCount }} archived, {{ errorCount }} failed": "{{ successCount }} archived, {{ errorCount }} failed",
"Are you sure you want to archive <em>{{ count }} documents</em>? They will be removed from collections and search results.": "Are you sure you want to archive <em>{{ count }} documents</em>? They will be removed from collections and search results.",
"Are you sure you want to archive <em>{{ count }} documents</em>? They will be removed from collections and search results._plural": "Are you sure you want to archive <em>{{ count }} documents</em>? They will be removed from collections and search results.",
"{{ count }} documents deleted": "{{ count }} documents deleted",
"{{ count }} documents deleted_plural": "{{ count }} documents deleted",
"{{ successCount }} deleted, {{ errorCount }} failed": "{{ successCount }} deleted, {{ errorCount }} failed",
"Are you sure you want to delete <em>{{ count }} documents</em>? This action will move them to the trash.": "Are you sure you want to delete <em>{{ count }} documents</em>? This action will move them to the trash.",
"Are you sure you want to delete <em>{{ count }} documents</em>? This action will move them to the trash._plural": "Are you sure you want to delete <em>{{ count }} documents</em>? This action will move them to the trash.",
"Deleting": "Deleting",
"I'm sure – Delete": "I'm sure – Delete",
"Archive {{ count }} documents": "Archive {{ count }} documents",
"Archive {{ count }} documents_plural": "Archive {{ count }} documents",
"Delete {{ count }} documents": "Delete {{ count }} documents",
@@ -219,6 +212,7 @@
"Create": "Create",
"Collection deleted": "Collection deleted",
"I’m sure – Delete": "I’m sure – Delete",
"Deleting": "Deleting",
"Are you sure about that? Deleting the <em>{{collectionName}}</em> collection is permanent and cannot be restored, however all published documents within will be moved to the trash.": "Are you sure about that? Deleting the <em>{{collectionName}}</em> collection is permanent and cannot be restored, however all published documents within will be moved to the trash.",
"Also, <em>{{collectionName}}</em> is being used as the start view – deleting it will reset the start view to the Home page.": "Also, <em>{{collectionName}}</em> is being used as the start view – deleting it will reset the start view to the Home page.",
"Type a command or search": "Type a command or search",
@@ -792,11 +786,21 @@
"Observing {{ userName }}": "Observing {{ userName }}",
"Backlinks": "Backlinks",
"This document is large which may affect performance": "This document is large which may affect performance",
"Are you sure you want to delete the <em>{{ documentTitle }}</em> template?": "Are you sure you want to delete the <em>{{ documentTitle }}</em> template?",
"{{ successCount }} deleted, {{ errorCount }} failed": "{{ successCount }} deleted, {{ errorCount }} failed",
"{{ count }} documents deleted": "{{ count }} documents deleted",
"{{ count }} documents deleted_plural": "{{ count }} documents deleted",
"Document deleted.": "Document deleted.",
"Document archived.": "Document archived.",
"Are you sure you want to delete these <em>{{ count }} documents</em>? This action will delete all their history.": "Are you sure you want to delete these <em>{{ count }} documents</em>? This action will delete all their history.",
"Are you sure you want to delete these <em>{{ count }} documents</em>? This action will delete all their history._plural": "Are you sure you want to delete these <em>{{ count }} documents</em>? This action will delete all their history.",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>.": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>.",
"Are you sure about that? Deleting these <em>{{ count }} documents</em> will delete all their history and their combined <em>{{ any }} nested documents.</em>.": "Are you sure about that? Deleting these <em>{{ count }} documents</em> will delete all their history and their combined <em>{{ any }} nested documents.</em>.",
"Are you sure about that? Deleting these <em>{{ count }} documents</em> will delete all their history and their combined <em>{{ any }} nested documents.</em>._plural": "Are you sure about that? Deleting these <em>{{ count }} documents</em> will delete all their history and their combined <em>{{ any }} nested documents.</em>.",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>.": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>.",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>._plural": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>.",
"If you’d like the option of referencing or restoring the {{noun}} in the future, consider archiving it instead.": "If you’d like the option of referencing or restoring the {{noun}} in the future, consider archiving it instead.",
"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 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.": "If you’d like the option of referencing or restoring the document in the future, consider archiving it instead.",
"Are you sure you want to delete the <em>{{ documentTitle }}</em> template?": "Are you sure you want to delete the <em>{{ documentTitle }}</em> template?",
"Select a location to move": "Select a location to move",
"Document moved": "Document moved",
"{{ count }} documents moved": "{{ count }} documents moved",