forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-container.js
More file actions
149 lines (127 loc) · 4.11 KB
/
remote-container.js
File metadata and controls
149 lines (127 loc) · 4.11 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import {QueryRenderer, graphql} from 'react-relay';
import {incrementCounter} from '../reporter-proxy';
import {autobind} from '../helpers';
import {
RemotePropType, RemoteSetPropType, BranchSetPropType, OperationStateObserverPropType, EndpointPropType,
} from '../prop-types';
import RelayNetworkLayerManager from '../relay-network-layer-manager';
import {UNAUTHENTICATED, INSUFFICIENT} from '../shared/keytar-strategy';
import RemoteController from '../controllers/remote-controller';
import ObserveModel from '../views/observe-model';
import LoadingView from '../views/loading-view';
import QueryErrorView from '../views/query-error-view';
import GithubLoginView from '../views/github-login-view';
export default class RemoteContainer extends React.Component {
static propTypes = {
// Connection
loginModel: PropTypes.object.isRequired,
endpoint: EndpointPropType.isRequired,
// Repository attributes
remoteOperationObserver: OperationStateObserverPropType.isRequired,
pushInProgress: PropTypes.bool.isRequired,
workingDirectory: PropTypes.string.isRequired,
workspace: PropTypes.object.isRequired,
remote: RemotePropType.isRequired,
remotes: RemoteSetPropType.isRequired,
branches: BranchSetPropType.isRequired,
aheadCount: PropTypes.number,
// Action methods
onPushBranch: PropTypes.func.isRequired,
}
constructor(props) {
super(props);
autobind(this, 'fetchToken', 'renderWithToken', 'renderWithResult', 'handleLogin', 'handleLogout');
}
fetchToken(loginModel) {
return loginModel.getToken(this.props.endpoint.getLoginAccount());
}
render() {
return (
<ObserveModel model={this.props.loginModel} fetchData={this.fetchToken}>
{this.renderWithToken}
</ObserveModel>
);
}
renderWithToken(token) {
if (token === null) {
return <LoadingView />;
}
if (token === UNAUTHENTICATED) {
return <GithubLoginView onLogin={this.handleLogin} />;
}
if (token === INSUFFICIENT) {
return (
<GithubLoginView onLogin={this.handleLogin}>
<p>
Your token no longer has sufficient authorizations. Please re-authenticate and generate a new one.
</p>
</GithubLoginView>
);
}
const environment = RelayNetworkLayerManager.getEnvironmentForHost(this.props.endpoint, token);
const query = graphql`
query remoteContainerQuery($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
defaultBranchRef {
prefix
name
}
}
}
`;
const variables = {
owner: this.props.remote.getOwner(),
name: this.props.remote.getRepo(),
};
return (
<QueryRenderer
environment={environment}
variables={variables}
query={query}
render={result => this.renderWithResult(result, token)}
/>
);
}
renderWithResult({error, props, retry}, token) {
if (error) {
return (
<QueryErrorView
error={error}
login={this.handleLogin}
retry={retry}
logout={this.handleLogout}
/>
);
}
if (props === null) {
return <LoadingView />;
}
return (
<RemoteController
endpoint={this.props.endpoint}
token={token}
repository={props.repository}
remoteOperationObserver={this.props.remoteOperationObserver}
workingDirectory={this.props.workingDirectory}
workspace={this.props.workspace}
remote={this.props.remote}
remotes={this.props.remotes}
branches={this.props.branches}
aheadCount={this.props.aheadCount}
pushInProgress={this.props.pushInProgress}
onPushBranch={this.props.onPushBranch}
/>
);
}
handleLogin(token) {
incrementCounter('github-login');
this.props.loginModel.setToken(this.props.endpoint.getLoginAccount(), token);
}
handleLogout() {
incrementCounter('github-logout');
this.props.loginModel.removeToken(this.props.endpoint.getLoginAccount());
}
}