Fix unstable node sorting order
This commit is contained in:
parent
98da7b1476
commit
bbea15dea4
@ -278,7 +278,18 @@ export default function Nodes() {
|
|||||||
size: pagination.size,
|
size: pagination.size,
|
||||||
search: filter?.search || undefined,
|
search: filter?.search || undefined,
|
||||||
});
|
});
|
||||||
const list = (data?.data?.list || []) as API.Node[];
|
const rawList = (data?.data?.list || []) as API.Node[];
|
||||||
|
// Backend should ideally return nodes already sorted, but we also sort on the
|
||||||
|
// frontend to keep the UI stable (and avoid "random" order after refresh).
|
||||||
|
const list = rawList.slice().sort((a, b) => {
|
||||||
|
const as = a.sort;
|
||||||
|
const bs = b.sort;
|
||||||
|
const an = typeof as === "number" ? as : Number.POSITIVE_INFINITY;
|
||||||
|
const bn = typeof bs === "number" ? bs : Number.POSITIVE_INFINITY;
|
||||||
|
if (an !== bn) return an - bn;
|
||||||
|
// Tie-breaker to keep a stable order.
|
||||||
|
return Number(a.id) - Number(b.id);
|
||||||
|
});
|
||||||
const total = Number(data?.data?.total || list.length);
|
const total = Number(data?.data?.total || list.length);
|
||||||
return { list, total };
|
return { list, total };
|
||||||
}}
|
}}
|
||||||
|
|||||||
@ -35,10 +35,16 @@ export function ProTableWrapper<TData extends { id?: string | number }>({
|
|||||||
|
|
||||||
const handleDragEnd = async (event: DragEndEvent) => {
|
const handleDragEnd = async (event: DragEndEvent) => {
|
||||||
const { active, over } = event;
|
const { active, over } = event;
|
||||||
if (onSort) {
|
|
||||||
const updatedData = await onSort(active.id, over?.id || null, data);
|
// If the pointer is released outside of any droppable row, `over` can be null.
|
||||||
setData(updatedData);
|
// In that case we should keep the current order (and avoid firing a sort API call).
|
||||||
}
|
if (!(onSort && over)) return;
|
||||||
|
|
||||||
|
// No-op when dropping onto itself.
|
||||||
|
if (String(active.id) === String(over.id)) return;
|
||||||
|
|
||||||
|
const updatedData = await onSort(active.id, over.id, data);
|
||||||
|
setData(updatedData);
|
||||||
};
|
};
|
||||||
if (!onSort) return children;
|
if (!onSort) return children;
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user