Skip to content

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

fix(web): fix collection sortable list on dashboard page

Kha Nguyenb8a6c771501c
diff --git a/apps/web/components/DashboardLayoutDropdown.tsx b/apps/web/components/DashboardLayoutDropdown.tsx
index 803a622c..1409c814 100644
--- a/apps/web/components/DashboardLayoutDropdown.tsx
+++ b/apps/web/components/DashboardLayoutDropdown.tsx
@@ -1,4 +1,4 @@
-import React, { useState, useMemo, DragEvent, useEffect } from "react";
+import React, { useState, useMemo, useEffect } from "react";
import {
DropdownMenu,
DropdownMenuTrigger,
@@ -14,6 +14,22 @@ import {
} from "@linkwarden/prisma/client";
import { useUser } from "@linkwarden/router/user";
import { useUpdateDashboardLayout } from "@linkwarden/router/dashboardData";
+import {
+ SortableContext,
+ useSortable,
+ verticalListSortingStrategy,
+} from "@dnd-kit/sortable";
+import { CSS } from "@dnd-kit/utilities";
+import {
+ DndContext,
+ DragEndEvent,
+ MouseSensor,
+ TouchSensor,
+ useSensor,
+ useSensors,
+} from "@dnd-kit/core";
+import { restrictToParentElement } from "@dnd-kit/modifiers";
+import { cn } from "@linkwarden/lib";
interface DashboardSectionOption {
type: DashboardSectionType;
@@ -29,11 +45,20 @@ export default function DashboardLayoutDropdown() {
const { data: collections = [] } = useCollections();
const updateDashboardLayout = useUpdateDashboardLayout();
const [searchTerm, setSearchTerm] = useState("");
- const [draggedId, setDraggedId] = useState<string | null>(null);
- const [dragOverId, setDragOverId] = useState<string | null>(null);
- const [dragOverPosition, setDragOverPosition] = useState<
- "above" | "below" | 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 200ms, with tolerance of 5px of movement
+ activationConstraint: {
+ delay: 200,
+ tolerance: 5,
+ },
+ });
+ const sensors = useSensors(mouseSensor, touchSensor);
const [dashboardSections, setDashboardSections] = useState<
DashboardSection[]
@@ -184,6 +209,32 @@ export default function DashboardLayoutDropdown() {
updateDashboardLayout.mutateAsync(updated);
};
+ const handleDragEnd = (event: DragEndEvent) => {
+ const { active, over } = event;
+ if (!over || active.id === over.id) {
+ return;
+ }
+
+ const sourceId = active.id as string;
+ const destId = over.id as string;
+
+ // Only allow reordering enabled sections
+ const sourceSection = filteredSections.find(
+ (s) => getSectionId(s) === sourceId
+ );
+ const destSection = filteredSections.find(
+ (s) => getSectionId(s) === destId
+ );
+ if (sourceSection?.enabled && destSection?.enabled) {
+ handleReorder(sourceId, destId);
+ }
+ };
+
+ // Only include enabled sections in the sortable context
+ const sortableItems = filteredSections
+ .filter((section) => section.enabled)
+ .map(getSectionId);
+
return (
<DropdownMenu modal>
<DropdownMenuTrigger asChild>
@@ -210,37 +261,40 @@ export default function DashboardLayoutDropdown() {
placeholder={t("search")}
/>
</div>
-
- <ul className="max-h-60 overflow-y-auto px-2 pb-2">
- {filteredSections.map((section) => {
- const color =
- section.type === "COLLECTION"
- ? collections.find((c) => c.id === section.collectionId)
- ?.color
- : undefined;
-
- return (
- <DraggableListItem
- key={getSectionId(section)}
- section={{ ...section, color }}
- onCheckboxChange={handleCheckboxChange}
- onReorder={handleReorder}
- draggedId={draggedId}
- dragOverId={dragOverId}
- dragOverPosition={dragOverPosition}
- setDraggedId={setDraggedId}
- setDragOverId={setDragOverId}
- setDragOverPosition={setDragOverPosition}
- />
- );
- })}
-
- {filteredSections.length === 0 && (
- <li className="text-sm py-2 text-center text-neutral">
- {t("no_results_found")}
- </li>
- )}
- </ul>
+ <DndContext
+ modifiers={[restrictToParentElement]}
+ onDragEnd={handleDragEnd}
+ sensors={sensors}
+ >
+ <SortableContext
+ items={sortableItems}
+ strategy={verticalListSortingStrategy}
+ >
+ <ul className="max-h-60 overflow-y-auto px-2 pb-2">
+ {filteredSections.map((section) => {
+ const color =
+ section.type === "COLLECTION"
+ ? collections.find((c) => c.id === section.collectionId)
+ ?.color
+ : undefined;
+
+ return (
+ <DraggableListItem
+ key={getSectionId(section)}
+ section={{ ...section, color }}
+ onCheckboxChange={handleCheckboxChange}
+ />
+ );
+ })}
+
+ {filteredSections.length === 0 && (
+ <li className="text-sm py-2 text-center text-neutral">
+ {t("no_results_found")}
+ </li>
+ )}
+ </ul>
+ </SortableContext>
+ </DndContext>
</div>
</DropdownMenuContent>
</DropdownMenu>
@@ -250,91 +304,44 @@ export default function DashboardLayoutDropdown() {
interface DraggableListItemProps {
section: DashboardSectionOption & { color?: string };
onCheckboxChange: (section: DashboardSectionOption) => void;
- onReorder: (sourceId: string, destId: string) => void;
- draggedId: string | null;
- dragOverId: string | null;
- dragOverPosition: "above" | "below" | null;
- setDraggedId: (id: string | null) => void;
- setDragOverId: (id: string | null) => void;
- setDragOverPosition: (pos: "above" | "below" | null) => void;
}
function DraggableListItem({
section,
onCheckboxChange,
- onReorder,
- draggedId,
- dragOverId,
- dragOverPosition,
- setDraggedId,
- setDragOverId,
- setDragOverPosition,
}: DraggableListItemProps) {
const sectionId = `${section.type}-${section.collectionId ?? "default"}`;
- const handleDragStart = (e: DragEvent<HTMLLIElement>) => {
- const img = new Image();
- img.src =
- "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'></svg>";
- e.dataTransfer.setDragImage(img, 0, 0);
-
- e.dataTransfer.setData("text/plain", sectionId);
- setDraggedId(sectionId);
- e.dataTransfer.effectAllowed = "move";
+ const {
+ attributes,
+ listeners,
+ setNodeRef,
+ transform,
+ transition,
+ isDragging,
+ } = useSortable({
+ id: sectionId,
+ disabled: !section.enabled,
+ });
+
+ const style = {
+ transform: CSS.Transform.toString(transform),
+ transition,
};
- const handleDragOver = (e: DragEvent<HTMLLIElement>) => {
- e.preventDefault();
- const rect = e.currentTarget.getBoundingClientRect();
- const midY = rect.top + rect.height / 2;
- const pos = e.clientY < midY ? "above" : "below";
- setDragOverPosition(pos);
- setDragOverId(sectionId);
- e.dataTransfer.dropEffect = "move";
- };
-
- const handleDragLeave = () => {
- setDragOverId(null);
- setDragOverPosition(null);
- };
-
- const handleDrop = (e: DragEvent<HTMLLIElement>) => {
- e.preventDefault();
- const sourceId = e.dataTransfer.getData("text/plain");
- if (sourceId) {
- const destId = dragOverPosition === "below" ? sectionId : sectionId;
- onReorder(sourceId, destId);
- }
- setDraggedId(null);
- setDragOverId(null);
- setDragOverPosition(null);
- };
-
- const indicatorClass =
- dragOverId === sectionId && dragOverPosition === "above"
- ? "border-t-2 border-primary"
- : dragOverId === sectionId && dragOverPosition === "below"
- ? "border-b-2 border-primary"
- : "";
-
return (
<li
- draggable={section.enabled}
- onDragStart={handleDragStart}
- onDragOver={handleDragOver}
- onDragLeave={handleDragLeave}
- onDrop={handleDrop}
- onDragEnd={() => {
- setDraggedId(null);
- setDragOverId(null);
- setDragOverPosition(null);
- }}
- className={`
- select-none py-1 px-1 flex items-center justify-between
- ${section.enabled ? "cursor-grab active:cursor-grabbing" : ""}
- ${draggedId === sectionId ? "opacity-70" : ""}
- ${indicatorClass}
- `}
+ ref={setNodeRef}
+ style={style}
+ {...attributes}
+ {...listeners}
+ className={cn(
+ "select-none py-1 px-1 flex items-center justify-between",
+ section.enabled
+ ? "cursor-grab active:cursor-grabbing"
+ : "cursor-default",
+ isDragging && "opacity-50"
+ )}
>
<div className="flex items-center gap-2">
<input
diff --git a/apps/web/package.json b/apps/web/package.json
index 5a51f4e6..db6057b2 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -20,6 +20,8 @@
"@auth/prisma-adapter": "^1.0.1",
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/modifiers": "^9.0.0",
+ "@dnd-kit/sortable": "^10.0.0",
+ "@dnd-kit/utilities": "^3.2.2",
"@headlessui/react": "^1.7.15",
"@linkwarden/filesystem": "*",
"@linkwarden/lib": "*",
diff --git a/yarn.lock b/yarn.lock
index 54d6c931..d81df058 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1690,6 +1690,14 @@
"@dnd-kit/utilities" "^3.2.2"
tslib "^2.0.0"
+"@dnd-kit/sortable@^10.0.0":
+ version "10.0.0"
+ resolved "https://registry.yarnpkg.com/@dnd-kit/sortable/-/sortable-10.0.0.tgz#1f9382b90d835cd5c65d92824fa9dafb78c4c3e8"
+ integrity sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==
+ dependencies:
+ "@dnd-kit/utilities" "^3.2.2"
+ tslib "^2.0.0"
+
"@dnd-kit/utilities@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@dnd-kit/utilities/-/utilities-3.2.2.tgz#5a32b6af356dc5f74d61b37d6f7129a4040ced7b"