1
0
Fork 0

More checks on thread contexts

Some instances return really wacky order of posts
This commit is contained in:
Lim Chee Aun 2023-05-05 16:59:06 +08:00
parent d55bd95c72
commit 1f5d74d78e

View file

@ -97,6 +97,34 @@ export function groupContext(items) {
contexts[contextIndex++] = [item, repliedItem];
}
});
// Check for cross-item contexts
// Merge contexts into one if they have a common item (same id)
for (let i = 0; i < contexts.length; i++) {
for (let j = i + 1; j < contexts.length; j++) {
const commonItem = contexts[i].find((t) => contexts[j].includes(t));
if (commonItem) {
contexts[i] = [...contexts[i], ...contexts[j]];
// Remove duplicate items
contexts[i] = contexts[i].filter(
(item, index, self) =>
self.findIndex((t) => t.id === item.id) === index,
);
contexts.splice(j, 1);
j--;
}
}
}
// Sort items by checking inReplyToId
contexts.forEach((context) => {
context.sort((a, b) => {
if (a.inReplyToId === b.id) return -1;
if (b.inReplyToId === a.id) return 1;
return 0;
});
});
if (contexts.length) console.log('🧵 Contexts', contexts);
const newItems = [];