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 @@ -91,4 +91,44 @@ 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( + ids: $ReadOnlyArray, + allItems: $ReadOnlyArray>, + id: string, +): $ReadOnlyArray { + const childItems = findThreadChildrenItems(allItems, id); + if (!childItems) { + return []; + } + const descendants = findAllDescendantIDs(childItems); + return ids.filter(item => !descendants.includes(item) && !(item === id)); +} + +export { flattenDrawerItemsData, filterThreadAndDescendantIDs };