diff --git a/app/components/Sidebar/components/CollectionLink.tsx b/app/components/Sidebar/components/CollectionLink.tsx index d0c3ff1902..ff2643c3cd 100644 --- a/app/components/Sidebar/components/CollectionLink.tsx +++ b/app/components/Sidebar/components/CollectionLink.tsx @@ -17,7 +17,7 @@ import useStores from "~/hooks/useStores"; import useToasts from "~/hooks/useToasts"; import CollectionMenu from "~/menus/CollectionMenu"; import CollectionSortMenu from "~/menus/CollectionSortMenu"; -import { NavigationNode, ToastOptions } from "~/types"; +import { ToastOptions } from "~/types"; import DocumentLink from "./DocumentLink"; import DropCursor from "./DropCursor"; import DropToImport from "./DropToImport"; @@ -34,28 +34,28 @@ type Props = { type MoveType = { documents: DocumentsStore; - documentId: string; - collectionId: string; - parentDocumentId?: string | null; - index?: number | null; + move: { + collectionId: string; + parentDocumentId?: string | null; + index?: number | null; + }; showToast: (message: string, options?: ToastOptions) => string | undefined; t: TFunction<"translation", undefined>; + item: DragObject; }; export const moveDocumentWithUndo = async ({ documents, - documentId, - collectionId, - parentDocumentId, - index, showToast, t, + move, + item, }: MoveType) => { - const undo = await documents.move( - documentId, - collectionId, - parentDocumentId, - index + await documents.move( + item.id, + move.collectionId, + move.parentDocumentId, + move.index ); showToast(t("Document moved"), { @@ -64,10 +64,10 @@ export const moveDocumentWithUndo = async ({ text: "undo", onClick: async () => { await documents.move( - documentId, - undo.collectionId, - undo.parentDocumentId, - undo.index + item.id, + item.collectionId, + item.parentDocumentId, + item.index ); }, }, @@ -91,9 +91,7 @@ function CollectionLink({ handlePermissionOpen, handlePermissionClose, ] = useBoolean(); - const itemRef = React.useRef< - NavigationNode & { depth: number; active: boolean; collectionId: string } - >(); + const itemRef = React.useRef(); const [isEditing, setIsEditing] = React.useState(false); const handleTitleChange = React.useCallback( @@ -123,16 +121,16 @@ function CollectionLink({ const [{ isOver, canDrop }, drop] = useDrop({ accept: "document", drop: async (item: DragObject, monitor) => { - const { id, collectionId } = item; + const { collectionId, parentDocumentId } = item; + if (monitor.didDrop()) { return; } - if (!collection) { + if (!collection || !item) { return; } - const document = documents.get(id); - if (collection.id === collectionId && !document?.parentDocumentId) { + if (collection.id === collectionId && !parentDocumentId) { return; } @@ -148,10 +146,12 @@ function CollectionLink({ } else { moveDocumentWithUndo({ documents, - documentId: id, - collectionId: collection.id, showToast, t, + item, + move: { + collectionId: collection.id, + }, }); } }, @@ -170,16 +170,19 @@ function CollectionLink({ const [{ isOverReorder }, dropToReorder] = useDrop({ accept: "document", drop: async (item: DragObject) => { - if (!collection) { + if (!collection || !item.id) { return; } + moveDocumentWithUndo({ documents, - documentId: item.id, - collectionId: collection.id, - index: 0, showToast, t, + move: { + collectionId: collection.id, + index: 0, + }, + item, }); }, collect: (monitor) => ({ diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index 79072a17dc..d6f632b9ca 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -143,6 +143,7 @@ function DocumentLink( active: isActiveDocument, parentDocumentId: document?.parentDocumentId, collectionId: collection?.id || "", + index, }), collect: (monitor) => ({ isDragging: !!monitor.isDragging(), @@ -175,16 +176,20 @@ function DocumentLink( if (monitor.didDrop()) { return; } + if (!collection) { return; } + moveDocumentWithUndo({ documents, - documentId: item.id, - collectionId: collection.id, - parentDocumentId: node.id, + move: { + collectionId: collection.id, + parentDocumentId: node.id, + }, showToast, t, + item, }); }, canDrop: (_item, monitor) => @@ -235,27 +240,19 @@ function DocumentLink( return; } - if (expanded) { - moveDocumentWithUndo({ - documents, - documentId: item.id, - collectionId: collection.id, - parentDocumentId: node.id, - showToast, - t, - index: 0, - }); - return; - } + const parentDocumentId = expanded ? node.id : parentId; + const droppedDocumentIndex = expanded ? 0 : index + 1; moveDocumentWithUndo({ documents, - documentId: item.id, - collectionId: collection.id, - parentDocumentId: parentId, showToast, t, - index: index + 1, + move: { + collectionId: collection.id, + parentDocumentId, + index: droppedDocumentIndex, + }, + item, }); }, collect: (monitor) => ({ diff --git a/app/components/Sidebar/components/SidebarLink.tsx b/app/components/Sidebar/components/SidebarLink.tsx index 17758141a9..67f78d269a 100644 --- a/app/components/Sidebar/components/SidebarLink.tsx +++ b/app/components/Sidebar/components/SidebarLink.tsx @@ -12,6 +12,7 @@ export type DragObject = NavigationNode & { active: boolean; collectionId: string; parentDocumentId?: string; + index: number; }; type Props = Omit & { diff --git a/app/scenes/DocumentReparent.tsx b/app/scenes/DocumentReparent.tsx index 7dd5dca7a8..091c0b27a0 100644 --- a/app/scenes/DocumentReparent.tsx +++ b/app/scenes/DocumentReparent.tsx @@ -7,26 +7,12 @@ import Button from "~/components/Button"; import Flex from "~/components/Flex"; import HelpText from "~/components/HelpText"; import { moveDocumentWithUndo } from "~/components/Sidebar/components/CollectionLink"; +import { DragObject } from "~/components/Sidebar/components/SidebarLink"; import useStores from "~/hooks/useStores"; import useToasts from "~/hooks/useToasts"; -import { NavigationNode } from "~/types"; type Props = { - item: - | { - active: boolean | null | undefined; - children: Array; - collectionId: string; - depth: number; - id: string; - title: string; - url: string; - } - | { - id: string; - collectionId: string; - title: string; - }; + item: DragObject; collection: Collection; onCancel: () => void; onSubmit: () => void; @@ -52,10 +38,12 @@ function DocumentReparent({ collection, item, onSubmit, onCancel }: Props) { try { await moveDocumentWithUndo({ documents, - documentId: item.id, - collectionId: collection.id, + move: { + collectionId: collection.id, + }, showToast, t, + item, }); onSubmit(); } catch (err) { @@ -66,7 +54,7 @@ function DocumentReparent({ collection, item, onSubmit, onCancel }: Props) { setIsSaving(false); } }, - [documents, item.id, collection.id, showToast, t, onSubmit] + [documents, collection.id, showToast, t, item, onSubmit] ); return (