This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 392
/
recent-commits-controller.js
68 lines (59 loc) · 2.15 KB
/
recent-commits-controller.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
import React from 'react';
import PropTypes from 'prop-types';
import {autobind} from '../helpers';
import {addEvent} from '../reporter-proxy';
import CommitDetailItem from '../items/commit-detail-item';
import URIPattern from '../atom/uri-pattern';
import RecentCommitsView from '../views/recent-commits-view';
import {CompositeDisposable} from 'event-kit';
export default class RecentCommitsController extends React.Component {
static propTypes = {
commits: PropTypes.arrayOf(PropTypes.object).isRequired,
isLoading: PropTypes.bool.isRequired,
undoLastCommit: PropTypes.func.isRequired,
workspace: PropTypes.object.isRequired,
repository: PropTypes.object.isRequired,
}
constructor(props, context) {
super(props, context);
autobind(this, 'openCommit', 'updateSelectedCommit');
this.subscriptions = new CompositeDisposable(
this.props.workspace.onDidChangeActivePaneItem(this.updateSelectedCommit),
);
this.state = {selectedCommitSha: ''};
}
updateSelectedCommit() {
const activeItem = this.props.workspace.getActivePaneItem();
const pattern = new URIPattern(decodeURIComponent(
CommitDetailItem.buildURI(
this.props.repository.getWorkingDirectoryPath(),
'{sha}'),
));
if (activeItem && activeItem.getURI) {
const match = pattern.matches(activeItem.getURI());
const {sha} = match.getParams();
if (match.ok() && sha && sha !== this.state.selectedCommitSha) {
return new Promise(resolve => this.setState({selectedCommitSha: sha}, resolve));
}
}
return Promise.resolve();
}
render() {
return (
<RecentCommitsView
commits={this.props.commits}
isLoading={this.props.isLoading}
undoLastCommit={this.props.undoLastCommit}
openCommit={this.openCommit}
selectedCommitSha={this.state.selectedCommitSha}
/>
);
}
openCommit({sha}) {
const workdir = this.props.repository.getWorkingDirectoryPath();
const uri = CommitDetailItem.buildURI(workdir, sha);
this.props.workspace.open(uri).then(() => {
addEvent('open-commit-in-pane', {package: 'github', from: 'recent commit'});
});
}
}