-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdrawer-utils.react.js
118 lines (103 loc) · 3.18 KB
/
drawer-utils.react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// @flow
import type { ThreadInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js';
import { threadTypeIsCommunityRoot } from 'lib/types/thread-types-enum.js';
import type { CommunityDrawerItemData } from 'lib/utils/drawer-utils.react.js';
import type { TextStyle } from '../types/styles.js';
export type CommunityDrawerItemDataFlattened = {
+threadInfo: ThreadInfo,
+hasSubchannelsButton: boolean,
+labelStyle: TextStyle,
+hasChildren: boolean,
+itemStyle: {
+indentation: number,
+background: 'none' | 'beginning' | 'middle' | 'end',
},
};
const defaultIndentation = 8;
const addedIndentation = 16;
function flattenDrawerItemsData(
data: $ReadOnlyArray<CommunityDrawerItemData<TextStyle>>,
expanded: $ReadOnlyArray<string>,
prevIndentation: ?number,
): $ReadOnlyArray<CommunityDrawerItemDataFlattened> {
let results: Array<CommunityDrawerItemDataFlattened> = [];
for (const item of data) {
const isOpen = expanded.includes(item.threadInfo.id);
const isCommunity = threadTypeIsCommunityRoot(item.threadInfo.type);
let background = 'middle';
if (isCommunity) {
background = isOpen ? 'beginning' : 'none';
}
let indentation = defaultIndentation;
if (!isCommunity && prevIndentation) {
indentation = prevIndentation + addedIndentation;
}
results.push({
threadInfo: item.threadInfo,
hasSubchannelsButton: item.hasSubchannelsButton,
labelStyle: item.labelStyle,
hasChildren: item.itemChildren?.length > 0,
itemStyle: {
indentation,
background,
},
});
if (!isOpen) {
continue;
}
results = results.concat(
flattenDrawerItemsData(item.itemChildren, expanded, indentation),
);
if (isCommunity) {
results[results.length - 1] = {
...results[results.length - 1],
itemStyle: {
...results[results.length - 1].itemStyle,
background: 'end',
},
};
}
}
return results;
}
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 };