Changeset View
Changeset View
Standalone View
Standalone View
native/utils/drawer-utils.react.js
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | if (isCommunity) { | ||||
}, | }, | ||||
}; | }; | ||||
} | } | ||||
} | } | ||||
return results; | return results; | ||||
} | } | ||||
export { flattenDrawerItemsData }; | function findAllDescendantIDs<T>( | ||||
data: $ReadOnlyArray<CommunityDrawerItemData<T>>, | |||||
): $ReadOnlyArray<string> { | |||||
const results = []; | |||||
for (const item of data) { | |||||
results.push(item.threadInfo.id); | |||||
results.concat(findAllDescendantIDs(item.itemChildren)); | |||||
} | |||||
return results; | |||||
} | |||||
function findThreadChildrenItems<T>( | |||||
data: $ReadOnlyArray<CommunityDrawerItemData<T>>, | |||||
id: string, | |||||
): ?$ReadOnlyArray<CommunityDrawerItemData<T>> { | |||||
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 filterOutThreadAndDescendantIDs<T>( | |||||
idsToFilter: $ReadOnlyArray<string>, | |||||
allItems: $ReadOnlyArray<CommunityDrawerItemData<T>>, | |||||
threadID: string, | |||||
): $ReadOnlyArray<string> { | |||||
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, filterOutThreadAndDescendantIDs }; |