Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(web): a dropped tab lands in its new place (#753)

Pasqual Troncone4fe810e7a6fa
diff --git a/apps/web/src/components/layout/ViewTabs.tsx b/apps/web/src/components/layout/ViewTabs.tsx
index 4178b4e2..34f46cd4 100644
--- a/apps/web/src/components/layout/ViewTabs.tsx
+++ b/apps/web/src/components/layout/ViewTabs.tsx
@@ -11,7 +11,8 @@ import { FilterIcon } from '@animateicons/react/lucide/filter-icon';
import { Plus } from 'lucide-react';
import { useTranslations } from 'next-intl';
import type { View } from '@/lib/api/endpoints/views';
-import { useDndSensors } from '@/lib/dnd';
+import { useDndSensors, useDroppedOrder } from '@/lib/dnd';
+import { reorderedViews, tabOrder } from '@/services/views.service';
import { usePermissions } from '@/hooks/usePermissions';
import AllViewTab, { ALL_DROP_ID } from '@/components/layout/AllViewTab';
import MobileViewSwitcher from '@/components/layout/MobileViewSwitcher';
@@ -58,6 +59,15 @@ export default function ViewTabs({
// The view being dragged, used to render the DragOverlay preview.
const [activeId, setActiveId] = useState<number | null>(null);
const activeView = activeId != null ? (views.find((v) => v.id === activeId) ?? null) : null;
+ const { items: shown, drop } = useDroppedOrder(views);
+
+ // Shows at once the tab row the server will answer with once the move is stored.
+ function reorder(draggedId: number, targetId: number | null) {
+ const moved = reorderedViews(views, draggedId, targetId);
+ if (!moved) return;
+ drop(tabOrder(moved).map((v) => v.id));
+ onReorder(draggedId, targetId);
+ }
function handleDragEnd(e: DragEndEvent) {
setActiveId(null);
@@ -67,10 +77,10 @@ export default function ViewTabs({
if (over.id === ALL_DROP_ID) {
// Move to the front of the stored order. The tab row is not that order —
// favorites are pinned ahead of it — so the front cannot be named by a tab.
- onReorder(draggedId, null);
+ reorder(draggedId, null);
return;
}
- if (over.id !== active.id) onReorder(draggedId, Number(over.id));
+ if (over.id !== active.id) reorder(draggedId, Number(over.id));
}
return (
@@ -101,8 +111,8 @@ export default function ViewTabs({
onClick={() => onSelect(null)}
/>
- <SortableContext items={views.map((v) => v.id)} strategy={horizontalListSortingStrategy}>
- {views.map((view) => (
+ <SortableContext items={shown.map((v) => v.id)} strategy={horizontalListSortingStrategy}>
+ {shown.map((view) => (
<SavedViewTab
key={view.id}
view={view}
diff --git a/apps/web/src/features/dashboards/DashboardsPage.tsx b/apps/web/src/features/dashboards/DashboardsPage.tsx
index 9767933d..cd9de1de 100644
--- a/apps/web/src/features/dashboards/DashboardsPage.tsx
+++ b/apps/web/src/features/dashboards/DashboardsPage.tsx
@@ -132,7 +132,7 @@ export default function DashboardsPage() {
onNewDashboard={(name) => void editor.createDashboard(name)}
onRename={(d, name) => void editor.renameDashboard(d, name)}
onDelete={(d) => void editor.deleteDashboard(d)}
- onReorder={(dragged, target) => editor.reorderDashboards(dragged, target)}
+ onReorder={editor.reorderDashboards}
actions={layoutActions()}
/>
diff --git a/apps/web/src/features/dashboards/components/DashboardTabs.tsx b/apps/web/src/features/dashboards/components/DashboardTabs.tsx
index bb4b1cf0..43b4e713 100644
--- a/apps/web/src/features/dashboards/components/DashboardTabs.tsx
+++ b/apps/web/src/features/dashboards/components/DashboardTabs.tsx
@@ -6,11 +6,11 @@ import {
type DragEndEvent,
type DragStartEvent,
} from '@dnd-kit/core';
-import { SortableContext, horizontalListSortingStrategy } from '@dnd-kit/sortable';
+import { arrayMove, SortableContext, horizontalListSortingStrategy } from '@dnd-kit/sortable';
import { LayoutDashboard, Plus } from 'lucide-react';
import { useTranslations } from 'next-intl';
import type { Dashboard } from '@/lib/api/endpoints/dashboards';
-import { useDndSensors } from '@/lib/dnd';
+import { useDndSensors, useDroppedOrder } from '@/lib/dnd';
import { usePermissions } from '@/hooks/usePermissions';
import { cn } from '@/lib/utils';
import DashboardTab from './DashboardTab';
@@ -38,7 +38,7 @@ export default function DashboardTabs({
onNewDashboard: (name: string) => void;
onRename: (d: Dashboard, name: string) => void;
onDelete: (d: Dashboard) => void;
- onReorder: (draggedId: number, targetId: number) => void;
+ onReorder: (ids: number[]) => void;
}) {
const t = useTranslations('dashboards');
const { can } = usePermissions();
@@ -48,6 +48,7 @@ export default function DashboardTabs({
const sensors = useDndSensors();
const [activeId, setActiveId] = useState<number | null>(null);
const dragged = activeId != null ? dashboards.find((d) => d.id === activeId) : null;
+ const { items: shown, drop } = useDroppedOrder(dashboards);
// Name dialog state: 'new' to create, a dashboard to rename, or null (closed).
const [dialog, setDialog] = useState<'new' | Dashboard | null>(null);
const renaming = dialog && dialog !== 'new' ? dialog : null;
@@ -55,7 +56,14 @@ export default function DashboardTabs({
function handleDragEnd(e: DragEndEvent) {
setActiveId(null);
const { active, over } = e;
- if (over && active.id !== over.id) onReorder(Number(active.id), Number(over.id));
+ if (!over || active.id === over.id) return;
+ const ids = dashboards.map((d) => d.id);
+ const from = ids.indexOf(Number(active.id));
+ const to = ids.indexOf(Number(over.id));
+ if (from === -1 || to === -1) return;
+ const next = arrayMove(ids, from, to);
+ drop(next);
+ onReorder(next);
}
return (
@@ -85,10 +93,10 @@ export default function DashboardTabs({
onDragEnd={handleDragEnd}
>
<SortableContext
- items={dashboards.map((d) => d.id)}
+ items={shown.map((d) => d.id)}
strategy={horizontalListSortingStrategy}
>
- {dashboards.map((d) => (
+ {shown.map((d) => (
<DashboardTab
key={d.id}
dashboard={d}
diff --git a/apps/web/src/features/dashboards/hooks/useDashboardEditor.ts b/apps/web/src/features/dashboards/hooks/useDashboardEditor.ts
index 4f559635..96e8004c 100644
--- a/apps/web/src/features/dashboards/hooks/useDashboardEditor.ts
+++ b/apps/web/src/features/dashboards/hooks/useDashboardEditor.ts
@@ -1,5 +1,4 @@
import { useState } from 'react';
-import { arrayMove } from '@dnd-kit/sortable';
import { useTranslations } from 'next-intl';
import type { Dashboard } from '@/lib/api/endpoints/dashboards';
import {
@@ -115,13 +114,7 @@ export function useDashboardEditor(
}
};
- const reorderDashboards = (draggedId: number, targetId: number) => {
- const ids = dashboards.map((d) => d.id);
- const from = ids.indexOf(draggedId);
- const to = ids.indexOf(targetId);
- if (from === -1 || to === -1 || from === to) return;
- reorderM.mutate(arrayMove(ids, from, to));
- };
+ const reorderDashboards = (ids: number[]) => reorderM.mutate(ids);
return {
active,
diff --git a/apps/web/src/hooks/useViewEditor.ts b/apps/web/src/hooks/useViewEditor.ts
index 220c25a9..88a2ab8d 100644
--- a/apps/web/src/hooks/useViewEditor.ts
+++ b/apps/web/src/hooks/useViewEditor.ts
@@ -3,6 +3,7 @@ import type { View } from '@/lib/api/endpoints/views';
import type { WorkItemsView } from '@/utils/viewTypes';
import {
normalizeView,
+ reorderedViews,
useCreateView,
useDeleteView,
useReorderViews,
@@ -241,14 +242,8 @@ export function useViewEditor(
// applies to the stored order, not to the tab row — favorites are pinned to the
// front for the caller only, while position is shared by the project.
function reorderView(draggedId: number, targetId: number | null) {
- if (draggedId === targetId) return;
- const next = [...views].sort((a, b) => a.position - b.position || a.id - b.id);
- const from = next.findIndex((v) => v.id === draggedId);
- const to = targetId == null ? 0 : next.findIndex((v) => v.id === targetId);
- if (from < 0 || to < 0 || from === to) return;
- const [moved] = next.splice(from, 1);
- next.splice(to, 0, moved);
- reorderViewsMutation.mutate(next.map((v) => v.id));
+ const next = reorderedViews(views, draggedId, targetId);
+ if (next) reorderViewsMutation.mutate(next.map((v) => v.id));
}
return {
diff --git a/apps/web/src/lib/dnd.test.tsx b/apps/web/src/lib/dnd.test.tsx
new file mode 100644
index 00000000..c838f247
--- /dev/null
+++ b/apps/web/src/lib/dnd.test.tsx
@@ -0,0 +1,76 @@
+import assert from 'node:assert/strict';
+import { afterEach, beforeEach, describe, it } from 'node:test';
+import { act } from 'react';
+import type { Root } from 'react-dom/client';
+import { JSDOM } from 'jsdom';
+import { useDroppedOrder } from './dnd';
+
+type Item = { id: number };
+
+let dom: JSDOM;
+let root: Root;
+let drop: (ids: number[]) => void;
+let shown: number[];
+const replacedGlobals = ['window', 'document', 'IS_REACT_ACT_ENVIRONMENT'] as const;
+let originalGlobalDescriptors: Map<string, PropertyDescriptor | undefined>;
+
+function Harness({ items }: { items: Item[] }) {
+ const dropped = useDroppedOrder(items);
+ drop = dropped.drop;
+ shown = dropped.items.map((item) => item.id);
+ return null;
+}
+
+const list = (...ids: number[]) => ids.map((id) => ({ id }));
+
+async function render(items: Item[]) {
+ await act(async () => root.render(<Harness items={items} />));
+}
+
+beforeEach(async () => {
+ originalGlobalDescriptors = new Map(
+ replacedGlobals.map((name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)]),
+ );
+ dom = new JSDOM('<!doctype html><div id="root"></div>');
+ Object.defineProperties(globalThis, {
+ window: { configurable: true, value: dom.window },
+ document: { configurable: true, value: dom.window.document },
+ IS_REACT_ACT_ENVIRONMENT: { configurable: true, value: true },
+ });
+ const { createRoot } = await import('react-dom/client');
+ root = createRoot(document.querySelector('#root')!);
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ dom.window.close();
+ for (const [name, descriptor] of originalGlobalDescriptors) {
+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);
+ else Reflect.deleteProperty(globalThis, name);
+ }
+});
+
+describe('useDroppedOrder', () => {
+ it('shows the dropped order at once, before the data reaches the list', async () => {
+ await render(list(1, 2, 3));
+ await act(async () => drop([3, 1, 2]));
+ assert.deepEqual(shown, [3, 1, 2]);
+ });
+
+ it('follows the data once it changes', async () => {
+ await render(list(1, 2, 3));
+ await act(async () => drop([3, 1, 2]));
+ await render(list(3, 1, 2));
+ assert.deepEqual(shown, [3, 1, 2]);
+ await render(list(2, 3, 1));
+ assert.deepEqual(shown, [2, 3, 1]);
+ });
+
+ it('shows the data again when a failed move rolls it back', async () => {
+ await render(list(1, 2, 3));
+ await act(async () => drop([3, 1, 2]));
+ await render(list(3, 1, 2));
+ await render(list(1, 2, 3));
+ assert.deepEqual(shown, [1, 2, 3]);
+ });
+});
diff --git a/apps/web/src/lib/dnd.ts b/apps/web/src/lib/dnd.ts
index 80a61705..49d6fb74 100644
--- a/apps/web/src/lib/dnd.ts
+++ b/apps/web/src/lib/dnd.ts
@@ -1,6 +1,6 @@
import { KeyboardSensor, MouseSensor, TouchSensor, useSensor, useSensors } from '@dnd-kit/core';
import { sortableKeyboardCoordinates } from '@dnd-kit/sortable';
-import type { SyntheticEvent } from 'react';
+import { useEffect, useState, type SyntheticEvent } from 'react';
// How far the pointer must move before a press becomes a drag. Also the threshold
// a draggable uses to tell a click apart from the click the browser fires after a
@@ -29,6 +29,21 @@ export function useDndSensors(inert = false) {
return inert ? inertSensors : sensors;
}
+// A sortable list's order from a drop until the reordered data reaches the list. A query
+// cache tells React a task after the drop, and by then a DragOverlay has measured the
+// dragged item in its old slot and flies back there before the list moves. Call drop in
+// onDragEnd with every id in the new order, so the list moves in the same render.
+export function useDroppedOrder<T extends { id: number }>(items: T[]) {
+ const [dropped, setDropped] = useState<{ from: string; ids: number[] } | null>(null);
+ const key = items.map((item) => item.id).join();
+ useEffect(() => setDropped(null), [key]);
+ const byId = new Map(items.map((item) => [item.id, item]));
+ return {
+ items: dropped?.from === key ? dropped.ids.flatMap((id) => byId.get(id) ?? []) : items,
+ drop: (ids: number[]) => setDropped({ from: key, ids }),
+ };
+}
+
const stopPropagation = (e: SyntheticEvent) => e.stopPropagation();
// Spread on a control inside a draggable so pressing it does not start a drag of
diff --git a/apps/web/src/services/views.service.test.ts b/apps/web/src/services/views.service.test.ts
new file mode 100644
index 00000000..9ff75a9a
--- /dev/null
+++ b/apps/web/src/services/views.service.test.ts
@@ -0,0 +1,49 @@
+import assert from 'node:assert/strict';
+import { describe, it } from 'node:test';
+import type { View } from '@/lib/api/endpoints/views';
+import { reorderedViews, tabOrder } from './views.service';
+
+function view(id: number, position: number, favorite = false): View {
+ return {
+ id,
+ projectId: 1,
+ name: `V${id}`,
+ icon: null,
+ filters: { conditions: [] },
+ display: { layout: 'kanban' } as View['display'],
+ position,
+ shareToken: null,
+ shareExtended: false,
+ favorite,
+ createdAt: '2026-01-01T00:00:00Z',
+ };
+}
+
+const ids = (views: View[] | null) => views?.map((v) => v.id);
+
+describe('reorderedViews', () => {
+ // F is a favorite stored last; the tab row shows it first.
+ const a = view(1, 0);
+ const b = view(2, 1);
+ const c = view(3, 2);
+ const f = view(4, 3, true);
+ const tabs = tabOrder([a, b, c, f]);
+
+ it('moves the view to the stored place of the one it is dropped on', () => {
+ assert.deepEqual(ids(tabs), [4, 1, 2, 3]);
+ assert.deepEqual(ids(reorderedViews(tabs, a.id, f.id)), [2, 3, 4, 1]);
+ });
+
+ it('gives the tab row the server will answer with, favorites still ahead', () => {
+ assert.deepEqual(ids(tabOrder(reorderedViews(tabs, a.id, f.id)!)), [4, 2, 3, 1]);
+ });
+
+ it('moves to the front of the stored order for a drop on All', () => {
+ assert.deepEqual(ids(reorderedViews(tabs, c.id, null)), [3, 1, 2, 4]);
+ });
+
+ it('moves nothing when the view stays where it is', () => {
+ assert.equal(reorderedViews(tabs, a.id, null), null);
+ assert.equal(reorderedViews(tabs, a.id, a.id), null);
+ });
+});
diff --git a/apps/web/src/services/views.service.ts b/apps/web/src/services/views.service.ts
index 0c6c1d6b..5c6e7aaa 100644
--- a/apps/web/src/services/views.service.ts
+++ b/apps/web/src/services/views.service.ts
@@ -25,15 +25,35 @@ export function normalizeView(v: View): View {
};
}
+// The tab row: the caller's favorites are pinned to the front; the rest keep their
+// stored order.
+export function tabOrder(views: View[]): View[] {
+ return [...views].sort((a, b) => Number(b.favorite) - Number(a.favorite));
+}
+
+// The stored order with the dragged view moved to the target's place, or to the front
+// for a null target; null when nothing moves.
+export function reorderedViews(
+ views: View[],
+ draggedId: number,
+ targetId: number | null,
+): View[] | null {
+ if (draggedId === targetId) return null;
+ const next = [...views].sort((a, b) => a.position - b.position || a.id - b.id);
+ const from = next.findIndex((v) => v.id === draggedId);
+ const to = targetId == null ? 0 : next.findIndex((v) => v.id === targetId);
+ if (from < 0 || to < 0 || from === to) return null;
+ const [moved] = next.splice(from, 1);
+ next.splice(to, 0, moved);
+ return next;
+}
+
export function useViewsQuery(projectKey: string | null) {
return useQuery({
queryKey: qk.views(projectKey ?? ''),
queryFn: () => listViews(projectKey!),
enabled: projectKey != null,
- // The caller's favorites are pinned to the front of the tab row; the rest keep
- // their stored order.
- select: (rows) =>
- rows.map(normalizeView).sort((a, b) => Number(b.favorite) - Number(a.favorite)),
+ select: (rows) => tabOrder(rows.map(normalizeView)),
});
}