| 1 | export function removeAndRenumberTimelineItem< |
| 2 | Id, |
| 3 | Row extends { readonly position: number; readonly sourceItemId: Id }, |
| 4 | >(rows: ReadonlyArray<Row>, sourceItemId: Id): Array<Row> { |
| 5 | return rows |
| 6 | .filter((row) => row.sourceItemId !== sourceItemId) |
| 7 | .map((row, position) => ({ ...row, position })); |
| 8 | } |
| 9 | |
| 10 | export function upsertTimelineItemAtStablePosition< |
| 11 | Id, |
| 12 | Row extends { readonly position: number; readonly sourceItemId: Id }, |
| 13 | >(rows: ReadonlyArray<Row>, item: Row): Array<Row> { |
| 14 | const existingIndex = rows.findIndex((row) => row.sourceItemId === item.sourceItemId); |
| 15 | if (existingIndex === -1) { |
| 16 | return [...rows, { ...item, position: rows.length }]; |
| 17 | } |
| 18 | |
| 19 | return rows.map((row, index) => |
| 20 | index === existingIndex ? { ...item, position: row.position } : row, |
| 21 | ); |
| 22 | } |