Files
buzz/app/(ui)/admin/tl/components/order.ts
T
dmgnr 8c14999c59
Build and Push Image / build-backend (push) Successful in 6m13s
Build and Push Image / build-frontend (push) Successful in 7m33s
Build and Push Image / push (push) Successful in 1m16s
feat(admin): tierlist editor
2026-08-06 03:00:13 +07:00

37 lines
1.0 KiB
TypeScript

export const ORDER_STEP = 10;
export function reorderFlatItems<T extends { id: string }>(
items: T[],
activeId: string,
overId: string,
) {
if (activeId === overId) return items;
const oldIndex = items.findIndex((item) => item.id === activeId);
const newIndex = items.findIndex((item) => item.id === overId);
if (oldIndex === -1 || newIndex === -1) return items;
return arrayMove(items, oldIndex, newIndex);
}
export function midpointOrder(prev?: number, next?: number) {
if (prev == null && next == null) return ORDER_STEP;
if (prev == null) return next! - ORDER_STEP;
if (next == null) return prev + ORDER_STEP;
const middle = Math.floor((prev + next) / 2);
if (middle === prev || middle === next) return null;
return middle;
}
export function normalizedOrders(ids: string[]) {
return ids.map((id, index) => ({
id,
order: (index + 1) * ORDER_STEP,
}));
}
function arrayMove<T>(arr: T[], oldIndex: number, newIndex: number) {
return arr.toSpliced(oldIndex, 1).toSpliced(newIndex, 0, arr[oldIndex]);
}