Skip to content

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

feat(web): add drag-n-drop to collection page

Kha Nguyenc58f8f308619
diff --git a/apps/web/pages/collections/[id].tsx b/apps/web/pages/collections/[id].tsx
index 80c10f0f..b5657e5d 100644
--- a/apps/web/pages/collections/[id].tsx
+++ b/apps/web/pages/collections/[id].tsx
@@ -1,6 +1,7 @@
import {
AccountSettings,
CollectionIncludingMembersAndLinkCount,
+ LinkIncludingShortenedCollectionAndTags,
Sort,
ViewMode,
} from "@linkwarden/types";
@@ -20,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 } from "@linkwarden/router/links";
+import { useLinks, useUpdateLink } from "@linkwarden/router/links";
import Links from "@/components/LinkViews/Links";
import Icon from "@/components/Icon";
import CollectionCard from "@/components/CollectionCard";
@@ -35,10 +36,25 @@ 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";
export default function Index() {
const { t } = useTranslation();
const router = useRouter();
+ const updateLink = useUpdateLink();
const { data: collections = [] } = useCollections();
@@ -51,6 +67,25 @@ export default function Index() {
collectionId: Number(router.query.id),
});
+ 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>();
@@ -106,290 +141,360 @@ 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 (
- <MainLayout>
- <div
- className="p-5 flex gap-3 flex-col"
- style={{
- backgroundImage: `linear-gradient(${activeCollection?.color}20 0%, ${
- user?.theme === "dark" ? "#262626" : "#f3f4f6"
- } 13rem, ${user?.theme === "dark" ? "#171717" : "#ffffff"} 100%)`,
- }}
- >
- {activeCollection && (
- <div className="flex gap-3 items-start justify-between">
- <div className="flex items-center gap-2">
- {activeCollection.icon ? (
- <Icon
- icon={activeCollection.icon}
- size={45}
- weight={
- (activeCollection.iconWeight || "regular") as IconWeight
- }
- color={activeCollection.color}
- />
- ) : (
- <i
- className="bi-folder-fill text-3xl"
- style={{ color: activeCollection.color }}
- />
- )}
-
- <p className="sm:text-3xl text-2xl w-full py-1 break-words hyphens-auto font-thin">
- {activeCollection?.name}
- </p>
+ <DndContext
+ onDragStart={handleDragStart}
+ onDragEnd={handleDragEnd}
+ onDragCancel={handleDragOverCancel}
+ sensors={sensors}
+ collisionDetection={customCollisionDetectionAlgorithm}
+ modifiers={[snapCenterToCursor]}
+ >
+ <MainLayout>
+ {!!activeLink && (
+ <DragOverlay className="z-50 pointer-events-none">
+ <div className="w-fit h-fit">
+ <LinkIcon link={activeLink} />
</div>
-
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button
- asChild
- variant="ghost"
- size="icon"
- className="mt-2 text-neutral"
- onMouseDown={(e) => e.preventDefault()}
- title={t("more")}
- >
- <i className="bi-three-dots text-xl" />
- </Button>
- </DropdownMenuTrigger>
-
- <DropdownMenuContent
- sideOffset={4}
- align="end"
- className="bg-base-200 border border-neutral-content rounded-box p-1"
- >
- <DropdownMenuItem
- onClick={() => {
- for (const link of links) {
- if (link.url) window.open(link.url, "_blank");
+ </DragOverlay>
+ )}
+ <div
+ className="p-5 flex gap-3 flex-col"
+ style={{
+ backgroundImage: `linear-gradient(${activeCollection?.color}20 0%, ${
+ user?.theme === "dark" ? "#262626" : "#f3f4f6"
+ } 13rem, ${user?.theme === "dark" ? "#171717" : "#ffffff"} 100%)`,
+ }}
+ >
+ {activeCollection && (
+ <div className="flex gap-3 items-start justify-between">
+ <div className="flex items-center gap-2">
+ {activeCollection.icon ? (
+ <Icon
+ icon={activeCollection.icon}
+ size={45}
+ weight={
+ (activeCollection.iconWeight || "regular") as IconWeight
}
- }}
- >
- <i className="bi-box-arrow-up-right" />
- {t("open_all_links")}
- </DropdownMenuItem>
+ color={activeCollection.color}
+ />
+ ) : (
+ <i
+ className="bi-folder-fill text-3xl"
+ style={{ color: activeCollection.color }}
+ />
+ )}
+
+ <p className="sm:text-3xl text-2xl w-full py-1 break-words hyphens-auto font-thin">
+ {activeCollection?.name}
+ </p>
+ </div>
- {permissions === true && (
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ asChild
+ variant="ghost"
+ size="icon"
+ className="mt-2 text-neutral"
+ onMouseDown={(e) => e.preventDefault()}
+ title={t("more")}
+ >
+ <i className="bi-three-dots text-xl" />
+ </Button>
+ </DropdownMenuTrigger>
+
+ <DropdownMenuContent
+ sideOffset={4}
+ align="end"
+ className="bg-base-200 border border-neutral-content rounded-box p-1"
+ >
<DropdownMenuItem
- onClick={() => setEditCollectionModal(true)}
+ onClick={() => {
+ for (const link of links) {
+ if (link.url) window.open(link.url, "_blank");
+ }
+ }}
>
- <i className="bi-pencil-square" />
- {t("edit_collection_info")}
+ <i className="bi-box-arrow-up-right" />
+ {t("open_all_links")}
</DropdownMenuItem>
- )}
- <DropdownMenuItem
- onClick={() => setEditCollectionSharingModal(true)}
- >
- <i className="bi-globe" />
- {permissions === true
- ? t("share_and_collaborate")
- : t("view_team")}
- </DropdownMenuItem>
-
- {permissions === true && (
- <DropdownMenuItem onClick={() => setNewCollectionModal(true)}>
- <i className="bi-folder-plus" />
- {t("create_subcollection")}
+ {permissions === true && (
+ <DropdownMenuItem
+ onClick={() => setEditCollectionModal(true)}
+ >
+ <i className="bi-pencil-square" />
+ {t("edit_collection_info")}
+ </DropdownMenuItem>
+ )}
+
+ <DropdownMenuItem
+ onClick={() => setEditCollectionSharingModal(true)}
+ >
+ <i className="bi-globe" />
+ {permissions === true
+ ? t("share_and_collaborate")
+ : t("view_team")}
</DropdownMenuItem>
- )}
- <DropdownMenuSeparator />
+ {permissions === true && (
+ <DropdownMenuItem
+ onClick={() => setNewCollectionModal(true)}
+ >
+ <i className="bi-folder-plus" />
+ {t("create_subcollection")}
+ </DropdownMenuItem>
+ )}
+
+ <DropdownMenuSeparator />
+
+ <DropdownMenuItem
+ onClick={() => setDeleteCollectionModal(true)}
+ className="text-error"
+ >
+ {permissions === true ? (
+ <>
+ <i className="bi-trash" />
+ {t("delete_collection")}
+ </>
+ ) : (
+ <>
+ <i className="bi-box-arrow-left" />
+ {t("leave_collection")}
+ </>
+ )}
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ )}
- <DropdownMenuItem
- onClick={() => setDeleteCollectionModal(true)}
- className="text-error"
+ {activeCollection && (
+ <div className="min-w-[15rem]">
+ <div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
+ <div
+ className="flex items-center px-1 py-1 rounded-full cursor-pointer hover:bg-base-content/20 transition-colors duration-200"
+ onClick={() => setEditCollectionSharingModal(true)}
>
- {permissions === true ? (
- <>
- <i className="bi-trash" />
- {t("delete_collection")}
- </>
- ) : (
- <>
- <i className="bi-box-arrow-left" />
- {t("leave_collection")}
- </>
+ {collectionOwner.id && (
+ <ProfilePhoto
+ src={collectionOwner.image || undefined}
+ name={collectionOwner.name}
+ />
)}
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- </div>
- )}
-
- {activeCollection && (
- <div className="min-w-[15rem]">
- <div className="flex gap-1 justify-center sm:justify-end items-center w-fit">
- <div
- className="flex items-center px-1 py-1 rounded-full cursor-pointer hover:bg-base-content/20 transition-colors duration-200"
- onClick={() => setEditCollectionSharingModal(true)}
- >
- {collectionOwner.id && (
- <ProfilePhoto
- src={collectionOwner.image || undefined}
- name={collectionOwner.name}
- />
- )}
- {activeCollection.members
- .sort((a, b) => (a.userId as number) - (b.userId as number))
- .map((e, i) => {
- return (
- <ProfilePhoto
- key={i}
- src={e.user.image ? e.user.image : undefined}
- name={e.user.name}
- className="-ml-3"
- />
- );
- })
- .slice(0, 3)}
- {activeCollection.members.length - 3 > 0 && (
- <div className={`avatar drop-shadow-md placeholder -ml-3`}>
- <div className="bg-base-100 text-neutral rounded-full w-8 h-8 ring-2 ring-neutral-content">
- <span>+{activeCollection.members.length - 3}</span>
+ {activeCollection.members
+ .sort((a, b) => (a.userId as number) - (b.userId as number))
+ .map((e, i) => {
+ return (
+ <ProfilePhoto
+ key={i}
+ src={e.user.image ? e.user.image : undefined}
+ name={e.user.name}
+ className="-ml-3"
+ />
+ );
+ })
+ .slice(0, 3)}
+ {activeCollection.members.length - 3 > 0 && (
+ <div className={`avatar drop-shadow-md placeholder -ml-3`}>
+ <div className="bg-base-100 text-neutral rounded-full w-8 h-8 ring-2 ring-neutral-content">
+ <span>+{activeCollection.members.length - 3}</span>
+ </div>
</div>
- </div>
- )}
- </div>
+ )}
+ </div>
- <p className="text-neutral text-sm ml-2">
- {activeCollection.members.length > 0
- ? activeCollection.members.length === 1
- ? t("by_author_and_other", {
- author: collectionOwner.name,
- count: activeCollection.members.length,
- })
- : t("by_author_and_others", {
- author: collectionOwner.name,
- count: activeCollection.members.length,
- })
- : t("by_author", { author: collectionOwner.name })}
- </p>
+ <p className="text-neutral text-sm ml-2">
+ {activeCollection.members.length > 0
+ ? activeCollection.members.length === 1
+ ? t("by_author_and_other", {
+ author: collectionOwner.name,
+ count: activeCollection.members.length,
+ })
+ : t("by_author_and_others", {
+ author: collectionOwner.name,
+ count: activeCollection.members.length,
+ })
+ : t("by_author", { author: collectionOwner.name })}
+ </p>
+ </div>
</div>
- </div>
- )}
+ )}
- {activeCollection?.description && <p>{activeCollection.description}</p>}
+ {activeCollection?.description && (
+ <p>{activeCollection.description}</p>
+ )}
- <Separator />
+ <Separator />
- {collections.some((e) => e.parentId === activeCollection?.id) && (
- <>
- <PageHeader
- icon="bi-folder"
- title={t("collections")}
- description={t(
- collections.filter((e) => e.parentId === activeCollection?.id)
- .length === 1
- ? "showing_count_result"
- : "showing_count_results",
- {
- count: collections.filter(
- (e) => e.parentId === activeCollection?.id
- ).length,
- }
- )}
- className="scale-90 w-fit"
- />
- <div className="grid 2xl:grid-cols-4 xl:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-5">
- {collections
- .filter((e) => e.parentId === activeCollection?.id)
- .map((e) => (
- <CollectionCard key={e.id} collection={e} />
- ))}
- </div>
- </>
- )}
+ {collections.some((e) => e.parentId === activeCollection?.id) && (
+ <>
+ <PageHeader
+ icon="bi-folder"
+ title={t("collections")}
+ description={t(
+ collections.filter((e) => e.parentId === activeCollection?.id)
+ .length === 1
+ ? "showing_count_result"
+ : "showing_count_results",
+ {
+ count: collections.filter(
+ (e) => e.parentId === activeCollection?.id
+ ).length,
+ }
+ )}
+ className="scale-90 w-fit"
+ />
+ <div className="grid 2xl:grid-cols-4 xl:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-5">
+ {collections
+ .filter((e) => e.parentId === activeCollection?.id)
+ .map((e) => (
+ <CollectionCard key={e.id} collection={e} />
+ ))}
+ </div>
+ </>
+ )}
- <LinkListOptions
- t={t}
- viewMode={viewMode}
- setViewMode={setViewMode}
- sortBy={sortBy}
- setSortBy={setSortBy}
- editMode={
- permissions === true ||
- permissions?.canUpdate ||
- permissions?.canDelete
- ? editMode
- : undefined
- }
- setEditMode={
- permissions === true ||
- permissions?.canUpdate ||
- permissions?.canDelete
- ? setEditMode
- : undefined
- }
- links={links}
- >
- {collections.some((e) => e.parentId === activeCollection?.id) ? (
- <PageHeader
- icon={"bi-link-45deg"}
- title={t("links")}
- description={
- activeCollection?._count?.links === 1
+ <LinkListOptions
+ t={t}
+ viewMode={viewMode}
+ setViewMode={setViewMode}
+ sortBy={sortBy}
+ setSortBy={setSortBy}
+ editMode={
+ permissions === true ||
+ permissions?.canUpdate ||
+ permissions?.canDelete
+ ? editMode
+ : undefined
+ }
+ setEditMode={
+ permissions === true ||
+ permissions?.canUpdate ||
+ permissions?.canDelete
+ ? setEditMode
+ : undefined
+ }
+ links={links}
+ >
+ {collections.some((e) => e.parentId === activeCollection?.id) ? (
+ <PageHeader
+ icon={"bi-link-45deg"}
+ title={t("links")}
+ description={
+ activeCollection?._count?.links === 1
+ ? t("showing_count_result", {
+ count: activeCollection?._count?.links,
+ })
+ : t("showing_count_results", {
+ count: activeCollection?._count?.links,
+ })
+ }
+ className="scale-90 w-fit"
+ />
+ ) : (
+ <p>
+ {activeCollection?._count?.links === 1
? t("showing_count_result", {
count: activeCollection?._count?.links,
})
: t("showing_count_results", {
count: activeCollection?._count?.links,
- })
- }
- className="scale-90 w-fit"
- />
- ) : (
- <p>
- {activeCollection?._count?.links === 1
- ? t("showing_count_result", {
- count: activeCollection?._count?.links,
- })
- : t("showing_count_results", {
- count: activeCollection?._count?.links,
- })}
- </p>
- )}
- </LinkListOptions>
-
- <Links
- editMode={editMode}
- links={links}
- layout={viewMode}
- placeholderCount={1}
- useData={data}
- />
- {!data.isLoading && links && !links[0] && <NoLinksFound />}
- </div>
- {activeCollection && (
- <>
- {editCollectionModal && (
- <EditCollectionModal
- onClose={() => setEditCollectionModal(false)}
- activeCollection={activeCollection}
- />
- )}
- {editCollectionSharingModal && (
- <EditCollectionSharingModal
- onClose={() => setEditCollectionSharingModal(false)}
- activeCollection={activeCollection}
- />
- )}
- {newCollectionModal && (
- <NewCollectionModal
- onClose={() => setNewCollectionModal(false)}
- parent={activeCollection}
- />
- )}
- {deleteCollectionModal && (
- <DeleteCollectionModal
- onClose={() => setDeleteCollectionModal(false)}
- activeCollection={activeCollection}
- />
- )}
- </>
- )}
- </MainLayout>
+ })}
+ </p>
+ )}
+ </LinkListOptions>
+
+ <Links
+ editMode={editMode}
+ links={links}
+ layout={viewMode}
+ placeholderCount={1}
+ useData={data}
+ />
+ {!data.isLoading && links && !links[0] && <NoLinksFound />}
+ </div>
+ {activeCollection && (
+ <>
+ {editCollectionModal && (
+ <EditCollectionModal
+ onClose={() => setEditCollectionModal(false)}
+ activeCollection={activeCollection}
+ />
+ )}
+ {editCollectionSharingModal && (
+ <EditCollectionSharingModal
+ onClose={() => setEditCollectionSharingModal(false)}
+ activeCollection={activeCollection}
+ />
+ )}
+ {newCollectionModal && (
+ <NewCollectionModal
+ onClose={() => setNewCollectionModal(false)}
+ parent={activeCollection}
+ />
+ )}
+ {deleteCollectionModal && (
+ <DeleteCollectionModal
+ onClose={() => setDeleteCollectionModal(false)}
+ activeCollection={activeCollection}
+ />
+ )}
+ </>
+ )}
+ </MainLayout>
+ </DndContext>
);
}