-
Notifications
You must be signed in to change notification settings - Fork 308
/
Toolbar.tsx
359 lines (331 loc) · 9.13 KB
/
Toolbar.tsx
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { Dialog, showDialog } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import {
caretDownIcon,
caretUpIcon,
refreshIcon
} from '@jupyterlab/ui-components';
import * as React from 'react';
import { classes } from 'typestyle';
import { branchIcon, desktopIcon, pullIcon, pushIcon } from '../style/icons';
import {
spacer,
toolbarButtonClass,
toolbarClass,
toolbarMenuButtonClass,
toolbarMenuButtonEnabledClass,
toolbarMenuButtonIconClass,
toolbarMenuButtonSubtitleClass,
toolbarMenuButtonTitleClass,
toolbarMenuButtonTitleWrapperClass,
toolbarMenuWrapperClass,
toolbarNavClass
} from '../style/Toolbar';
import { IGitExtension } from '../tokens';
import { GitCredentialsForm } from '../widgets/CredentialsBox';
import { GitPullPushDialog, Operation } from '../widgets/gitPushPull';
import { ActionButton } from './ActionButton';
import { BranchMenu } from './BranchMenu';
/**
* Displays an error dialog when a Git operation fails.
*
* @private
* @param model - Git extension model
* @param operation - Git operation name
* @returns Promise for displaying a dialog
*/
async function showGitOperationDialog(
model: IGitExtension,
operation: Operation
): Promise<void> {
const title = `Git ${operation}`;
let result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
let retry = false;
while (!result.button.accept) {
const credentials = await showDialog({
title: 'Git credentials required',
body: new GitCredentialsForm(
'Enter credentials for remote repository',
retry ? 'Incorrect username or password.' : ''
),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
});
if (!credentials.button.accept) {
break;
}
result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation, credentials.value),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
retry = true;
}
}
/**
* Interface describing component properties.
*/
export interface IToolbarProps {
/**
* Git extension data model.
*/
model: IGitExtension;
/**
* Boolean indicating whether branching is disabled.
*/
branching: boolean;
/**
* Callback to invoke in order to refresh a repository.
*
* @returns promise which refreshes a repository
*/
refresh: () => Promise<void>;
}
/**
* Interface describing component state.
*/
export interface IToolbarState {
/**
* Boolean indicating whether a branch menu is shown.
*/
branchMenu: boolean;
/**
* Boolean indicating whether a repository menu is shown.
*/
repoMenu: boolean;
/**
* Current repository.
*/
repository: string;
/**
* Current branch name.
*/
branch: string;
}
/**
* React component for rendering a panel toolbar.
*/
export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
/**
* Returns a React component for rendering a panel toolbar.
*
* @param props - component properties
* @returns React component
*/
constructor(props: IToolbarProps) {
super(props);
const repo = this.props.model.pathRepository;
this.state = {
branchMenu: false,
repoMenu: false,
repository: repo || '',
branch: repo ? this.props.model.currentBranch.name : ''
};
}
/**
* Callback invoked immediately after mounting a component (i.e., inserting into a tree).
*/
componentDidMount(): void {
this._addListeners();
}
/**
* Callback invoked when a component will no longer be mounted.
*/
componentWillUnmount(): void {
this._removeListeners();
}
/**
* Renders the component.
*
* @returns React element
*/
render(): React.ReactElement {
return (
<div className={toolbarClass}>
{this._renderTopNav()}
{this._renderRepoMenu()}
{this._renderBranchMenu()}
</div>
);
}
/**
* Renders the top navigation.
*
* @returns React element
*/
private _renderTopNav(): React.ReactElement {
return (
<div className={toolbarNavClass}>
<span className={spacer} />
<ActionButton
className={toolbarButtonClass}
icon={pullIcon}
onClick={this._onPullClick}
title={'Pull latest changes'}
/>
<ActionButton
className={toolbarButtonClass}
icon={pushIcon}
onClick={this._onPushClick}
title={'Push committed changes'}
/>
<ActionButton
className={toolbarButtonClass}
icon={refreshIcon}
onClick={this._onRefreshClick}
title={'Refresh the repository to detect local and remote changes'}
/>
</div>
);
}
/**
* Renders a repository menu.
*
* @returns React element
*/
private _renderRepoMenu(): React.ReactElement {
return (
<div className={toolbarMenuWrapperClass}>
<button
disabled
className={toolbarMenuButtonClass}
title={`Current repository: ${this.state.repository}`}
onClick={this._onRepositoryClick}
>
<desktopIcon.react className={toolbarMenuButtonIconClass} />
<div className={toolbarMenuButtonTitleWrapperClass}>
<p className={toolbarMenuButtonTitleClass}>Current Repository</p>
<p className={toolbarMenuButtonSubtitleClass}>
{PathExt.basename(this.state.repository)}
</p>
</div>
</button>
{this.state.repoMenu ? null : null}
</div>
);
}
/**
* Renders a branch menu.
*
* @returns React element
*/
private _renderBranchMenu(): React.ReactElement | null {
if (!this.state.repository) {
return null;
}
return (
<div className={toolbarMenuWrapperClass}>
<button
className={classes(
toolbarMenuButtonClass,
toolbarMenuButtonEnabledClass
)}
title={`Change the current branch: ${this.state.branch}`}
onClick={this._onBranchClick}
>
<branchIcon.react className={toolbarMenuButtonIconClass} />
<div className={toolbarMenuButtonTitleWrapperClass}>
<p className={toolbarMenuButtonTitleClass}>Current Branch</p>
<p className={toolbarMenuButtonSubtitleClass}>
{this.state.branch}
</p>
</div>
{this.state.branchMenu ? (
<caretUpIcon.react className={toolbarMenuButtonIconClass} />
) : (
<caretDownIcon.react className={toolbarMenuButtonIconClass} />
)}
</button>
{this.state.branchMenu ? (
<BranchMenu
model={this.props.model}
branching={this.props.branching}
/>
) : null}
</div>
);
}
/**
* Adds model listeners.
*/
private _addListeners(): void {
// When the HEAD changes, decent probability that we've switched branches:
this.props.model.headChanged.connect(this._syncState, this);
// When the status changes, we may have checked out a new branch (e.g., via the command-line and not via the extension) or changed repositories:
this.props.model.statusChanged.connect(this._syncState, this);
}
/**
* Removes model listeners.
*/
private _removeListeners(): void {
this.props.model.headChanged.disconnect(this._syncState, this);
this.props.model.statusChanged.disconnect(this._syncState, this);
}
/**
* Syncs the component state with the underlying model.
*/
private _syncState(): void {
const repo = this.props.model.pathRepository;
this.setState({
repository: repo || '',
branch: repo ? this.props.model.currentBranch.name : ''
});
}
/**
* Callback invoked upon clicking a button to pull the latest changes.
*
* @param event - event object
*/
private _onPullClick = (): void => {
showGitOperationDialog(this.props.model, Operation.Pull).catch(reason => {
console.error(
`Encountered an error when pulling changes. Error: ${reason}`
);
});
};
/**
* Callback invoked upon clicking a button to push the latest changes.
*
* @param event - event object
*/
private _onPushClick = (): void => {
showGitOperationDialog(this.props.model, Operation.Push).catch(reason => {
console.error(
`Encountered an error when pushing changes. Error: ${reason}`
);
});
};
/**
* Callback invoked upon clicking a button to change the current repository.
*
* @param event - event object
*/
private _onRepositoryClick = (): void => {
// Toggle the repository menu:
this.setState({
repoMenu: !this.state.repoMenu
});
};
/**
* Callback invoked upon clicking a button to change the current branch.
*
* @param event - event object
*/
private _onBranchClick = (): void => {
// Toggle the branch menu:
this.setState({
branchMenu: !this.state.branchMenu
});
};
/**
* Callback invoked upon clicking a button to refresh a repository.
*
* @param event - event object
*/
private _onRefreshClick = (): void => {
this.props.refresh();
};
}