Lets a live list put an item that left it, as by turning private, back at its old position when it returns.

Functions

f
insertAt<T extends { id: string; }>(
list: T[],
item: T,
idx: number
): T[]

Returns list with item at idx, or at the end when the list has since shrunk below it, unless the list holds it already.

f
usePositionMemory<T extends { id: string; }>(items: T[]): (id: string) => number | undefined

Remembers the index of every item that leaves items, whatever took it out, for useDumpListSync and usePlaylistListSync, which own the lists' live changes. items is the loaded list, or [] before it loads. The returned call gives an item's old index, once, and forgets it; it is undefined for an item that never left.

Usage

import * as mod from "src/hooks/feed/usePositionMemory.ts";