| 1 | import type { Item } from "./useController"; |
| 2 | |
| 3 | /** Defensive deduplication uses durable identity, never submission identity. */ |
| 4 | export function uniqueUserItems(items: readonly Item[]): Item[] { |
| 5 | const ids = new Map<string, number>(); |
| 6 | const output: Item[] = []; |
| 7 | for (const item of items) { |
| 8 | if (item.kind !== "user") { output.push(item); continue; } |
| 9 | const identity = item.messageId ? `m:${item.messageId}` : item.id; |
| 10 | let index = ids.get(identity); |
| 11 | if (index !== undefined) { |
| 12 | const prior = output[index]; |
| 13 | if (prior.kind === "user" && item.messageId) output[index] = { ...prior, ...item }; |
| 14 | } else { index = output.length; output.push(item); } |
| 15 | ids.set(identity, index); |
| 16 | } |
| 17 | return output; |
| 18 | } |
| 19 |