⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
feat(web): add drag-n-drop to tag page
Kha Nguyena0ceca443c6d
diff --git a/apps/web/pages/tags/[id].tsx b/apps/web/pages/tags/[id].tsxindex 921d2ac3..f907119e 100644--- a/apps/web/pages/tags/[id].tsx+++ b/apps/web/pages/tags/[id].tsx@@ -1,8 +1,13 @@import { useRouter } from "next/router";import { FormEvent, useEffect, useState } from "react";import MainLayout from "@/layouts/MainLayout";-import { Sort, TagIncludingLinkCount, ViewMode } from "@linkwarden/types";-import { useLinks } from "@linkwarden/router/links";+import {+ LinkIncludingShortenedCollectionAndTags,+ Sort,+ TagIncludingLinkCount,+ ViewMode,+} from "@linkwarden/types";+import { useLinks, useUpdateLink } from "@linkwarden/router/links";import BulkDeleteLinksModal from "@/components/ModalContent/BulkDeleteLinksModal";import BulkEditLinksModal from "@/components/ModalContent/BulkEditLinksModal";import { useTranslation } from "next-i18next";@@ -19,6 +24,19 @@ 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";export default function Index() {const { t } = useTranslation();@@ -27,6 +45,7 @@ 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@@ -40,7 +59,24 @@ export default function Index() {const [bulkDeleteLinksModal, setBulkDeleteLinksModal] = useState(false);const [bulkEditLinksModal, setBulkEditLinksModal] = useState(false);const [editMode, setEditMode] = useState(false);+ 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]);@@ -136,102 +172,174 @@ 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 flex-col gap-5 w-full">- <LinkListOptions- t={t}- viewMode={viewMode}- setViewMode={setViewMode}- sortBy={sortBy}- setSortBy={setSortBy}- editMode={editMode}- setEditMode={setEditMode}- links={links}- >- <div className="flex gap-3 items-center">- <div className="flex gap-2 items-center font-thin">- <i className="bi-hash text-primary text-3xl" />-- {renameTag ? (- <form onSubmit={submit} className="flex items-center gap-2">- <input- type="text"- autoFocus- className="sm:text-3xl text-xl bg-transparent h-10 w-3/4 outline-none border-b border-b-neutral-content"- value={newTagName}- onChange={(e) => setNewTagName(e.target.value)}- />- <Button variant="ghost" size="icon" onClick={submit}>- <i className="bi-check2 text-neutral text-xl" />- </Button>- <Button variant="ghost" size="icon" onClick={cancelUpdateTag}>- <i className="bi-x text-neutral text-xl" />- </Button>- </form>- ) : (- <>- <p className="sm:text-3xl text-xl">{activeTag?.name}</p>- <div className="relative">- <DropdownMenu>- <DropdownMenuTrigger asChild>- <Button- asChild- variant="ghost"- size="icon"- title={t("more")}- >- <i className="bi-three-dots text-xl text-neutral" />- </Button>- </DropdownMenuTrigger>-- <DropdownMenuContent- sideOffset={4}- align={- activeTag?.name.length && activeTag?.name.length > 8- ? "end"- : "start"- }- className="bg-base-200 border border-neutral-content rounded-box p-1"- >- <DropdownMenuItem onClick={() => setRenameTag(true)}>- <i className="bi-pencil-square" />- {t("rename_tag")}- </DropdownMenuItem>-- <DropdownMenuSeparator />-- <DropdownMenuItem- onClick={remove}- className="text-error"+ <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>+ </DragOverlay>+ )}+ <div className="p-5 flex flex-col gap-5 w-full">+ <LinkListOptions+ t={t}+ viewMode={viewMode}+ setViewMode={setViewMode}+ sortBy={sortBy}+ setSortBy={setSortBy}+ editMode={editMode}+ setEditMode={setEditMode}+ links={links}+ >+ <div className="flex gap-3 items-center">+ <div className="flex gap-2 items-center font-thin">+ <i className="bi-hash text-primary text-3xl" />++ {renameTag ? (+ <form onSubmit={submit} className="flex items-center gap-2">+ <input+ type="text"+ autoFocus+ className="sm:text-3xl text-xl bg-transparent h-10 w-3/4 outline-none border-b border-b-neutral-content"+ value={newTagName}+ onChange={(e) => setNewTagName(e.target.value)}+ />+ <Button variant="ghost" size="icon" onClick={submit}>+ <i className="bi-check2 text-neutral text-xl" />+ </Button>+ <Button+ variant="ghost"+ size="icon"+ onClick={cancelUpdateTag}+ >+ <i className="bi-x text-neutral text-xl" />+ </Button>+ </form>+ ) : (+ <>+ <p className="sm:text-3xl text-xl">{activeTag?.name}</p>+ <div className="relative">+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <Button+ asChild+ variant="ghost"+ size="icon"+ title={t("more")}+ >+ <i className="bi-three-dots text-xl text-neutral" />+ </Button>+ </DropdownMenuTrigger>++ <DropdownMenuContent+ sideOffset={4}+ align={+ activeTag?.name.length && activeTag?.name.length > 8+ ? "end"+ : "start"+ }+ className="bg-base-200 border border-neutral-content rounded-box p-1">- <i className="bi-trash" />- {t("delete_tag")}- </DropdownMenuItem>- </DropdownMenuContent>- </DropdownMenu>- </div>- </>- )}+ <DropdownMenuItem onClick={() => setRenameTag(true)}>+ <i className="bi-pencil-square" />+ {t("rename_tag")}+ </DropdownMenuItem>++ <DropdownMenuSeparator />++ <DropdownMenuItem+ onClick={remove}+ className="text-error"+ >+ <i className="bi-trash" />+ {t("delete_tag")}+ </DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>+ </div>+ </>+ )}+ </div></div>- </div>- </LinkListOptions>-- <Links- editMode={editMode}- links={links}- layout={viewMode}- placeholderCount={1}- useData={data}- />- </div>- {bulkDeleteLinksModal && (- <BulkDeleteLinksModal onClose={() => setBulkDeleteLinksModal(false)} />- )}- {bulkEditLinksModal && (- <BulkEditLinksModal onClose={() => setBulkEditLinksModal(false)} />- )}- </MainLayout>+ </LinkListOptions>++ <Links+ editMode={editMode}+ links={links}+ layout={viewMode}+ placeholderCount={1}+ useData={data}+ />+ </div>+ {bulkDeleteLinksModal && (+ <BulkDeleteLinksModal+ onClose={() => setBulkDeleteLinksModal(false)}+ />+ )}+ {bulkEditLinksModal && (+ <BulkEditLinksModal onClose={() => setBulkEditLinksModal(false)} />+ )}+ </MainLayout>+ </DndContext>);}