diff --git a/native/utils/drawer-utils.react.js b/native/utils/drawer-utils.react.js --- a/native/utils/drawer-utils.react.js +++ b/native/utils/drawer-utils.react.js @@ -72,4 +72,47 @@ return results; } -export { flattenDrawerItemsData }; +function findAllDescendantIDs( + data: $ReadOnlyArray>, +): $ReadOnlyArray { + const results = []; + for (const item of data) { + results.push(item.threadInfo.id); + results.concat(findAllDescendantIDs(item.itemChildren)); + } + return results; +} + +function findThreadChildrenItems( + data: $ReadOnlyArray>, + id: string, +): ?$ReadOnlyArray> { + for (const item of data) { + if (item.threadInfo.id === id) { + return item.itemChildren; + } + const result = findThreadChildrenItems(item.itemChildren, id); + if (result) { + return result; + } + } + return undefined; +} + +function filterThreadAndDescendantIDs( + idsToFilter: $ReadOnlyArray, + allItems: $ReadOnlyArray>, + threadID: string, +): $ReadOnlyArray { + const childItems = findThreadChildrenItems(allItems, threadID); + if (!childItems) { + return []; + } + const descendants = findAllDescendantIDs(childItems); + const descendantsSet = new Set(descendants); + return idsToFilter.filter( + item => !descendantsSet.has(item) && item !== threadID, + ); +} + +export { flattenDrawerItemsData, filterThreadAndDescendantIDs };