Skip to content

Commit f940dfd

Browse files
authored
Merge pull request #5984 from vector-im/t3chguy/datesep
fix&refactor DateSeparator and MessageTimestamp
2 parents fc84456 + f97395f commit f940dfd

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2015, 2016 OpenMarket Ltd
3+
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -15,8 +16,9 @@ limitations under the License.
1516
*/
1617

1718
import React from 'react';
19+
import PropTypes from 'prop-types';
1820
import { _t } from 'matrix-react-sdk/lib/languageHandler';
19-
import {formatFullDate} from 'matrix-react-sdk/lib/DateUtils';
21+
import {formatFullDateNoTime} from 'matrix-react-sdk/lib/DateUtils';
2022

2123
function getdaysArray() {
2224
return [
@@ -30,30 +32,30 @@ function getdaysArray() {
3032
];
3133
}
3234

33-
module.exports = React.createClass({
34-
displayName: 'DateSeparator',
35-
render: function() {
36-
var date = new Date(this.props.ts);
37-
var today = new Date();
38-
var yesterday = new Date();
39-
var days = getdaysArray();
35+
export default class DateSeparator extends React.Component {
36+
static propTypes = {
37+
ts: PropTypes.number.isRequired,
38+
};
39+
40+
getLabel() {
41+
const date = new Date(this.props.ts);
42+
const today = new Date();
43+
const yesterday = new Date();
44+
const days = getdaysArray();
4045
yesterday.setDate(today.getDate() - 1);
41-
var label;
46+
4247
if (date.toDateString() === today.toDateString()) {
43-
label = _t('Today');
44-
}
45-
else if (date.toDateString() === yesterday.toDateString()) {
46-
label = _t('Yesterday');
47-
}
48-
else if (today.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
49-
label = days[date.getDay()];
50-
}
51-
else {
52-
label = formatFullDate(date, this.props.showTwelveHour);
48+
return _t('Today');
49+
} else if (date.toDateString() === yesterday.toDateString()) {
50+
return _t('Yesterday');
51+
} else if (today.getTime() - date.getTime() < 6 * 24 * 60 * 60 * 1000) {
52+
return days[date.getDay()];
53+
} else {
54+
return formatFullDateNoTime(date);
5355
}
56+
}
5457

55-
return (
56-
<h2 className="mx_DateSeparator">{ label }</h2>
57-
);
58+
render() {
59+
return <h2 className="mx_DateSeparator">{ this.getLabel() }</h2>;
5860
}
59-
});
61+
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2015, 2016 OpenMarket Ltd
3+
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -14,24 +15,22 @@ See the License for the specific language governing permissions and
1415
limitations under the License.
1516
*/
1617

17-
'use strict';
18-
1918
import React from 'react';
19+
import PropTypes from 'prop-types';
2020
import {formatFullDate, formatTime} from 'matrix-react-sdk/lib/DateUtils';
2121

22-
module.exports = React.createClass({
23-
displayName: 'MessageTimestamp',
24-
25-
propTypes: {
26-
showTwelveHour: React.PropTypes.bool,
27-
},
22+
export default class MessageTimestamp extends React.Component {
23+
static propTypes = {
24+
ts: PropTypes.number.isRequired,
25+
showTwelveHour: PropTypes.bool,
26+
};
2827

29-
render: function() {
28+
render() {
3029
const date = new Date(this.props.ts);
3130
return (
32-
<span className="mx_MessageTimestamp" title={ formatFullDate(date, this.props.showTwelveHour) }>
31+
<span className="mx_MessageTimestamp" title={formatFullDate(date, this.props.showTwelveHour)}>
3332
{ formatTime(date, this.props.showTwelveHour) }
3433
</span>
3534
);
36-
},
37-
});
35+
}
36+
}

0 commit comments

Comments
 (0)