Skip to content

Commit 0ed27e7

Browse files
authored
Add confirmation dialog (#1366)
1 parent da4710f commit 0ed27e7

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/components/BranchMenu.tsx

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ import {
66
} from '@jupyterlab/apputils';
77
import { TranslationBundle } from '@jupyterlab/translation';
88
import { CommandRegistry } from '@lumino/commands';
9+
import ClearIcon from '@mui/icons-material/Clear';
910
import List from '@mui/material/List';
1011
import ListItem from '@mui/material/ListItem';
11-
import ClearIcon from '@mui/icons-material/Clear';
1212
import * as React from 'react';
1313
import { FixedSizeList, ListChildComponentProps } from 'react-window';
1414
import { classes } from 'typestyle';
15+
import { showError } from '../notifications';
1516
import { hiddenButtonStyle } from '../style/ActionButtonStyle';
1617
import {
1718
activeListItemClass,
18-
nameClass,
1919
filterClass,
2020
filterClearClass,
2121
filterInputClass,
2222
filterWrapperClass,
2323
listItemClass,
2424
listItemIconClass,
25+
nameClass,
2526
newBranchButtonClass,
2627
wrapperClass
2728
} from '../style/BranchMenu';
2829
import { branchIcon, mergeIcon, trashIcon } from '../style/icons';
2930
import { CommandIDs, Git, IGitExtension } from '../tokens';
3031
import { ActionButton } from './ActionButton';
3132
import { NewBranchDialog } from './NewBranchDialog';
32-
import { showError } from '../notifications';
3333

3434
const ITEM_HEIGHT = 24.8; // HTML element height for a single branch
3535
const MIN_HEIGHT = 150; // Minimal HTML element height for the branches list
@@ -274,7 +274,10 @@ export class BranchMenu extends React.Component<
274274
listItemClass,
275275
isActive ? activeListItemClass : null
276276
)}
277-
onClick={this._onBranchClickFactory(branch.name)}
277+
onClick={this._onBranchClickFactory(
278+
branch.name,
279+
branch.is_remote_branch
280+
)}
278281
role="listitem"
279282
style={style}
280283
>
@@ -425,7 +428,7 @@ export class BranchMenu extends React.Component<
425428
* @param branch - branch name
426429
* @returns callback
427430
*/
428-
private _onBranchClickFactory(branch: string) {
431+
private _onBranchClickFactory(branch: string, isRemote: boolean) {
429432
/**
430433
* Callback invoked upon clicking a branch name.
431434
*
@@ -441,6 +444,9 @@ export class BranchMenu extends React.Component<
441444
);
442445
return;
443446
}
447+
448+
let message = 'Switched branch.';
449+
let type: Notification.TypeOptions = 'success';
444450
const opts = {
445451
branchname: branch
446452
};
@@ -452,15 +458,42 @@ export class BranchMenu extends React.Component<
452458
);
453459

454460
try {
455-
await this.props.model.checkout(opts);
461+
let shouldCheckout = true;
462+
const localBranchname = branch.split('/').slice(-1)[0];
463+
const localBranchExists = this.props.model.branches.find(
464+
branch => branch.name === localBranchname
465+
);
466+
467+
if (localBranchExists && isRemote) {
468+
const result = await showDialog({
469+
title: this.props.trans.__('Confirm Checkout'),
470+
body: this.props.trans.__(
471+
'Checking out this branch will reset your local copy to the remote. Any changes not pushed to the remote will be lost. Are you sure you want to continue?'
472+
),
473+
buttons: [
474+
Dialog.cancelButton({ label: this.props.trans.__('Cancel') }),
475+
Dialog.warnButton({ label: this.props.trans.__('Continue') })
476+
]
477+
});
478+
479+
if (!result.button.accept) {
480+
shouldCheckout = false;
481+
message = 'Checkout cancelled.';
482+
type = 'warning';
483+
}
484+
}
485+
486+
if (shouldCheckout) {
487+
await this.props.model.checkout(opts);
488+
}
456489
} catch (err) {
457490
return onBranchError(err, id, this.props.trans);
458491
}
459492

460493
Notification.update({
461494
id,
462-
message: this.props.trans.__('Switched branch.'),
463-
type: 'success',
495+
message: this.props.trans.__(message),
496+
type,
464497
autoClose: 5000
465498
});
466499
};

0 commit comments

Comments
 (0)