-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.tsx
209 lines (181 loc) · 5.25 KB
/
index.tsx
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as React from 'react';
import { Col, Row } from 'react-bootstrap';
import { RouteComponentProps } from 'react-router-dom';
import { connect } from 'react-redux';
import { ApplicationState } from '../../reducers';
import { ConnectedReduxProps } from '../../configureStore';
import FileTree from '../../components/FileTree';
import DiffView from '../../components/DiffView';
import Loading from '../../components/Loading';
import VersionChooser from '../../components/VersionChooser';
import {
CompareInfo,
Version,
fetchDiff,
getVersionInfo,
viewVersionFile,
} from '../../reducers/versions';
import { gettext, getPathFromQueryString } from '../../utils';
import styles from './styles.module.scss';
export type PublicProps = {
_fetchDiff: typeof fetchDiff;
_viewVersionFile: typeof viewVersionFile;
};
type PropsFromRouter = {
addonId: string;
baseVersionId: string;
headVersionId: string;
lang: string;
};
type PropsFromState = {
addonId: number;
compareInfo: CompareInfo | null | void;
version: Version | void | null;
};
type Props = RouteComponentProps<PropsFromRouter> &
PropsFromState &
PublicProps &
ConnectedReduxProps;
export class CompareBase extends React.Component<Props> {
static defaultProps = {
_fetchDiff: fetchDiff,
_viewVersionFile: viewVersionFile,
};
componentDidMount() {
this.loadData();
}
componentDidUpdate(prevProps: Props) {
this.loadData(prevProps);
}
loadData(prevProps?: Props) {
const {
_fetchDiff,
compareInfo,
dispatch,
history,
match,
version,
} = this.props;
const { addonId, baseVersionId, headVersionId, lang } = match.params;
const oldVersionId = parseInt(baseVersionId, 10);
const newVersionId = parseInt(headVersionId, 10);
// We ensure the new version ID is newer than the old version ID when
// loading the page.
if (oldVersionId > newVersionId) {
history.push(
`/${lang}/compare/${addonId}/versions/${headVersionId}...${baseVersionId}/`,
);
return;
}
const path = getPathFromQueryString(history);
if (compareInfo === null) {
// An error has occured when fetching the version.
return;
}
if (compareInfo === undefined) {
dispatch(
_fetchDiff({
addonId: parseInt(addonId, 10),
baseVersionId: oldVersionId,
headVersionId: newVersionId,
path: path || undefined,
}),
);
return;
}
if (!prevProps || !version) {
return;
}
if (
(prevProps.version &&
version.selectedPath !== prevProps.version.selectedPath) ||
addonId !== prevProps.match.params.addonId ||
baseVersionId !== prevProps.match.params.baseVersionId ||
headVersionId !== prevProps.match.params.headVersionId
) {
dispatch(
_fetchDiff({
addonId: parseInt(addonId, 10),
baseVersionId: oldVersionId,
headVersionId: newVersionId,
path: version.selectedPath,
}),
);
}
}
viewVersionFile = (path: string) => {
const { _viewVersionFile, dispatch, match } = this.props;
const { headVersionId } = match.params;
dispatch(
_viewVersionFile({
selectedPath: path,
versionId: parseInt(headVersionId, 10),
// When selecting a new file to view, we do not want to preserve the
// hash in the URL (this hash highlights a specific line of code).
preserveHash: false,
}),
);
};
renderLoadingMessageOrError(message: string) {
const { compareInfo } = this.props;
if (compareInfo === null) {
return (
<p className={styles.error}>
{gettext('Ooops, an error has occured.')}
</p>
);
}
return <Loading message={message} />;
}
render() {
const { addonId, compareInfo, version } = this.props;
return (
<React.Fragment>
<Col md="3">
{version ? (
<FileTree onSelect={this.viewVersionFile} versionId={version.id} />
) : (
this.renderLoadingMessageOrError(gettext('Loading file tree...'))
)}
</Col>
<Col md="9">
<Row>
<Col>
<VersionChooser addonId={addonId} />
</Col>
</Row>
<Row>
<Col>
{version && compareInfo ? (
<DiffView
diffs={compareInfo.diffs}
mimeType={compareInfo.mimeType}
version={version}
/>
) : (
this.renderLoadingMessageOrError(gettext('Loading diff...'))
)}
</Col>
</Row>
</Col>
</React.Fragment>
);
}
}
const mapStateToProps = (
state: ApplicationState,
ownProps: RouteComponentProps<PropsFromRouter>,
): PropsFromState => {
const { match } = ownProps;
const addonId = parseInt(match.params.addonId, 10);
const headVersionId = parseInt(match.params.headVersionId, 10);
const { compareInfo } = state.versions;
// The Compare API returns the version info of the head/newest version.
const version = getVersionInfo(state.versions, headVersionId);
return {
addonId,
compareInfo,
version,
};
};
export default connect(mapStateToProps)(CompareBase);