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 (
- {document.isTemplate ? ( + {!isBulkAction && documents[0].isTemplate ? ( , }} /> ) : nestedDocumentsCount < 1 ? ( - , - }} - /> + ) : ( - , - }} - /> + )} - {canArchive && ( + {canArchiveAll && ( - - If you’d like the option of referencing or restoring the{" "} - {{ - noun: document.noun, - }}{" "} - in the future, consider archiving it instead. - + )} - {canArchive && ( + {canArchiveAll && ( diff --git a/app/scenes/DocumentMove.tsx b/app/scenes/DocumentMove.tsx index 5456723494..67c41dbf4e 100644 --- a/app/scenes/DocumentMove.tsx +++ b/app/scenes/DocumentMove.tsx @@ -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(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 { diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index b366401718..1bb49309ca 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -188,13 +188,6 @@ "{{ successCount }} archived, {{ errorCount }} failed": "{{ successCount }} archived, {{ errorCount }} failed", "Are you sure you want to archive {{ count }} documents? They will be removed from collections and search results.": "Are you sure you want to archive {{ count }} documents? They will be removed from collections and search results.", "Are you sure you want to archive {{ count }} documents? They will be removed from collections and search results._plural": "Are you sure you want to archive {{ count }} documents? 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 {{ count }} documents? This action will move them to the trash.": "Are you sure you want to delete {{ count }} documents? This action will move them to the trash.", - "Are you sure you want to delete {{ count }} documents? This action will move them to the trash._plural": "Are you sure you want to delete {{ count }} documents? 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 {{collectionName}} 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 {{collectionName}} collection is permanent and cannot be restored, however all published documents within will be moved to the trash.", "Also, {{collectionName}} is being used as the start view – deleting it will reset the start view to the Home page.": "Also, {{collectionName}} 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 {{ documentTitle }} template?": "Are you sure you want to delete the {{ documentTitle }} 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 {{ count }} documents? This action will delete all their history.": "Are you sure you want to delete these {{ count }} documents? This action will delete all their history.", + "Are you sure you want to delete these {{ count }} documents? This action will delete all their history._plural": "Are you sure you want to delete these {{ count }} documents? This action will delete all their history.", "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history.": "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history.", + "Are you sure about that? Deleting these {{ count }} documents will delete all their history and their combined {{ any }} nested documents..": "Are you sure about that? Deleting these {{ count }} documents will delete all their history and their combined {{ any }} nested documents..", + "Are you sure about that? Deleting these {{ count }} documents will delete all their history and their combined {{ any }} nested documents.._plural": "Are you sure about that? Deleting these {{ count }} documents will delete all their history and their combined {{ any }} nested documents..", "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and {{ any }} nested document.": "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and {{ any }} nested document.", "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and {{ any }} nested document._plural": "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and {{ any }} nested document.", - "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 {{ documentTitle }} template?": "Are you sure you want to delete the {{ documentTitle }} template?", "Select a location to move": "Select a location to move", "Document moved": "Document moved", "{{ count }} documents moved": "{{ count }} documents moved",