-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sidebar-children.js
160 lines (141 loc) · 4.65 KB
/
sidebar-children.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// @flow
import * as React from 'react';
import SidebarRequestRow from './sidebar-request-row';
import SidebarRequestGroupRow from './sidebar-request-group-row';
import * as models from '../../../models/index';
import type { RequestGroup } from '../../../models/request-group';
import type { Workspace } from '../../../models/workspace';
import type { Request } from '../../../models/request';
import type { HotKeyRegistry } from '../../../common/hotkeys';
type Child = {
doc: Request | RequestGroup,
children: Array<Child>,
collapsed: boolean,
hidden: boolean,
pinned: boolean,
};
export type SidebarChildObjects = {
pinned: Array<Child>,
all: Array<Child>,
};
type Props = {
// Required
handleActivateRequest: Function,
handleCreateRequest: Function,
handleCreateRequestGroup: Function,
handleSetRequestPinned: Function,
handleSetRequestGroupCollapsed: Function,
handleDuplicateRequest: Function,
handleDuplicateRequestGroup: Function,
handleMoveRequestGroup: Function,
handleGenerateCode: Function,
handleCopyAsCurl: Function,
moveDoc: Function,
childObjects: SidebarChildObjects,
workspace: Workspace,
filter: string,
hotKeyRegistry: HotKeyRegistry,
// Optional
activeRequest?: Request,
};
class SidebarChildren extends React.PureComponent<Props> {
_renderChildren(children: Array<Child>, isInPinnedList: boolean): React.Node {
const {
filter,
handleCreateRequest,
handleCreateRequestGroup,
handleSetRequestPinned,
handleSetRequestGroupCollapsed,
handleDuplicateRequest,
handleDuplicateRequestGroup,
handleMoveRequestGroup,
handleGenerateCode,
handleCopyAsCurl,
moveDoc,
handleActivateRequest,
activeRequest,
workspace,
hotKeyRegistry,
} = this.props;
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
return children.map(child => {
if (!isInPinnedList && child.hidden) {
return null;
}
if (child.doc.type === models.request.type) {
return (
<SidebarRequestRow
key={child.doc._id}
filter={isInPinnedList ? '' : filter || ''}
moveDoc={moveDoc}
handleActivateRequest={handleActivateRequest}
handleSetRequestPinned={handleSetRequestPinned}
handleDuplicateRequest={handleDuplicateRequest}
handleGenerateCode={handleGenerateCode}
handleCopyAsCurl={handleCopyAsCurl}
requestCreate={handleCreateRequest}
isActive={child.doc._id === activeRequestId}
isPinned={child.pinned}
request={child.doc}
workspace={workspace}
hotKeyRegistry={hotKeyRegistry}
/>
);
}
// We have a RequestGroup!
const requestGroup = child.doc;
function hasActiveChild(children) {
for (const c of children) {
if (hasActiveChild(c.children || [])) {
return true;
} else if (c.doc._id === activeRequestId) {
return true;
}
}
// Didn't find anything, so return
return false;
}
const isActive = hasActiveChild(child.children);
const children = this._renderChildren(child.children, isInPinnedList);
return (
<SidebarRequestGroupRow
key={requestGroup._id}
filter={filter || ''}
isActive={isActive}
moveDoc={moveDoc}
handleActivateRequest={handleActivateRequest}
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
handleDuplicateRequestGroup={handleDuplicateRequestGroup}
handleMoveRequestGroup={handleMoveRequestGroup}
isCollapsed={child.collapsed}
handleCreateRequest={handleCreateRequest}
handleCreateRequestGroup={handleCreateRequestGroup}
numChildren={child.children.length}
workspace={workspace}
requestGroup={requestGroup}
hotKeyRegistry={hotKeyRegistry}
children={children}
/>
);
});
}
_renderList(children: Array<Child>, pinnedList: boolean): React.Node {
return (
<ul className="sidebar__list sidebar__list-root theme--sidebar__list">
{this._renderChildren(children, pinnedList)}
</ul>
);
}
render() {
const { childObjects } = this.props;
const showSeparator = childObjects.pinned.length > 0;
return (
<React.Fragment>
{this._renderList(childObjects.pinned, true)}
<div className={`sidebar__list-separator${showSeparator ? '' : '--invisible'}`} />
{this._renderList(childObjects.all, false)}
</React.Fragment>
);
}
}
export default SidebarChildren;