This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathgit-tab-header-controller.js
More file actions
137 lines (118 loc) · 3.78 KB
/
git-tab-header-controller.js
File metadata and controls
137 lines (118 loc) · 3.78 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
import React from 'react';
import PropTypes from 'prop-types';
import {CompositeDisposable} from 'atom';
import {nullAuthor} from '../models/author';
import GitTabHeaderView from '../views/git-tab-header-view';
export default class GitTabHeaderController extends React.Component {
static propTypes = {
getCommitter: PropTypes.func.isRequired,
// Workspace
currentWorkDir: PropTypes.string,
getCurrentWorkDirs: PropTypes.func.isRequired,
changeWorkingDirectory: PropTypes.func.isRequired,
contextLocked: PropTypes.bool.isRequired,
setContextLock: PropTypes.func.isRequired,
// Event Handlers
onDidClickAvatar: PropTypes.func.isRequired,
onDidChangeWorkDirs: PropTypes.func.isRequired,
onDidUpdateRepo: PropTypes.func.isRequired,
}
constructor(props) {
super(props);
this._isMounted = false;
this.state = {
currentWorkDirs: [],
committer: nullAuthor,
changingLock: null,
changingWorkDir: null,
};
this.disposable = new CompositeDisposable();
}
static getDerivedStateFromProps(props) {
return {
currentWorkDirs: props.getCurrentWorkDirs(),
};
}
componentDidMount() {
this._isMounted = true;
this.disposable.add(this.props.onDidChangeWorkDirs(this.resetWorkDirs));
this.disposable.add(this.props.onDidUpdateRepo(this.updateCommitter));
this.updateCommitter();
}
componentDidUpdate(prevProps) {
if (
prevProps.onDidChangeWorkDirs !== this.props.onDidChangeWorkDirs
|| prevProps.onDidUpdateRepo !== this.props.onDidUpdateRepo
) {
this.disposable.dispose();
this.disposable = new CompositeDisposable();
this.disposable.add(this.props.onDidChangeWorkDirs(this.resetWorkDirs));
this.disposable.add(this.props.onDidUpdateRepo(this.updateCommitter));
}
if (prevProps.getCommitter !== this.props.getCommitter) {
this.updateCommitter();
}
}
render() {
return (
<GitTabHeaderView
committer={this.state.committer}
// Workspace
workdir={this.getWorkDir()}
workdirs={this.state.currentWorkDirs}
contextLocked={this.getLocked()}
changingWorkDir={this.state.changingWorkDir !== null}
changingLock={this.state.changingLock !== null}
// Event Handlers
handleAvatarClick={this.props.onDidClickAvatar}
handleWorkDirSelect={this.handleWorkDirSelect}
handleLockToggle={this.handleLockToggle}
/>
);
}
handleLockToggle = async () => {
if (this.state.changingLock !== null) {
return;
}
const nextLock = !this.props.contextLocked;
try {
this.setState({changingLock: nextLock});
await this.props.setContextLock(this.getWorkDir(), nextLock);
} finally {
await new Promise(resolve => this.setState({changingLock: null}, resolve));
}
}
handleWorkDirSelect = async e => {
if (this.state.changingWorkDir !== null) {
return;
}
const nextWorkDir = e.target.value;
try {
this.setState({changingWorkDir: nextWorkDir});
await this.props.changeWorkingDirectory(nextWorkDir);
} finally {
await new Promise(resolve => this.setState({changingWorkDir: null}, resolve));
}
}
resetWorkDirs = () => {
this.setState(() => ({
currentWorkDirs: [],
}));
}
updateCommitter = async () => {
const committer = await this.props.getCommitter() || nullAuthor;
if (this._isMounted) {
this.setState({committer});
}
}
getWorkDir() {
return this.state.changingWorkDir !== null ? this.state.changingWorkDir : this.props.currentWorkDir;
}
getLocked() {
return this.state.changingLock !== null ? this.state.changingLock : this.props.contextLocked;
}
componentWillUnmount() {
this._isMounted = false;
this.disposable.dispose();
}
}