forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository-home-selection-view.js
244 lines (217 loc) · 6.08 KB
/
repository-home-selection-view.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import {createPaginationContainer, graphql} from 'react-relay';
import {TabbableTextEditor, TabbableSelect} from './tabbable';
const PAGE_DELAY = 500;
export const PAGE_SIZE = 50;
export class BareRepositoryHomeSelectionView extends React.Component {
static propTypes = {
// Relay
relay: PropTypes.shape({
hasMore: PropTypes.func.isRequired,
isLoading: PropTypes.func.isRequired,
loadMore: PropTypes.func.isRequired,
}).isRequired,
user: PropTypes.shape({
id: PropTypes.string.isRequired,
login: PropTypes.string.isRequired,
avatarUrl: PropTypes.string.isRequired,
organizations: PropTypes.shape({
edges: PropTypes.arrayOf(PropTypes.shape({
node: PropTypes.shape({
id: PropTypes.string.isRequired,
login: PropTypes.string.isRequired,
avatarUrl: PropTypes.string.isRequired,
viewerCanCreateRepositories: PropTypes.bool.isRequired,
}),
})),
}).isRequired,
}),
// Model
nameBuffer: PropTypes.object.isRequired,
isLoading: PropTypes.bool.isRequired,
selectedOwnerID: PropTypes.string.isRequired,
tabGroup: PropTypes.object.isRequired,
autofocusOwner: PropTypes.bool,
autofocusName: PropTypes.bool,
// Selection callback
didChangeOwnerID: PropTypes.func.isRequired,
// Atom environment
commands: PropTypes.object.isRequired,
}
static defaultProps = {
autofocusOwner: false,
autofocusName: false,
}
render() {
const owners = this.getOwners();
const currentOwner = owners.find(o => o.id === this.props.selectedOwnerID) || owners[0];
return (
<div className="github-RepositoryHome">
<TabbableSelect
tabGroup={this.props.tabGroup}
commands={this.props.commands}
autofocus={this.props.autofocusOwner}
className="github-RepositoryHome-owner"
clearable={false}
disabled={this.props.isLoading}
options={owners}
optionRenderer={this.renderOwner}
value={currentOwner}
valueRenderer={this.renderOwner}
onChange={this.didChangeOwner}
/>
<span className="github-RepositoryHome-separator">/</span>
<TabbableTextEditor
tabGroup={this.props.tabGroup}
commands={this.props.commands}
autofocus={this.props.autofocusName}
mini={true}
buffer={this.props.nameBuffer}
/>
</div>
);
}
renderOwner = owner => (
<Fragment>
<div className="github-RepositoryHome-ownerOption">
<img alt="" src={owner.avatarURL} className="github-RepositoryHome-ownerAvatar" />
<span className="github-RepositoryHome-ownerName">{owner.login}</span>
</div>
{owner.disabled && !owner.placeholder && (
<div className="github-RepositoryHome-ownerUnwritable">
(insufficient permissions)
</div>
)}
</Fragment>
);
componentDidMount() {
this.schedulePageLoad();
}
componentDidUpdate() {
this.schedulePageLoad();
}
getOwners() {
if (!this.props.user) {
return [{
id: 'loading',
login: 'loading...',
avatarURL: '',
disabled: true,
placeholder: true,
}];
}
const owners = [{
id: this.props.user.id,
login: this.props.user.login,
avatarURL: this.props.user.avatarUrl,
disabled: false,
}];
/* istanbul ignore if */
if (!this.props.user.organizations.edges) {
return owners;
}
for (const {node} of this.props.user.organizations.edges) {
/* istanbul ignore if */
if (!node) {
continue;
}
owners.push({
id: node.id,
login: node.login,
avatarURL: node.avatarUrl,
disabled: !node.viewerCanCreateRepositories,
});
}
if (this.props.relay && this.props.relay.hasMore()) {
owners.push({
id: 'loading',
login: 'loading...',
avatarURL: '',
disabled: true,
placeholder: true,
});
}
return owners;
}
didChangeOwner = owner => this.props.didChangeOwnerID(owner.id);
schedulePageLoad() {
if (!this.props.relay.hasMore()) {
return;
}
setTimeout(this.loadNextPage, PAGE_DELAY);
}
loadNextPage = () => {
/* istanbul ignore if */
if (this.props.relay.isLoading()) {
setTimeout(this.loadNextPage, PAGE_DELAY);
return;
}
this.props.relay.loadMore(PAGE_SIZE);
}
}
export default createPaginationContainer(BareRepositoryHomeSelectionView, {
user: graphql`
fragment repositoryHomeSelectionView_user on User
@argumentDefinitions(
organizationCount: {type: "Int!"}
organizationCursor: {type: "String"}
) {
id
login
avatarUrl(size: 24)
organizations(
first: $organizationCount
after: $organizationCursor
) @connection(key: "RepositoryHomeSelectionView_organizations") {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
login
avatarUrl(size: 24)
viewerCanCreateRepositories
}
}
}
}
`,
}, {
direction: 'forward',
/* istanbul ignore next */
getConnectionFromProps(props) {
return props.user && props.user.organizations;
},
/* istanbul ignore next */
getFragmentVariables(prevVars, totalCount) {
return {...prevVars, totalCount};
},
/* istanbul ignore next */
getVariables(props, {count, cursor}) {
return {
id: props.user.id,
organizationCount: count,
organizationCursor: cursor,
};
},
query: graphql`
query repositoryHomeSelectionViewQuery(
$id: ID!
$organizationCount: Int!
$organizationCursor: String
) {
node(id: $id) {
... on User {
...repositoryHomeSelectionView_user @arguments(
organizationCount: $organizationCount
organizationCursor: $organizationCursor
)
}
}
}
`,
});