From c1b2d3c4a702ab2c649befdcef78f66cd4787e00 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 19 Dec 2023 09:33:36 -0500 Subject: [PATCH] feat: Drag document into starred section to star --- app/components/Sidebar/components/Starred.tsx | 35 +++++++++++++--- .../Sidebar/components/StarredLink.tsx | 41 +++++++++++++++---- app/models/Document.ts | 2 +- app/stores/DocumentsStore.ts | 3 +- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/app/components/Sidebar/components/Starred.tsx b/app/components/Sidebar/components/Starred.tsx index 6ae3870df..8de2a2c7d 100644 --- a/app/components/Sidebar/components/Starred.tsx +++ b/app/components/Sidebar/components/Starred.tsx @@ -13,14 +13,14 @@ import DropCursor from "./DropCursor"; import Header from "./Header"; import PlaceholderCollections from "./PlaceholderCollections"; import Relative from "./Relative"; -import SidebarLink from "./SidebarLink"; +import SidebarLink, { DragObject } from "./SidebarLink"; import StarredContext from "./StarredContext"; import StarredLink from "./StarredLink"; const STARRED_PAGINATION_LIMIT = 10; function Starred() { - const { stars } = useStores(); + const { documents, stars } = useStores(); const { t } = useTranslation(); const { loading, next, end, error, page } = usePaginatedRequest( @@ -30,7 +30,7 @@ function Starred() { } ); - // Drop to reorder document + // Drop to reorder star const [{ isOverReorder, isDraggingAnyStar }, dropToReorder] = useDrop({ accept: "star", drop: async (item: { star: Star }) => { @@ -44,9 +44,25 @@ function Starred() { }), }); - if (error) { - toast.error(t("Could not load starred documents")); - } + // Drop to star document + const [{ documentIsOverReorder, isDraggingAnyDocument }, dropToStar] = + useDrop({ + accept: "document", + drop: async (item: DragObject) => { + const document = documents.get(item.id); + await document?.star(fractionalIndex(null, stars.orderedData[0].index)); + }, + collect: (monitor) => ({ + documentIsOverReorder: !!monitor.isOver(), + isDraggingAnyDocument: monitor.getItemType() === "document", + }), + }); + + React.useEffect(() => { + if (error) { + toast.error(t("Could not load starred documents")); + } + }, [t, error]); if (!stars.orderedData.length) { return null; @@ -64,6 +80,13 @@ function Starred() { position="top" /> )} + {isDraggingAnyDocument && ( + + )} {stars.orderedData .slice(0, page * STARRED_PAGINATION_LIMIT) .map((star) => ( diff --git a/app/components/Sidebar/components/StarredLink.tsx b/app/components/Sidebar/components/StarredLink.tsx index caa703eef..048892edc 100644 --- a/app/components/Sidebar/components/StarredLink.tsx +++ b/app/components/Sidebar/components/StarredLink.tsx @@ -21,7 +21,7 @@ import DocumentLink from "./DocumentLink"; import DropCursor from "./DropCursor"; import Folder from "./Folder"; import Relative from "./Relative"; -import SidebarLink from "./SidebarLink"; +import SidebarLink, { DragObject } from "./SidebarLink"; type Props = { star: Star; @@ -135,8 +135,39 @@ function StarredLink({ star }: Props) { }), }); + // Drop to star document + const [{ documentIsOverReorder, isDraggingAnyDocument }, dropToStar] = + useDrop({ + accept: "document", + drop: async (item: DragObject) => { + const next = star?.next(); + const document = documents.get(item.id); + await document?.star( + fractionalIndex(star?.index || null, next?.index || null) + ); + }, + collect: (monitor) => ({ + documentIsOverReorder: !!monitor.isOver(), + isDraggingAnyDocument: monitor.getItemType() === "document", + }), + }); + const displayChildDocuments = expanded && !isDragging; + const cursor = ( + <> + {isDraggingAny && ( + + )} + {isDraggingAnyDocument && ( + + )} + + ); + if (documentId) { const document = documents.get(documentId); if (!document) { @@ -200,9 +231,7 @@ function StarredLink({ star }: Props) { /> ))} - {isDraggingAny && ( - - )} + {cursor} ); @@ -225,9 +254,7 @@ function StarredLink({ star }: Props) { collection={collection} expanded={displayChildDocuments} /> - {isDraggingAny && ( - - )} + {cursor} ); diff --git a/app/models/Document.ts b/app/models/Document.ts index adaf0071e..48835e566 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -343,7 +343,7 @@ export default class Document extends ParanoidModel { }; @action - star = () => this.store.star(this); + star = (index?: string) => this.store.star(this, index); @action unstar = () => this.store.unstar(this); diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index 49e1d0bcf..fc01384f3 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -728,9 +728,10 @@ export default class DocumentsStore extends Store { }); }; - star = (document: Document) => + star = (document: Document, index?: string) => this.rootStore.stars.create({ documentId: document.id, + index, }); unstar = (document: Document) => {