diff --git a/src/DateUtils.js b/src/DateUtils.js index 38a6a3a36f..91df1e46d5 100644 --- a/src/DateUtils.js +++ b/src/DateUtils.js @@ -100,3 +100,17 @@ export function formatTime(date, showTwelveHour=false) { } return pad(date.getHours()) + ':' + pad(date.getMinutes()); } + +const MILLIS_IN_DAY = 86400000; +export function wantsDateSeparator(prevEventDate, nextEventDate) { + if (!nextEventDate || !prevEventDate) { + return false; + } + // Return early for events that are > 24h apart + if (Math.abs(prevEventDate.getTime() - nextEventDate.getTime()) > MILLIS_IN_DAY) { + return true; + } + + // Compare weekdays + return prevEventDate.getDay() !== nextEventDate.getDay(); +} diff --git a/src/components/structures/MessagePanel.js b/src/components/structures/MessagePanel.js index 84441e1317..5523fc27a0 100644 --- a/src/components/structures/MessagePanel.js +++ b/src/components/structures/MessagePanel.js @@ -19,13 +19,12 @@ import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import shouldHideEvent from '../../shouldHideEvent'; +import {wantsDateSeparator} from '../../DateUtils'; import dis from "../../dispatcher"; import sdk from '../../index'; import MatrixClientPeg from '../../MatrixClientPeg'; -const MILLIS_IN_DAY = 86400000; - /* (almost) stateless UI component which builds the event tiles in the room timeline. */ module.exports = React.createClass({ @@ -523,17 +522,7 @@ module.exports = React.createClass({ // here. return !this.props.suppressFirstDateSeparator; } - const prevEventDate = prevEvent.getDate(); - if (!nextEventDate || !prevEventDate) { - return false; - } - // Return early for events that are > 24h apart - if (Math.abs(prevEvent.getTs() - nextEventDate.getTime()) > MILLIS_IN_DAY) { - return true; - } - - // Compare weekdays - return prevEventDate.getDay() !== nextEventDate.getDay(); + return wantsDateSeparator(prevEvent.getDate(), nextEventDate); }, // get a list of read receipts that should be shown next to this event diff --git a/src/components/views/elements/Quote.js b/src/components/views/elements/Quote.js index e5b9e56949..bceba0f536 100644 --- a/src/components/views/elements/Quote.js +++ b/src/components/views/elements/Quote.js @@ -18,28 +18,13 @@ import sdk from '../../../index'; import {_t} from '../../../languageHandler'; import PropTypes from 'prop-types'; import MatrixClientPeg from '../../../MatrixClientPeg'; +import {wantsDateSeparator} from '../../../DateUtils'; import {MatrixEvent} from 'matrix-js-sdk'; // For URLs of matrix.to links in the timeline which have been reformatted by // HttpUtils transformTags to relative links. This excludes event URLs (with `[^\/]*`) const REGEX_LOCAL_MATRIXTO = /^#\/room\/(([\#\!])[^\/]*)\/(\$[^\/]*)$/; -const MILLIS_IN_DAY = 86400000; -function wantsDateSeparator(parentEvent, event) { - const parentEventDate = parentEvent.getDate(); - const eventDate = event.getDate(); - if (!eventDate || !parentEventDate) { - return false; - } - // Return early for events that are > 24h apart - if (Math.abs(parentEvent.getTs() - eventDate.getTime()) > MILLIS_IN_DAY) { - return true; - } - - // Compare weekdays - return parentEventDate.getDay() !== eventDate.getDay(); -} - export default class Quote extends React.Component { static isMessageUrl(url) { return !!REGEX_LOCAL_MATRIXTO.exec(url); @@ -129,9 +114,11 @@ export default class Quote extends React.Component { if (this.state.show) { const EventTile = sdk.getComponent('views.rooms.EventTile'); let dateSep = null; - if (wantsDateSeparator(this.props.parentEv, this.state.event)) { + + const evDate = ev.getDate(); + if (wantsDateSeparator(this.props.parentEv.getDate(), evDate)) { const DateSeparator = sdk.getComponent('messages.DateSeparator'); - dateSep = ; + dateSep = ; } return