diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index 63aa5677cf..4f642bbaea 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -13,6 +13,7 @@ import Fade from "~/components/Fade"; import NudeButton from "~/components/NudeButton"; import useBoolean from "~/hooks/useBoolean"; import useStores from "~/hooks/useStores"; +import useToasts from "~/hooks/useToasts"; import DocumentMenu from "~/menus/DocumentMenu"; import { NavigationNode } from "~/types"; import { newDocumentPath } from "~/utils/routeHelpers"; @@ -56,7 +57,7 @@ function DocumentLink( const document = documents.get(node.id); const { fetchChildDocuments } = documents; const [isEditing, setIsEditing] = React.useState(false); - + const { showToast } = useToasts(); React.useEffect(() => { if (isActiveDocument && hasChildDocuments) { fetchChildDocuments(node.id); @@ -139,6 +140,7 @@ function DocumentLink( ...node, depth, active: isActiveDocument, + parentDocumentId: document?.parentDocumentId, collectionId: collection?.id || "", }), collect: (monitor) => ({ @@ -168,14 +170,28 @@ function DocumentLink( // Drop to re-parent const [{ isOverReparent, canDropToReparent }, dropToReparent] = useDrop({ accept: "document", - drop: (item: DragObject, monitor) => { + drop: async (item: DragObject, monitor) => { if (monitor.didDrop()) { return; } if (!collection) { return; } - documents.move(item.id, collection.id, node.id); + const undo = await documents.move(item.id, collection.id, node.id); + showToast(t("Document moved"), { + type: "info", + action: { + text: "undo", + onClick: async () => { + await documents.move( + item.id, + undo.collectionId, + undo.parentDocumentId, + undo.index + ); + }, + }, + }); }, canDrop: (_item, monitor) => !isDraft && diff --git a/app/components/Sidebar/components/SidebarLink.tsx b/app/components/Sidebar/components/SidebarLink.tsx index 9dbaf0d609..17758141a9 100644 --- a/app/components/Sidebar/components/SidebarLink.tsx +++ b/app/components/Sidebar/components/SidebarLink.tsx @@ -11,6 +11,7 @@ export type DragObject = NavigationNode & { depth: number; active: boolean; collectionId: string; + parentDocumentId?: string; }; type Props = Omit & { diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index f01c13058f..85111bb827 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -523,6 +523,7 @@ export default class DocumentsStore extends BaseStore { res.data.documents.forEach(this.add); res.data.collections.forEach(this.rootStore.collections.add); this.addPolicies(res.policies); + return res.undo; } finally { this.movingDocumentId = undefined; } diff --git a/server/commands/documentMover.ts b/server/commands/documentMover.ts index bad89d59d5..94225a4d71 100644 --- a/server/commands/documentMover.ts +++ b/server/commands/documentMover.ts @@ -62,6 +62,12 @@ type Result = { collectionChanged: boolean; }; +type UndoState = { + collectionId: string; + index?: number; + parentDocumentId: string | null; +}; + export default async function documentMover({ user, document, @@ -70,8 +76,14 @@ export default async function documentMover({ // convert undefined to null so parentId comparison treats them as equal index, ip, -}: Props): Promise { +}: Props): Promise<{ result: Result; undo?: UndoState }> { let transaction: Transaction | undefined; + const undoState: UndoState = { + collectionId: document.collectionId, + index: undefined, + parentDocumentId: document.parentDocumentId, + }; + const collectionChanged = collectionId !== document.collectionId; const previousCollectionId = document.collectionId; const result: Result = { @@ -82,7 +94,7 @@ export default async function documentMover({ if (document.template) { if (!collectionChanged) { - return result; + return { result }; } document.collectionId = collectionId; @@ -107,6 +119,7 @@ export default async function documentMover({ const documentJson = response?.[0]; const fromIndex = response?.[1] || 0; + undoState.index = fromIndex; // if we're reordering from within the same parent // the original and destination collection are the same, @@ -236,5 +249,5 @@ export default async function documentMover({ }); // we need to send all updated models back to the client - return result; + return { result, undo: undoState }; } diff --git a/server/routes/api/documents.ts b/server/routes/api/documents.ts index 103a892dc4..b8f3236b46 100644 --- a/server/routes/api/documents.ts +++ b/server/routes/api/documents.ts @@ -1163,7 +1163,7 @@ router.post("documents.move", auth(), async (ctx) => { authorize(user, "update", parent); } - const { documents, collections, collectionChanged } = await documentMover({ + const { result, undo } = await documentMover({ user, document, collectionId, @@ -1172,6 +1172,8 @@ router.post("documents.move", auth(), async (ctx) => { ip: ctx.request.ip, }); + const { documents, collections, collectionChanged } = result; + ctx.body = { data: { documents: await Promise.all( @@ -1182,6 +1184,7 @@ router.post("documents.move", auth(), async (ctx) => { ), }, policies: collectionChanged ? presentPolicies(user, documents) : [], + undo, }; }); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 7f195cf07b..d097d5158d 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -132,6 +132,7 @@ "Move document": "Move document", "Collections could not be loaded, please reload the app": "Collections could not be loaded, please reload the app", "Collections": "Collections", + "Document moved": "Document moved", "Untitled": "Untitled", "New nested document": "New nested document", "Document not supported – try Markdown, Plain text, HTML, or Word": "Document not supported – try Markdown, Plain text, HTML, or Word", @@ -399,7 +400,6 @@ "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and any nested documents.": "Are you sure about that? Deleting the {{ documentTitle }} document will delete all of its history and any nested documents.", "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.", "Archiving": "Archiving", - "Document moved": "Document moved", "Current location": "Current location", "Choose a new location": "Choose a new location", "Search collections & documents": "Search collections & documents",