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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// @flow
import React from 'react';
import { max } from 'lodash';
import { intlShape } from 'react-intl';
import DateFromNow from '../../../components/intl/DateFromNow';
import { longFormatterOption } from '../../../components/intl/DateFormatter';
import DateTimeFormatter from '../../../components/intl/DateTimeFormatter';
import FacetBox from '../../../components/facet/FacetBox';
import FacetHeader from '../../../components/facet/FacetHeader';
import FacetItem from '../../../components/facet/FacetItem';
import { BarChart } from '../../../components/charts/bar-chart';
import DateInput from '../../../components/controls/DateInput';
import { isSameDay, parseDate, toShortNotSoISOString } from '../../../helpers/dates';
import { translate } from '../../../helpers/l10n';
import { formatMeasure } from '../../../helpers/measures';
/*:: import type { Component } from '../utils'; */
/*::
type Props = {|
component?: Component,
createdAfter: string,
createdAt: string,
createdBefore: string,
createdInLast: string,
facetMode: string,
onChange: (changes: {}) => void,
onToggle: (property: string) => void,
open: boolean,
sinceLeakPeriod: boolean,
stats?: { [string]: number }
|};
*/
export default class CreationDateFacet extends React.PureComponent {
/*:: props: Props; */
property = 'createdAt';
static defaultProps = {
open: true
};
static contextTypes = {
intl: intlShape
};
hasValue = () =>
this.props.createdAfter.length > 0 ||
this.props.createdAt.length > 0 ||
this.props.createdBefore.length > 0 ||
this.props.createdInLast.length > 0 ||
this.props.sinceLeakPeriod;
handleHeaderClick = () => {
this.props.onToggle(this.property);
};
handleClear = () => {
this.resetTo({});
};
resetTo = (changes /*: {} */) => {
this.props.onChange({
createdAfter: undefined,
createdAt: undefined,
createdBefore: undefined,
createdInLast: undefined,
sinceLeakPeriod: undefined,
...changes
});
};
handleBarClick = (
{ createdAfter, createdBefore } /*: {
createdAfter: Date,
createdBefore?: Date
} */
) => {
this.resetTo({
createdAfter: toShortNotSoISOString(createdAfter),
createdBefore: createdBefore && toShortNotSoISOString(createdBefore)
});
};
handlePeriodChange = (property /*: string */, value /*: string */) => {
this.props.onChange({
createdAt: undefined,
createdInLast: undefined,
sinceLeakPeriod: undefined,
[property]: value ? toShortNotSoISOString(parseDate(value)) : undefined
});
};
handlePeriodChangeBefore = (value /*: string */) =>
this.handlePeriodChange('createdBefore', value);
handlePeriodChangeAfter = (value /*: string */) => this.handlePeriodChange('createdAfter', value);
handlePeriodClick = (period /*: string */) => this.resetTo({ createdInLast: period });
handleLeakPeriodClick = () => this.resetTo({ sinceLeakPeriod: true });
getValues() {
const { createdAfter, createdAt, createdBefore, createdInLast, sinceLeakPeriod } = this.props;
const { formatDate } = this.context.intl;
const values = [];
if (createdAfter) {
values.push(formatDate(createdAfter, longFormatterOption));
}
if (createdAt) {
values.push(formatDate(createdAt, longFormatterOption));
}
if (createdBefore) {
values.push(formatDate(createdBefore, longFormatterOption));
}
if (createdInLast === '1w') {
values.push(translate('issues.facet.createdAt.last_week'));
}
if (createdInLast === '1m') {
values.push(translate('issues.facet.createdAt.last_month'));
}
if (createdInLast === '1y') {
values.push(translate('issues.facet.createdAt.last_year'));
}
if (sinceLeakPeriod) {
values.push(translate('issues.leak_period'));
}
return values;
}
renderBarChart() {
const { createdBefore, stats } = this.props;
if (!stats) {
return null;
}
const periods = Object.keys(stats);
if (periods.length < 2 || periods.every(period => !stats[period])) {
return null;
}
const { formatDate } = this.context.intl;
const data = periods.map((start, index) => {
const startDate = parseDate(start);
let endDate;
if (index < periods.length - 1) {
endDate = parseDate(periods[index + 1]);
endDate.setDate(endDate.getDate() - 1);
} else {
endDate = createdBefore && parseDate(createdBefore);
}
const tooltipEndDate = endDate || Date.now();
const tooltip = (
<React.Fragment>
{formatMeasure(stats[start], 'SHORT_INT')}
<br />
{formatDate(startDate, longFormatterOption)}
{!isSameDay(tooltipEndDate, startDate) &&
` - ${formatDate(tooltipEndDate, longFormatterOption)}`}
</React.Fragment>
);
return {
createdAfter: startDate,
createdBefore: endDate,
tooltip,
x: index,
y: stats[start]
};
});
const barsWidth = Math.floor(240 / data.length);
const width = barsWidth * data.length - 1 + 20;
const maxValue = max(data.map(d => d.y));
const format = this.props.facetMode === 'count' ? 'SHORT_INT' : 'SHORT_WORK_DUR';
const xValues = data.map(d => (d.y === maxValue ? formatMeasure(maxValue, format) : ''));
return (
<BarChart
barsWidth={barsWidth - 1}
data={data}
height={75}
onBarClick={this.handleBarClick}
padding={[25, 10, 5, 10]}
width={width}
xValues={xValues}
/>
);
}
renderExactDate() {
return (
<div className="search-navigator-facet-container">
<DateTimeFormatter date={this.props.createdAt} />
<br />
<span className="note">
<DateFromNow date={this.props.createdAt} />
</span>
</div>
);
}
renderPeriodSelectors() {
const { createdAfter, createdBefore } = this.props;
return (
<div className="search-navigator-date-facet-selection">
<DateInput
className="search-navigator-date-facet-selection-dropdown-left"
inputClassName="search-navigator-date-facet-selection-input"
maxDate={createdBefore ? toShortNotSoISOString(createdBefore) : '+0'}
onChange={this.handlePeriodChangeAfter}
placeholder={translate('from')}
value={createdAfter ? toShortNotSoISOString(createdAfter) : undefined}
/>
<DateInput
className="search-navigator-date-facet-selection-dropdown-right"
inputClassName="search-navigator-date-facet-selection-input"
minDate={createdAfter ? toShortNotSoISOString(createdAfter) : undefined}
onChange={this.handlePeriodChangeBefore}
placeholder={translate('to')}
value={createdBefore ? toShortNotSoISOString(createdBefore) : undefined}
/>
</div>
);
}
renderPredefinedPeriods() {
const { component, createdInLast, sinceLeakPeriod } = this.props;
if (component != null && component.branch != null) {
// FIXME handle long-living branches
return null;
}
return (
<div className="spacer-top issues-predefined-periods">
<FacetItem
active={!this.hasValue()}
name={translate('issues.facet.createdAt.all')}
onClick={this.handlePeriodClick}
value=""
/>
{component == null && (
<FacetItem
active={createdInLast === '1w'}
name={translate('issues.facet.createdAt.last_week')}
onClick={this.handlePeriodClick}
value="1w"
/>
)}
{component == null && (
<FacetItem
active={createdInLast === '1m'}
name={translate('issues.facet.createdAt.last_month')}
onClick={this.handlePeriodClick}
value="1m"
/>
)}
{component == null && (
<FacetItem
active={createdInLast === '1y'}
name={translate('issues.facet.createdAt.last_year')}
onClick={this.handlePeriodClick}
value="1y"
/>
)}
{component != null && (
<FacetItem
active={sinceLeakPeriod}
name={translate('issues.leak_period')}
onClick={this.handleLeakPeriodClick}
value=""
/>
)}
</div>
);
}
renderInner() {
const { createdAt } = this.props;
return createdAt ? (
this.renderExactDate()
) : (
<div>
{this.renderBarChart()}
{this.renderPeriodSelectors()}
{this.renderPredefinedPeriods()}
</div>
);
}
render() {
return (
<FacetBox property={this.property}>
<FacetHeader
name={translate('issues.facet', this.property)}
onClear={this.handleClear}
onClick={this.handleHeaderClick}
open={this.props.open}
values={this.getValues()}
/>
{this.props.open && this.renderInner()}
</FacetBox>
);
}
}
|