-
Notifications
You must be signed in to change notification settings - Fork 317
/
BranchMenu.tsx
431 lines (403 loc) · 10.8 KB
/
BranchMenu.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { Dialog, showDialog, showErrorMessage } from '@jupyterlab/apputils';
import { TranslationBundle } from '@jupyterlab/translation';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ClearIcon from '@material-ui/icons/Clear';
import * as React from 'react';
import { FixedSizeList, ListChildComponentProps } from 'react-window';
import { classes } from 'typestyle';
import { Logger } from '../logger';
import { hiddenButtonStyle } from '../style/ActionButtonStyle';
import {
activeListItemClass,
nameClass,
filterClass,
filterClearClass,
filterInputClass,
filterWrapperClass,
listItemClass,
listItemIconClass,
newBranchButtonClass,
wrapperClass
} from '../style/BranchMenu';
import { branchIcon, trashIcon } from '../style/icons';
import { Git, IGitExtension, Level } from '../tokens';
import { ActionButton } from './ActionButton';
import { NewBranchDialog } from './NewBranchDialog';
const ITEM_HEIGHT = 24.8; // HTML element height for a single branch
const MIN_HEIGHT = 150; // Minimal HTML element height for the branches list
const MAX_HEIGHT = 400; // Maximal HTML element height for the branches list
/**
* Callback invoked upon encountering an error when switching branches.
*
* @private
* @param error - error
* @param logger - the logger
*/
function onBranchError(
error: any,
logger: Logger,
trans: TranslationBundle
): void {
if (error.message.includes('following files would be overwritten')) {
// Empty log message to hide the executing alert
logger.log({
message: '',
level: Level.INFO
});
showDialog({
title: trans.__('Unable to switch branch'),
body: (
<React.Fragment>
<p>
{trans.__(
'Your changes to the following files would be overwritten by switching:'
)}
</p>
<List>
{error.message.split('\n').slice(1, -3).map(renderFileName)}
</List>
<span>
{trans.__(
'Please commit, stash, or discard your changes before you switch branches.'
)}
</span>
</React.Fragment>
),
buttons: [Dialog.okButton({ label: trans.__('Dismiss') })]
});
} else {
logger.log({
level: Level.ERROR,
message: trans.__('Failed to switch branch.'),
error
});
}
}
/**
* Renders a file name.
*
* @private
* @param filename - file name
* @returns React element
*/
function renderFileName(filename: string): React.ReactElement {
return <ListItem key={filename}>{filename}</ListItem>;
}
/**
* Interface describing component properties.
*/
export interface IBranchMenuProps {
/**
* Current branch name.
*/
currentBranch: string;
/**
* Current list of branches.
*/
branches: Git.IBranch[];
/**
* Boolean indicating whether branching is disabled.
*/
branching: boolean;
/**
* Extension logger
*/
logger: Logger;
/**
* Git extension data model.
*/
model: IGitExtension;
/**
* The application language translator.
*/
trans: TranslationBundle;
}
/**
* Interface describing component state.
*/
export interface IBranchMenuState {
/**
* Boolean indicating whether to show a dialog to create a new branch.
*/
branchDialog: boolean;
/**
* Menu filter.
*/
filter: string;
}
/**
* React component for rendering a branch menu.
*/
export class BranchMenu extends React.Component<
IBranchMenuProps,
IBranchMenuState
> {
CHANGES_ERR_MSG = this.props.trans.__(
'The current branch contains files with uncommitted changes. Please commit or discard these changes before switching to or creating another branch.'
);
/**
* Returns a React component for rendering a branch menu.
*
* @param props - component properties
* @returns React component
*/
constructor(props: IBranchMenuProps) {
super(props);
this.state = {
filter: '',
branchDialog: false
};
}
/**
* Renders the component.
*
* @returns React element
*/
render(): React.ReactElement {
return (
<div className={wrapperClass}>
{this._renderFilter()}
{this._renderBranchList()}
{this._renderNewBranchDialog()}
</div>
);
}
/**
* Renders a branch input filter.
*
* @returns React element
*/
private _renderFilter(): React.ReactElement {
return (
<div className={filterWrapperClass}>
<div className={filterClass}>
<input
className={filterInputClass}
type="text"
onChange={this._onFilterChange}
value={this.state.filter}
placeholder={this.props.trans.__('Filter')}
title={this.props.trans.__('Filter branch menu')}
/>
{this.state.filter ? (
<button className={filterClearClass}>
<ClearIcon
titleAccess={this.props.trans.__('Clear the current filter')}
fontSize="small"
onClick={this._resetFilter}
/>
</button>
) : null}
</div>
<input
className={newBranchButtonClass}
type="button"
title={this.props.trans.__('Create a new branch')}
value={this.props.trans.__('New Branch')}
onClick={this._onNewBranchClick}
/>
</div>
);
}
/**
* Renders a
*
* @returns React element
*/
private _renderBranchList(): React.ReactElement {
// Perform a "simple" filter... (TODO: consider implementing fuzzy filtering)
const filter = this.state.filter;
const branches = this.props.branches.filter(
branch => !filter || branch.name.includes(filter)
);
return (
<FixedSizeList
height={Math.min(
Math.max(MIN_HEIGHT, branches.length * ITEM_HEIGHT),
MAX_HEIGHT
)}
itemCount={branches.length}
itemData={branches}
itemKey={(index, data) => data[index].name}
itemSize={ITEM_HEIGHT}
style={{ overflowX: 'hidden', paddingTop: 0, paddingBottom: 0 }}
width={'auto'}
>
{this._renderItem}
</FixedSizeList>
);
}
/**
* Renders a menu item.
*
* @param props Row properties
* @returns React element
*/
private _renderItem = (props: ListChildComponentProps): JSX.Element => {
const { data, index, style } = props;
const branch = data[index] as Git.IBranch;
const isActive = branch.name === this.props.currentBranch;
return (
<ListItem
button
title={this.props.trans.__('Switch to branch: %1', branch.name)}
className={classes(
listItemClass,
isActive ? activeListItemClass : null
)}
onClick={this._onBranchClickFactory(branch.name)}
style={style}
>
<branchIcon.react className={listItemIconClass} tag="span" />
<span className={nameClass}>{branch.name}</span>
{!branch.is_remote_branch && !isActive && (
<ActionButton
className={hiddenButtonStyle}
icon={trashIcon}
title={this.props.trans.__('Delete this branch locally')}
onClick={(event: React.MouseEvent) => {
event.stopPropagation();
this._onDeleteBranch(branch.name);
}}
/>
)}
</ListItem>
);
};
/**
* Renders a dialog for creating a new branch.
*
* @returns React element
*/
private _renderNewBranchDialog(): React.ReactElement {
return (
<NewBranchDialog
currentBranch={this.props.currentBranch}
branches={this.props.branches}
logger={this.props.logger}
open={this.state.branchDialog}
model={this.props.model}
onClose={this._onNewBranchDialogClose}
trans={this.props.trans}
/>
);
}
/**
* Callback invoked upon a change to the menu filter.
*
* @param event - event object
*/
private _onFilterChange = (event: any): void => {
this.setState({
filter: event.target.value
});
};
/**
* Callback invoked to reset the menu filter.
*/
private _resetFilter = (): void => {
this.setState({
filter: ''
});
};
/**
* Callback on delete branch name button
*
* @param branchName Branch name
*/
private _onDeleteBranch = async (branchName: string): Promise<void> => {
const acknowledgement = await showDialog<void>({
title: this.props.trans.__('Delete branch'),
body: (
<p>
{this.props.trans.__(
'Are you sure you want to permanently delete the branch '
)}
<b>{branchName}</b>?
<br />
{this.props.trans.__('This action cannot be undone.')}
</p>
),
buttons: [
Dialog.cancelButton({ label: this.props.trans.__('Cancel') }),
Dialog.warnButton({ label: this.props.trans.__('Delete') })
]
});
if (acknowledgement.button.accept) {
try {
await this.props.model.deleteBranch(branchName);
await this.props.model.refreshBranch();
} catch (error) {
console.error(`Failed to delete branch ${branchName}`, error);
}
}
};
/**
* Callback invoked upon clicking a button to create a new branch.
*
* @param event - event object
*/
private _onNewBranchClick = (): void => {
if (!this.props.branching) {
showErrorMessage(
this.props.trans.__('Creating a new branch is disabled'),
this.CHANGES_ERR_MSG
);
return;
}
this.setState({
branchDialog: true
});
};
/**
* Callback invoked upon closing a dialog to create a new branch.
*/
private _onNewBranchDialogClose = (): void => {
this.setState({
branchDialog: false
});
};
/**
* Returns a callback which is invoked upon clicking a branch name.
*
* @param branch - branch name
* @returns callback
*/
private _onBranchClickFactory(branch: string) {
const self = this;
return onClick;
/**
* Callback invoked upon clicking a branch name.
*
* @private
* @param event - event object
* @returns promise which resolves upon attempting to switch branches
*/
async function onClick(): Promise<void> {
if (!self.props.branching) {
showErrorMessage(
self.props.trans.__('Switching branches is disabled'),
self.CHANGES_ERR_MSG
);
return;
}
const opts = {
branchname: branch
};
self.props.logger.log({
level: Level.RUNNING,
message: self.props.trans.__('Switching branch...')
});
try {
await self.props.model.checkout(opts);
} catch (err) {
return onBranchError(err, self.props.logger, self.props.trans);
}
self.props.logger.log({
level: Level.SUCCESS,
message: self.props.trans.__('Switched branch.')
});
}
}
}