This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
branch-menu-view.js
152 lines (134 loc) · 4.81 KB
/
branch-menu-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
import React from 'react';
import PropTypes from 'prop-types';
import {autobind} from 'core-decorators';
import cx from 'classnames';
import Commands, {Command} from '../atom/commands';
import {BranchPropType, BranchSetPropType} from '../prop-types';
import {GitError} from '../git-shell-out-strategy';
export default class BranchMenuView extends React.Component {
static propTypes = {
workspace: PropTypes.object.isRequired,
commandRegistry: PropTypes.object.isRequired,
notificationManager: PropTypes.object.isRequired,
repository: PropTypes.object,
branches: BranchSetPropType.isRequired,
currentBranch: BranchPropType.isRequired,
checkout: PropTypes.func,
}
static defaultProps = {
checkout: () => Promise.resolve(),
}
constructor(props, context) {
super(props, context);
this.state = {
createNew: false,
checkedOutBranch: null,
};
}
render() {
const branchNames = this.props.branches.getNames();
let currentBranchName = this.props.currentBranch.isDetached() ? 'detached' : this.props.currentBranch.getName();
if (this.state.checkedOutBranch) {
currentBranchName = this.state.checkedOutBranch;
if (branchNames.indexOf(this.state.checkedOutBranch) === -1) {
branchNames.push(this.state.checkedOutBranch);
}
}
const disableControls = !!this.state.checkedOutBranch;
const branchEditorClasses = cx('github-BranchMenuView-item', 'github-BranchMenuView-editor', {
hidden: !this.state.createNew,
});
const branchSelectListClasses = cx('github-BranchMenuView-item', 'github-BranchMenuView-select', 'input-select', {
hidden: !!this.state.createNew,
});
const iconClasses = cx('github-BranchMenuView-item', 'icon', {
'icon-git-branch': !disableControls,
'icon-sync': disableControls,
});
const newBranchEditor = (
<div className={branchEditorClasses}>
<atom-text-editor
ref={e => { this.editorElement = e; }}
mini={true}
readonly={disableControls ? true : undefined}
/>
</div>
);
const selectBranchView = (
<select
className={branchSelectListClasses}
onChange={this.didSelectItem}
disabled={disableControls}
value={currentBranchName}>
{this.props.currentBranch.isDetached() &&
<option key="detached" value="detached" disabled>{this.props.currentBranch.getName()}</option>
}
{branchNames.map(branchName => {
return <option key={branchName} value={branchName}>{branchName}</option>;
})}
</select>
);
return (
<div className="github-BranchMenuView">
<Commands registry={this.props.commandRegistry} target=".github-BranchMenuView-editor atom-text-editor[mini]">
<Command command="tool-panel:unfocus" callback={this.cancelCreateNewBranch} />
<Command command="core:cancel" callback={this.cancelCreateNewBranch} />
<Command command="core:confirm" callback={this.createBranch} />
</Commands>
<div className="github-BranchMenuView-selector">
<span className={iconClasses} />
{newBranchEditor}
{selectBranchView}
<button className="github-BranchMenuView-item github-BranchMenuView-button btn"
onClick={this.createBranch} disabled={disableControls}> New Branch </button>
</div>
</div>
);
}
componentDidUpdate() {
const currentBranch = this.props.currentBranch.getName();
const branchNames = this.props.branches.getNames();
const hasNewBranch = branchNames.includes(this.state.checkedOutBranch);
if (currentBranch === this.state.checkedOutBranch && hasNewBranch) {
this.editorElement.classList.add('is-focused');
this.setState({checkedOutBranch: null, createNew: false});
}
}
@autobind
async didSelectItem(event) {
const branchName = event.target.value;
await this.checkout(branchName);
}
@autobind
async createBranch() {
if (this.state.createNew) {
const branchName = this.editorElement.innerText.trim();
await this.checkout(branchName, {createNew: true});
} else {
this.setState({createNew: true}, () => {
this.editorElement.focus();
});
}
}
@autobind
async checkout(branchName, options) {
this.editorElement.classList.remove('is-focused');
this.setState({checkedOutBranch: branchName});
try {
await this.props.checkout(branchName, options);
} catch (error) {
this.editorElement.classList.add('is-focused');
this.setState({checkedOutBranch: null});
if (error instanceof GitError) {
// eslint-disable-next-line no-console
console.warn('Non-fatal', error);
} else {
throw error;
}
}
}
@autobind
cancelCreateNewBranch() {
this.setState({createNew: false});
}
}