Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

refactor drag and drop pages

Kha Nguyenebdc8b9db200
diff --git a/apps/web/components/CollectionListing.tsx b/apps/web/components/CollectionListing.tsx
index 73c7930a..dee61ad3 100644
--- a/apps/web/components/CollectionListing.tsx
+++ b/apps/web/components/CollectionListing.tsx
@@ -300,8 +300,8 @@ const renderItem = (
<Droppable
id={`side-bar-collection-${collection.id}`}
data={{
- collectionName: collection.name,
- collectionId: collection.id,
+ name: collection.name,
+ id: collection.id,
ownerId: collection.ownerId,
}}
className="group"
diff --git a/apps/web/components/DragNDrop.tsx b/apps/web/components/DragNDrop.tsx
new file mode 100644
index 00000000..4a3a6e36
--- /dev/null
+++ b/apps/web/components/DragNDrop.tsx
@@ -0,0 +1,151 @@
+import {
+ DndContext,
+ DragEndEvent,
+ DragOverlay,
+ DragStartEvent,
+ MouseSensor,
+ SensorDescriptor,
+ SensorOptions,
+ TouchSensor,
+ useSensor,
+ useSensors,
+} from "@dnd-kit/core";
+import LinkIcon from "./LinkViews/LinkComponents/LinkIcon";
+import { LinkIncludingShortenedCollectionAndTags } from "@linkwarden/types";
+import toast from "react-hot-toast";
+import { useUpdateLink } from "@linkwarden/router/links";
+import { useTranslation } from "react-i18next";
+import { restrictToWindowEdges, snapCenterToCursor } from "@dnd-kit/modifiers";
+import { customCollisionDetectionAlgorithm } from "@/lib/utils";
+
+interface DragNDropProps {
+ children: React.ReactNode;
+ /**
+ * The currently active link being dragged
+ */
+ activeLink: LinkIncludingShortenedCollectionAndTags | null;
+ /**
+ * All links available for drag and drop
+ */
+ links: LinkIncludingShortenedCollectionAndTags[];
+ setActiveLink: (link: LinkIncludingShortenedCollectionAndTags | null) => void;
+ /**
+ * Override the default sensors used for drag and drop.
+ */
+ sensors?: SensorDescriptor<SensorOptions>[];
+
+ /**
+ * Override onDragEnd function.
+ */
+ onDragEnd?: (event: DragEndEvent) => void;
+}
+
+/**
+ * Wrapper component for drag and drop functionality.
+ */
+export default function DragNDrop({
+ children,
+ activeLink,
+ links,
+ setActiveLink,
+ sensors: sensorProp,
+ onDragEnd: onDragEndProp,
+}: DragNDropProps) {
+ const { t } = useTranslation();
+ const updateLink = useUpdateLink();
+ const mouseSensor = useSensor(MouseSensor, {
+ // Require the mouse to move by 10 pixels before activating
+ activationConstraint: {
+ distance: 10,
+ },
+ });
+ const touchSensor = useSensor(TouchSensor, {
+ // Press delay of 250ms, with tolerance of 5px of movement
+ activationConstraint: {
+ delay: 200,
+ tolerance: 5,
+ },
+ });
+
+ const sensors = useSensors(mouseSensor, touchSensor);
+
+ const handleDragStart = (event: DragStartEvent) => {
+ const draggedLink = links.find(
+ (link: any) => link.id === event.active.data.current?.linkId
+ );
+ setActiveLink(draggedLink || null);
+ };
+
+ const handleDragOverCancel = () => {
+ setActiveLink(null);
+ };
+
+ const handleDragEnd = async (event: DragEndEvent) => {
+ // If an onDragEnd prop is provided, use it instead of the default behavior
+ if (onDragEndProp) {
+ onDragEndProp(event);
+ return;
+ }
+ const { over } = event;
+ if (!over || !activeLink) return;
+
+ const collectionId = over.data.current?.collectionId as number;
+ const collectionName = over.data.current?.collectionName as string;
+ const ownerId = over.data.current?.ownerId as number;
+
+ // Immediately hide the drag overlay
+ setActiveLink(null);
+
+ // if the link dropped over the same collection, toast
+ if (activeLink.collection.id === collectionId) {
+ toast.error(t("link_already_in_collection"));
+ return;
+ }
+
+ const updatedLink: LinkIncludingShortenedCollectionAndTags = {
+ ...activeLink,
+ collection: {
+ id: collectionId,
+ name: collectionName,
+ ownerId,
+ },
+ };
+
+ const load = toast.loading(t("updating"));
+ await updateLink.mutateAsync(updatedLink, {
+ onSettled: (_, error) => {
+ toast.dismiss(load);
+ if (error) {
+ toast.error(error.message);
+ } else {
+ toast.success(t("updated"));
+ }
+ },
+ });
+ };
+ return (
+ <DndContext
+ onDragStart={handleDragStart}
+ onDragEnd={handleDragEnd}
+ onDragCancel={handleDragOverCancel}
+ modifiers={[restrictToWindowEdges, snapCenterToCursor]}
+ sensors={sensorProp ? sensorProp : sensors}
+ collisionDetection={customCollisionDetectionAlgorithm}
+ >
+ {!!activeLink && (
+ // when drag end, immediately hide the overlay
+ <DragOverlay
+ style={{
+ zIndex: 100,
+ pointerEvents: "none",
+ }}
+ >
+ <div className="w-fit h-fit">
+ <LinkIcon link={activeLink} />
+ </div>
+ </DragOverlay>
+ )}
+ {children}
+ </DndContext>
+ );
+}
diff --git a/apps/web/components/Droppable.tsx b/apps/web/components/Droppable.tsx
index d7e928d7..508f9f30 100644
--- a/apps/web/components/Droppable.tsx
+++ b/apps/web/components/Droppable.tsx
@@ -13,6 +13,7 @@ const Droppable = ({
collectionId?: string;
collectionName?: string;
ownerId?: string;
+ type?: "collection" | "tag";
};
className?: string;
}) => {
diff --git a/apps/web/pages/collections/[id].tsx b/apps/web/pages/collections/[id].tsx
index b5657e5d..87bba518 100644
--- a/apps/web/pages/collections/[id].tsx
+++ b/apps/web/pages/collections/[id].tsx
@@ -21,7 +21,7 @@ import { useTranslation } from "next-i18next";
import LinkListOptions from "@/components/LinkListOptions";
import { useCollections } from "@linkwarden/router/collections";
import { useUser } from "@linkwarden/router/user";
-import { useLinks, useUpdateLink } from "@linkwarden/router/links";
+import { useLinks } from "@linkwarden/router/links";
import Links from "@/components/LinkViews/Links";
import Icon from "@/components/Icon";
import CollectionCard from "@/components/CollectionCard";
@@ -36,25 +36,11 @@ import {
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
-import {
- DndContext,
- DragEndEvent,
- DragOverlay,
- DragStartEvent,
- MouseSensor,
- TouchSensor,
- useSensor,
- useSensors,
-} from "@dnd-kit/core";
-import toast from "react-hot-toast";
-import { customCollisionDetectionAlgorithm } from "@/lib/utils";
-import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
-import { snapCenterToCursor } from "@dnd-kit/modifiers";
+import DragNDrop from "@/components/DragNDrop";
export default function Index() {
const { t } = useTranslation();
const router = useRouter();
- const updateLink = useUpdateLink();
const { data: collections = [] } = useCollections();
@@ -70,22 +56,6 @@ export default function Index() {
const [activeLink, setActiveLink] =
useState<LinkIncludingShortenedCollectionAndTags | null>(null);
- const mouseSensor = useSensor(MouseSensor, {
- // Require the mouse to move by 10 pixels before activating
- activationConstraint: {
- distance: 10,
- },
- });
- const touchSensor = useSensor(TouchSensor, {
- // Press delay of 250ms, with tolerance of 5px of movement
- activationConstraint: {
- delay: 200,
- tolerance: 5,
- },
- });
-
- const sensors = useSensors(mouseSensor, touchSensor);
-
const [activeCollection, setActiveCollection] =
useState<CollectionIncludingMembersAndLinkCount>();
@@ -141,73 +111,13 @@ export default function Index() {
(localStorage.getItem("viewMode") as ViewMode) || ViewMode.Card
);
- const handleDragStart = (event: DragStartEvent) => {
- const draggedLink = links.find(
- (link: any) => link.id === event.active.data.current?.linkId
- );
- setActiveLink(draggedLink || null);
- };
-
- const handleDragOverCancel = () => {
- setActiveLink(null);
- };
-
- const handleDragEnd = async (event: DragEndEvent) => {
- const { over } = event;
- if (!over || !activeLink) return;
-
- const collectionId = over.data.current?.collectionId as number;
- const collectionName = over.data.current?.collectionName as string;
- const ownerId = over.data.current?.ownerId as number;
-
- // Immediately hide the drag overlay
- setActiveLink(null);
-
- // if the link dropped over the same collection, toast
- if (activeLink.collection.id === collectionId) {
- toast.error(t("link_already_in_collection"));
- return;
- }
-
- const updatedLink: LinkIncludingShortenedCollectionAndTags = {
- ...activeLink,
- collection: {
- id: collectionId,
- name: collectionName,
- ownerId,
- },
- };
-
- const load = toast.loading(t("updating"));
- await updateLink.mutateAsync(updatedLink, {
- onSettled: (_, error) => {
- toast.dismiss(load);
- if (error) {
- toast.error(error.message);
- } else {
- toast.success(t("updated"));
- }
- },
- });
- };
-
return (
- <DndContext
- onDragStart={handleDragStart}
- onDragEnd={handleDragEnd}
- onDragCancel={handleDragOverCancel}
- sensors={sensors}
- collisionDetection={customCollisionDetectionAlgorithm}
- modifiers={[snapCenterToCursor]}
+ <DragNDrop
+ links={links}
+ activeLink={activeLink}
+ setActiveLink={setActiveLink}
>
<MainLayout>
- {!!activeLink && (
- <DragOverlay className="z-50 pointer-events-none">
- <div className="w-fit h-fit">
- <LinkIcon link={activeLink} />
- </div>
- </DragOverlay>
- )}
<div
className="p-5 flex gap-3 flex-col"
style={{
@@ -494,7 +404,7 @@ export default function Index() {
</>
)}
</MainLayout>
- </DndContext>
+ </DragNDrop>
);
}
diff --git a/apps/web/pages/dashboard.tsx b/apps/web/pages/dashboard.tsx
index 3e0809a5..17c9c54c 100644
--- a/apps/web/pages/dashboard.tsx
+++ b/apps/web/pages/dashboard.tsx
@@ -28,23 +28,12 @@ import {
import ViewDropdown from "@/components/ViewDropdown";
import clsx from "clsx";
import Icon from "@/components/Icon";
-import {
- DndContext,
- DragEndEvent,
- DragStartEvent,
- DragOverlay,
- useSensor,
- MouseSensor,
- TouchSensor,
- useSensors,
-} from "@dnd-kit/core";
-import { restrictToWindowEdges, snapCenterToCursor } from "@dnd-kit/modifiers";
+import { DragEndEvent } from "@dnd-kit/core";
import Droppable from "@/components/Droppable";
import { useUpdateLink } from "@linkwarden/router/links";
import usePinLink from "@/lib/client/pinLink";
import { useQueryClient } from "@tanstack/react-query";
-import { customCollisionDetectionAlgorithm } from "@/lib/utils";
-import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
+import DragNDrop from "@/components/DragNDrop";
export default function Dashboard() {
const { t } = useTranslation();
@@ -70,22 +59,6 @@ export default function Dashboard() {
const pinLink = usePinLink();
const queryClient = useQueryClient();
- const mouseSensor = useSensor(MouseSensor, {
- // Require the mouse to move by 10 pixels before activating
- activationConstraint: {
- distance: 10,
- },
- });
- const touchSensor = useSensor(TouchSensor, {
- // Press delay of 250ms, with tolerance of 5px of movement
- activationConstraint: {
- delay: 200,
- tolerance: 5,
- },
- });
-
- const sensors = useSensors(mouseSensor, touchSensor);
-
const [numberOfLinks, setNumberOfLinks] = useState(0);
const [activeLink, setActiveLink] =
useState<LinkIncludingShortenedCollectionAndTags | null>(null);
@@ -175,18 +148,6 @@ export default function Dashboard() {
);
};
- const handleDragStart = (event: DragStartEvent) => {
- const draggedLink = allLinks.find(
- (link: any) => link.id === event.active.data.current?.linkId
- );
- setActiveLink(draggedLink || null);
- };
-
- const handleDragOverCancel = () => {
- // Reset the dropping collection when dragging is cancelled
- setActiveLink(null);
- };
-
const handleDragEnd = async (event: DragEndEvent) => {
const { over, active } = event;
if (!over || !activeLink) return;
@@ -288,28 +249,13 @@ export default function Dashboard() {
};
return (
- <DndContext
- onDragStart={handleDragStart}
+ <DragNDrop
onDragEnd={handleDragEnd}
- onDragCancel={handleDragOverCancel}
- modifiers={[restrictToWindowEdges, snapCenterToCursor]}
- sensors={sensors}
- collisionDetection={customCollisionDetectionAlgorithm}
+ links={allLinks}
+ activeLink={activeLink}
+ setActiveLink={setActiveLink}
>
<MainLayout>
- {!!activeLink && (
- // when drag end, immediately hide the overlay
- <DragOverlay
- style={{
- zIndex: 100,
- pointerEvents: "none",
- }}
- >
- <div className="w-fit h-fit">
- <LinkIcon link={activeLink} />
- </div>
- </DragOverlay>
- )}
<div className="p-5 flex flex-col gap-4 h-full">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
@@ -375,7 +321,7 @@ export default function Dashboard() {
<NewLinkModal onClose={() => setNewLinkModal(false)} />
)}
</MainLayout>
- </DndContext>
+ </DragNDrop>
);
}
diff --git a/apps/web/pages/links/index.tsx b/apps/web/pages/links/index.tsx
index 2650ebd8..23791286 100644
--- a/apps/web/pages/links/index.tsx
+++ b/apps/web/pages/links/index.tsx
@@ -11,22 +11,9 @@ import { useRouter } from "next/router";
import LinkListOptions from "@/components/LinkListOptions";
import getServerSideProps from "@/lib/client/getServerSideProps";
import { useTranslation } from "next-i18next";
-import { toast } from "react-hot-toast";
import Links from "@/components/LinkViews/Links";
import clsx from "clsx";
-import {
- DndContext,
- DragEndEvent,
- DragOverlay,
- DragStartEvent,
- MouseSensor,
- TouchSensor,
- useSensor,
- useSensors,
-} from "@dnd-kit/core";
-import { customCollisionDetectionAlgorithm } from "@/lib/utils";
-import { snapCenterToCursor } from "@dnd-kit/modifiers";
-import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
+import DragNDrop from "@/components/DragNDrop";
export default function Index() {
const [activeLink, setActiveLink] =
@@ -44,24 +31,6 @@ export default function Index() {
sort: sortBy,
});
- const updateLink = useUpdateLink();
-
- const mouseSensor = useSensor(MouseSensor, {
- // Require the mouse to move by 10 pixels before activating
- activationConstraint: {
- distance: 10,
- },
- });
- const touchSensor = useSensor(TouchSensor, {
- // Press delay of 250ms, with tolerance of 5px of movement
- activationConstraint: {
- delay: 200,
- tolerance: 5,
- },
- });
-
- const sensors = useSensors(mouseSensor, touchSensor);
-
const router = useRouter();
const [editMode, setEditMode] = useState(false);
@@ -70,73 +39,13 @@ export default function Index() {
if (editMode) return setEditMode(false);
}, [router]);
- const handleDragStart = (event: DragStartEvent) => {
- const draggedLink = links.find(
- (link: any) => link.id === event.active.data.current?.linkId
- );
- setActiveLink(draggedLink || null);
- };
-
- const handleDragOverCancel = () => {
- setActiveLink(null);
- };
-
- const handleDragEnd = async (event: DragEndEvent) => {
- const { over } = event;
- if (!over || !activeLink) return;
-
- const collectionId = over.data.current?.collectionId as number;
- const collectionName = over.data.current?.collectionName as string;
- const ownerId = over.data.current?.ownerId as number;
-
- // Immediately hide the drag overlay
- setActiveLink(null);
-
- // if the link dropped over the same collection, toast
- if (activeLink.collection.id === collectionId) {
- toast.error(t("link_already_in_collection"));
- return;
- }
-
- const updatedLink: LinkIncludingShortenedCollectionAndTags = {
- ...activeLink,
- collection: {
- id: collectionId,
- name: collectionName,
- ownerId,
- },
- };
-
- const load = toast.loading(t("updating"));
- await updateLink.mutateAsync(updatedLink, {
- onSettled: (_, error) => {
- toast.dismiss(load);
- if (error) {
- toast.error(error.message);
- } else {
- toast.success(t("updated"));
- }
- },
- });
- };
-
return (
- <DndContext
- onDragStart={handleDragStart}
- onDragEnd={handleDragEnd}
- onDragCancel={handleDragOverCancel}
- sensors={sensors}
- collisionDetection={customCollisionDetectionAlgorithm}
- modifiers={[snapCenterToCursor]}
+ <DragNDrop
+ links={links}
+ activeLink={activeLink}
+ setActiveLink={setActiveLink}
>
<MainLayout>
- {!!activeLink && (
- <DragOverlay className="z-50 pointer-events-none">
- <div className="w-fit h-fit">
- <LinkIcon link={activeLink} />
- </div>
- </DragOverlay>
- )}
<div className="p-5 flex flex-col gap-5 w-full h-full">
<LinkListOptions
t={t}
@@ -173,7 +82,7 @@ export default function Index() {
/>
</div>
</MainLayout>
- </DndContext>
+ </DragNDrop>
);
}
diff --git a/apps/web/pages/links/pinned.tsx b/apps/web/pages/links/pinned.tsx
index cd21efb4..6f8a8f3d 100644
--- a/apps/web/pages/links/pinned.tsx
+++ b/apps/web/pages/links/pinned.tsx
@@ -9,22 +9,9 @@ import {
import { useTranslation } from "next-i18next";
import getServerSideProps from "@/lib/client/getServerSideProps";
import LinkListOptions from "@/components/LinkListOptions";
-import { useLinks, useUpdateLink } from "@linkwarden/router/links";
+import { useLinks } from "@linkwarden/router/links";
import Links from "@/components/LinkViews/Links";
-import {
- DndContext,
- DragEndEvent,
- DragOverlay,
- DragStartEvent,
- MouseSensor,
- TouchSensor,
- useSensor,
- useSensors,
-} from "@dnd-kit/core";
-import toast from "react-hot-toast";
-import { snapCenterToCursor } from "@dnd-kit/modifiers";
-import { customCollisionDetectionAlgorithm } from "@/lib/utils";
-import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
+import DragNDrop from "@/components/DragNDrop";
export default function PinnedLinks() {
const { t } = useTranslation();
@@ -41,94 +28,17 @@ export default function PinnedLinks() {
pinnedOnly: true,
});
- const mouseSensor = useSensor(MouseSensor, {
- // Require the mouse to move by 10 pixels before activating
- activationConstraint: {
- distance: 10,
- },
- });
- const touchSensor = useSensor(TouchSensor, {
- // Press delay of 250ms, with tolerance of 5px of movement
- activationConstraint: {
- delay: 200,
- tolerance: 5,
- },
- });
-
- const sensors = useSensors(mouseSensor, touchSensor);
-
- const updateLink = useUpdateLink();
const [activeLink, setActiveLink] =
useState<LinkIncludingShortenedCollectionAndTags | null>(null);
const [editMode, setEditMode] = useState(false);
- const handleDragStart = (event: DragStartEvent) => {
- const draggedLink = links.find(
- (link: any) => link.id === event.active.data.current?.linkId
- );
- setActiveLink(draggedLink || null);
- };
-
- const handleDragOverCancel = () => {
- setActiveLink(null);
- };
-
- const handleDragEnd = async (event: DragEndEvent) => {
- const { over } = event;
- if (!over || !activeLink) return;
-
- const collectionId = over.data.current?.collectionId as number;
- const collectionName = over.data.current?.collectionName as string;
- const ownerId = over.data.current?.ownerId as number;
-
- // Immediately hide the drag overlay
- setActiveLink(null);
-
- // if the link dropped over the same collection, toast
- if (activeLink.collection.id === collectionId) {
- toast.error(t("link_already_in_collection"));
- return;
- }
-
- const updatedLink: LinkIncludingShortenedCollectionAndTags = {
- ...activeLink,
- collection: {
- id: collectionId,
- name: collectionName,
- ownerId,
- },
- };
-
- const load = toast.loading(t("updating"));
- await updateLink.mutateAsync(updatedLink, {
- onSettled: (_, error) => {
- toast.dismiss(load);
- if (error) {
- toast.error(error.message);
- } else {
- toast.success(t("updated"));
- }
- },
- });
- };
-
return (
- <DndContext
- onDragStart={handleDragStart}
- onDragEnd={handleDragEnd}
- onDragCancel={handleDragOverCancel}
- sensors={sensors}
- collisionDetection={customCollisionDetectionAlgorithm}
- modifiers={[snapCenterToCursor]}
+ <DragNDrop
+ links={links}
+ activeLink={activeLink}
+ setActiveLink={setActiveLink}
>
<MainLayout>
- {!!activeLink && (
- <DragOverlay className="z-50 pointer-events-none">
- <div className="w-fit h-fit">
- <LinkIcon link={activeLink} />
- </div>
- </DragOverlay>
- )}
<div className="p-5 flex flex-col gap-5 w-full h-full">
<LinkListOptions
t={t}
@@ -177,7 +87,7 @@ export default function PinnedLinks() {
/>
</div>
</MainLayout>
- </DndContext>
+ </DragNDrop>
);
}
diff --git a/apps/web/pages/tags/[id].tsx b/apps/web/pages/tags/[id].tsx
index f907119e..5e35ad20 100644
--- a/apps/web/pages/tags/[id].tsx
+++ b/apps/web/pages/tags/[id].tsx
@@ -7,7 +7,7 @@ import {
TagIncludingLinkCount,
ViewMode,
} from "@linkwarden/types";
-import { useLinks, useUpdateLink } from "@linkwarden/router/links";
+import { useLinks } from "@linkwarden/router/links";
import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal";
import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal";
import { useTranslation } from "next-i18next";
@@ -24,19 +24,7 @@ import {
DropdownMenuItem,
DropdownMenuSeparator,
} from "@/components/ui/dropdown-menu";
-import {
- DndContext,
- DragEndEvent,
- DragOverlay,
- DragStartEvent,
- MouseSensor,
- TouchSensor,
- useSensor,
- useSensors,
-} from "@dnd-kit/core";
-import { customCollisionDetectionAlgorithm } from "@/lib/utils";
-import { snapCenterToCursor } from "@dnd-kit/modifiers";
-import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
+import DragNDrop from "@/components/DragNDrop";
export default function Index() {
const { t } = useTranslation();
@@ -45,7 +33,6 @@ export default function Index() {
const { data: tags = [] } = useTags();
const updateTag = useUpdateTag();
const removeTag = useRemoveTag();
- const updateLink = useUpdateLink();
const [sortBy, setSortBy] = useState<Sort>(
Number(localStorage.getItem("sortBy")) ?? Sort.DateNewestFirst
@@ -62,21 +49,6 @@ export default function Index() {
const [activeLink, setActiveLink] =
useState<LinkIncludingShortenedCollectionAndTags | null>(null);
- const mouseSensor = useSensor(MouseSensor, {
- // Require the mouse to move by 10 pixels before activating
- activationConstraint: {
- distance: 10,
- },
- });
- const touchSensor = useSensor(TouchSensor, {
- // Press delay of 250ms, with tolerance of 5px of movement
- activationConstraint: {
- delay: 200,
- tolerance: 5,
- },
- });
-
- const sensors = useSensors(mouseSensor, touchSensor);
useEffect(() => {
if (editMode) return setEditMode(false);
}, [router]);
@@ -172,73 +144,13 @@ export default function Index() {
(localStorage.getItem("viewMode") as ViewMode) || ViewMode.Card
);
- const handleDragStart = (event: DragStartEvent) => {
- const draggedLink = links.find(
- (link: any) => link.id === event.active.data.current?.linkId
- );
- setActiveLink(draggedLink || null);
- };
-
- const handleDragOverCancel = () => {
- setActiveLink(null);
- };
-
- const handleDragEnd = async (event: DragEndEvent) => {
- const { over } = event;
- if (!over || !activeLink) return;
-
- const collectionId = over.data.current?.collectionId as number;
- const collectionName = over.data.current?.collectionName as string;
- const ownerId = over.data.current?.ownerId as number;
-
- // Immediately hide the drag overlay
- setActiveLink(null);
-
- // if the link dropped over the same collection, toast
- if (activeLink.collection.id === collectionId) {
- toast.error(t("link_already_in_collection"));
- return;
- }
-
- const updatedLink: LinkIncludingShortenedCollectionAndTags = {
- ...activeLink,
- collection: {
- id: collectionId,
- name: collectionName,
- ownerId,
- },
- };
-
- const load = toast.loading(t("updating"));
- await updateLink.mutateAsync(updatedLink, {
- onSettled: (_, error) => {
- toast.dismiss(load);
- if (error) {
- toast.error(error.message);
- } else {
- toast.success(t("updated"));
- }
- },
- });
- };
-
return (
- <DndContext
- onDragStart={handleDragStart}
- onDragEnd={handleDragEnd}
- onDragCancel={handleDragOverCancel}
- sensors={sensors}
- collisionDetection={customCollisionDetectionAlgorithm}
- modifiers={[snapCenterToCursor]}
+ <DragNDrop
+ links={links}
+ activeLink={activeLink}
+ setActiveLink={setActiveLink}
>
<MainLayout>
- {!!activeLink && (
- <DragOverlay className="z-50 pointer-events-none">
- <div className="w-fit h-fit">
- <LinkIcon link={activeLink} />
- </div>
- </DragOverlay>
- )}
<div className="p-5 flex flex-col gap-5 w-full">
<LinkListOptions
t={t}
@@ -339,7 +251,7 @@ export default function Index() {
<BulkEditLinksModal onClose={() => setBulkEditLinksModal(false)} />
)}
</MainLayout>
- </DndContext>
+ </DragNDrop>
);
}