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 and drop to tag on sidebar

Kha Nguyenb59ad361929d
diff --git a/apps/web/components/DragNDrop.tsx b/apps/web/components/DragNDrop.tsx
index 4a3a6e36..89ebea75 100644
--- a/apps/web/components/DragNDrop.tsx
+++ b/apps/web/components/DragNDrop.tsx
@@ -17,6 +17,7 @@ import { useUpdateLink } from "@linkwarden/router/links";
import { useTranslation } from "react-i18next";
import { restrictToWindowEdges, snapCenterToCursor } from "@dnd-kit/modifiers";
import { customCollisionDetectionAlgorithm } from "@/lib/utils";
+import { useUpdateTag } from "@linkwarden/router/tags";
interface DragNDropProps {
children: React.ReactNode;
@@ -52,6 +53,7 @@ export default function DragNDrop({
onDragEnd: onDragEndProp,
}: DragNDropProps) {
const { t } = useTranslation();
+ const updateTag = useUpdateTag();
const updateLink = useUpdateLink();
const mouseSensor = useSensor(MouseSensor, {
// Require the mouse to move by 10 pixels before activating
@@ -89,27 +91,49 @@ export default function DragNDrop({
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;
+ let updatedLink: LinkIncludingShortenedCollectionAndTags | null = null;
- // Immediately hide the drag overlay
- setActiveLink(null);
+ // if the link is dropped over a tag
+ if (over.data.current?.type === "tag") {
+ const isTagAlreadyExists = activeLink.tags.some(
+ (tag) => tag.name === over.data.current?.name
+ );
+ if (isTagAlreadyExists) {
+ toast.error(t("tag_already_added"));
+ return;
+ }
+ // to match the tags structure required to update the link
+ const allTags: { name: string }[] = activeLink.tags.map((tag) => ({
+ name: tag.name,
+ }));
+ const newTags = [...allTags, { name: over.data.current?.name as string }];
+ updatedLink = {
+ ...activeLink,
+ tags: newTags as any,
+ };
+ } else {
+ const collectionId = over.data.current?.id as number;
+ const collectionName = over.data.current?.name as string;
+ const ownerId = over.data.current?.ownerId as number;
- // if the link dropped over the same collection, toast
- if (activeLink.collection.id === collectionId) {
- toast.error(t("link_already_in_collection"));
- return;
- }
+ // Immediately hide the drag overlay
+ setActiveLink(null);
- const updatedLink: LinkIncludingShortenedCollectionAndTags = {
- ...activeLink,
- collection: {
- id: collectionId,
- name: collectionName,
- ownerId,
- },
- };
+ // if the link dropped over the same collection, toast
+ if (activeLink.collection.id === collectionId) {
+ toast.error(t("link_already_in_collection"));
+ return;
+ }
+
+ updatedLink = {
+ ...activeLink,
+ collection: {
+ id: collectionId,
+ name: collectionName,
+ ownerId,
+ },
+ };
+ }
const load = toast.loading(t("updating"));
await updateLink.mutateAsync(updatedLink, {
@@ -128,7 +152,7 @@ export default function DragNDrop({
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onDragCancel={handleDragOverCancel}
- modifiers={[restrictToWindowEdges, snapCenterToCursor]}
+ modifiers={[snapCenterToCursor]}
sensors={sensorProp ? sensorProp : sensors}
collisionDetection={customCollisionDetectionAlgorithm}
>
diff --git a/apps/web/components/Droppable.tsx b/apps/web/components/Droppable.tsx
index 508f9f30..bbfbcb8d 100644
--- a/apps/web/components/Droppable.tsx
+++ b/apps/web/components/Droppable.tsx
@@ -10,8 +10,14 @@ const Droppable = ({
children: React.ReactNode;
id: string;
data?: {
- collectionId?: string;
- collectionName?: string;
+ /**
+ * Id of collection or tag to drop into.
+ */
+ id?: string;
+ /**
+ * Name of collection or tag to drop into.
+ */
+ name?: string;
ownerId?: string;
type?: "collection" | "tag";
};
diff --git a/apps/web/components/TagListing.tsx b/apps/web/components/TagListing.tsx
index 2db63d48..7a287aa1 100644
--- a/apps/web/components/TagListing.tsx
+++ b/apps/web/components/TagListing.tsx
@@ -2,6 +2,7 @@ import { Tag } from "@linkwarden/prisma/client";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import Droppable from "./Droppable";
+import { cn } from "@linkwarden/lib";
interface TagListingProps {
tags: Tag[];
@@ -27,14 +28,20 @@ export function TagListing({ tags, active }: TagListingProps) {
.sort((a: any, b: any) => a.name.localeCompare(b.name))
.map((e: any, i: any) => {
return (
- <Droppable id={`tag-${e.id}`} key={i}>
+ <Droppable
+ id={`tag-${e.id}`}
+ key={i}
+ className="group"
+ data={{ type: "tag", id: e.id, name: e.name }}
+ >
<Link key={i} href={`/tags/${e.id}`}>
<div
- className={`${
+ className={cn(
active === `/tags/${e.id}`
? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ : "group-[&:not([data-over])]:hover:bg-neutral/20",
+ "duration-100 py-1 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8"
+ )}
>
<i className="bi-hash text-xl text-primary drop-shadow"></i>
<p className="truncate w-full pr-7">{e.name}</p>
diff --git a/apps/web/pages/dashboard.tsx b/apps/web/pages/dashboard.tsx
index 17c9c54c..edbcfecb 100644
--- a/apps/web/pages/dashboard.tsx
+++ b/apps/web/pages/dashboard.tsx
@@ -153,14 +153,46 @@ export default function Dashboard() {
if (!over || !activeLink) return;
const targetSectionId = over.id as string;
- const collectionId = over.data.current?.collectionId as number;
- const collectionName = over.data.current?.collectionName as string;
+ const collectionId = over.data.current?.id as number;
+ const collectionName = over.data.current?.name as string;
const ownerId = over.data.current?.ownerId as number;
const isFromRecentSection = active.data.current?.dashboardType === "recent";
// Immediately hide the drag overlay
setActiveLink(null);
+ if (over.data.current?.type === "tag") {
+ const isTagAlreadyExists = activeLink.tags.some(
+ (tag) => tag.name === over.data.current?.name
+ );
+ if (isTagAlreadyExists) {
+ toast.error(t("tag_already_added"));
+ return;
+ }
+ // to match the tags structure required to update the link
+ const allTags: { name: string }[] = activeLink.tags.map((tag) => ({
+ name: tag.name,
+ }));
+ const newTags = [...allTags, { name: over.data.current?.name as string }];
+ const updatedLink = {
+ ...activeLink,
+ tags: newTags as any,
+ };
+ const load = toast.loading(t("updating"));
+ await updateLink.mutateAsync(updatedLink, {
+ onSettled: (_, error) => {
+ toast.dismiss(load);
+ if (error) {
+ // If there's an error, invalidate queries to restore the original state
+ queryClient.invalidateQueries({ queryKey: ["dashboardData"] });
+ toast.error(error.message);
+ } else {
+ toast.success(t("updated"));
+ }
+ },
+ });
+ return;
+ }
// Handle pinning the link
if (targetSectionId === "pinned-links-section") {
@@ -450,7 +482,7 @@ const Section = ({
<Droppable
id="pinned-links-section"
data={{
- collectionName: "pinned-links",
+ name: "pinned-links",
}}
>
{dashboardData.isLoading ||
@@ -510,9 +542,10 @@ const Section = ({
<Droppable
id={`dashboard-${collection.id}`}
data={{
- collectionId: collection.id,
- collectionName: collection.name,
+ id: collection.id,
+ name: collection.name,
ownerId: collection.ownerId,
+ type: "collection",
}}
>
{dashboardData.isLoading || collectionLinks?.length > 0 ? (
diff --git a/apps/web/public/locales/en/common.json b/apps/web/public/locales/en/common.json
index 13b67050..a021ab17 100644
--- a/apps/web/public/locales/en/common.json
+++ b/apps/web/public/locales/en/common.json
@@ -492,5 +492,6 @@
"save": "Save",
"edit_layout": "Edit Layout",
"refresh_multiple_preserved_formats_confirmation_desc": "This will delete the current preserved formats and re-preserve {{count}} links.",
- "refresh_preserved_formats_confirmation_desc": "This will delete the current preserved formats and re-preserve this link."
+ "refresh_preserved_formats_confirmation_desc": "This will delete the current preserved formats and re-preserve this link.",
+ "tag_already_added": "This link is already tagged with this tag."
}