26 lines
938 B
TypeScript
26 lines
938 B
TypeScript
import type { PageSummary } from '@/lib/pages'
|
|
|
|
/**
|
|
* Reorder pages by moving sourceId beside targetId. Pinned and unpinned pages
|
|
* are separate groups (the server sorts pinned first), so a cross-group drop
|
|
* returns 'blocked' instead of silently landing at a different boundary.
|
|
* `after` places the source after the target; otherwise it goes before it.
|
|
*/
|
|
export function reorderPages(
|
|
pages: PageSummary[],
|
|
sourceId: string,
|
|
targetId: string,
|
|
after = false,
|
|
): PageSummary[] | 'blocked' | null {
|
|
const from = pages.findIndex((p) => p.id === sourceId)
|
|
const to = pages.findIndex((p) => p.id === targetId)
|
|
if (from < 0 || to < 0 || sourceId === targetId) return null
|
|
if (pages[from].pinned !== pages[to].pinned) return 'blocked'
|
|
const next = [...pages]
|
|
const [moved] = next.splice(from, 1)
|
|
let insertAt = to + (after ? 1 : 0)
|
|
if (from < insertAt) insertAt--
|
|
next.splice(insertAt, 0, moved)
|
|
return next
|
|
}
|