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 402
/
Copy pathcommit-preview-container.js
101 lines (83 loc) · 2.76 KB
/
commit-preview-container.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
import React from 'react';
import PropTypes from 'prop-types';
import yubikiri from 'yubikiri';
import {CompositeDisposable, Emitter} from 'event-kit';
import ObserveModel from '../views/observe-model';
import LoadingView from '../views/loading-view';
import CommitPreviewController from '../controllers/commit-preview-controller';
import PatchBuffer from '../models/patch/patch-buffer';
export default class CommitPreviewContainer extends React.Component {
static propTypes = {
repository: PropTypes.object.isRequired,
largeDiffThreshold: PropTypes.number,
}
constructor(props) {
super(props);
this.emitter = new Emitter();
this.patchBuffer = new PatchBuffer();
this.lastMultiFilePatch = null;
this.sub = new CompositeDisposable();
this.state = {renderStatusOverrides: {}};
}
fetchData = repository => {
const builderOpts = {renderStatusOverrides: this.state.renderStatusOverrides};
if (this.props.largeDiffThreshold !== undefined) {
builderOpts.largeDiffThreshold = this.props.largeDiffThreshold;
}
const before = () => this.emitter.emit('will-update-patch');
const after = patch => this.emitter.emit('did-update-patch', patch);
return yubikiri({
multiFilePatch: repository.getStagedChangesPatch({
patchBuffer: this.patchBuffer,
builder: builderOpts,
before,
after,
}),
});
}
render() {
return (
<ObserveModel model={this.props.repository} fetchData={this.fetchData}>
{this.renderResult}
</ObserveModel>
);
}
renderResult = data => {
const currentMultiFilePatch = data && data.multiFilePatch;
if (currentMultiFilePatch !== this.lastMultiFilePatch) {
this.sub.dispose();
if (currentMultiFilePatch) {
this.sub = new CompositeDisposable(
...currentMultiFilePatch.getFilePatches().map(fp => fp.onDidChangeRenderStatus(() => {
this.setState(prevState => {
return {
renderStatusOverrides: {
...prevState.renderStatusOverrides,
[fp.getPath()]: fp.getRenderStatus(),
},
};
});
})),
);
}
this.lastMultiFilePatch = currentMultiFilePatch;
}
if (this.props.repository.isLoading() || data === null) {
return <LoadingView />;
}
return (
<CommitPreviewController
stagingStatus={'staged'}
onWillUpdatePatch={this.onWillUpdatePatch}
onDidUpdatePatch={this.onDidUpdatePatch}
{...data}
{...this.props}
/>
);
}
componentWillUnmount() {
this.sub.dispose();
}
onWillUpdatePatch = cb => this.emitter.on('will-update-patch', cb);
onDidUpdatePatch = cb => this.emitter.on('did-update-patch', cb);
}