2018-02-13 23:33:03 +00:00
|
|
|
// @flow
|
|
|
|
import React, { Component } from 'react';
|
2017-12-13 10:35:42 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
import { appNavigate } from '../../app';
|
|
|
|
import {
|
|
|
|
getLocalizedDateFormatter,
|
|
|
|
getLocalizedDurationFormatter,
|
2018-02-15 19:01:54 +00:00
|
|
|
translate
|
|
|
|
} from '../../base/i18n';
|
|
|
|
import { NavigateSectionList } from '../../base/react';
|
|
|
|
import { parseURIString } from '../../base/util';
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* The type of the React {@code Component} props of {@link RecentList}
|
2017-12-13 10:35:42 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the list disabled.
|
|
|
|
*/
|
|
|
|
disabled: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The redux store's {@code dispatch} function.
|
|
|
|
*/
|
|
|
|
dispatch: Dispatch<*>,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The translate function.
|
|
|
|
*/
|
|
|
|
t: Function,
|
|
|
|
|
2018-01-17 11:19:10 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* The default server URL.
|
2018-01-17 11:19:10 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_defaultServerURL: string,
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
/**
|
|
|
|
* The recent list from the Redux store.
|
|
|
|
*/
|
|
|
|
_recentList: Array<Object>
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The native container rendering the list of the recently joined rooms.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RecentList extends Component<Props> {
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code RecentList} instance.
|
2018-01-17 11:19:10 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
2017-12-13 10:35:42 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
constructor(props: Props) {
|
2018-02-16 16:06:03 +00:00
|
|
|
super(props);
|
2017-12-13 10:35:42 +00:00
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
this._onPress = this._onPress.bind(this);
|
|
|
|
this._toDateString = this._toDateString.bind(this);
|
|
|
|
this._toDurationString = this._toDurationString.bind(this);
|
|
|
|
this._toDisplayableItem = this._toDisplayableItem.bind(this);
|
|
|
|
this._toDisplayableList = this._toDisplayableList.bind(this);
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Implements the React Components's render method.
|
2017-12-13 10:35:42 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
2018-02-13 23:33:03 +00:00
|
|
|
const { disabled } = this.props;
|
2018-01-17 11:19:10 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
return (
|
2018-02-13 23:33:03 +00:00
|
|
|
<NavigateSectionList
|
|
|
|
disabled = { disabled }
|
|
|
|
onPress = { this._onPress }
|
|
|
|
sections = { this._toDisplayableList() } />
|
2017-12-13 10:35:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-16 23:07:25 +00:00
|
|
|
_onPress: string => Function;
|
2018-02-13 23:33:03 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Handles the list's navigate action.
|
2017-12-20 00:49:36 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-02-13 23:33:03 +00:00
|
|
|
* @param {string} url - The url string to navigate to.
|
|
|
|
* @returns {void}
|
2017-12-20 00:49:36 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_onPress(url) {
|
|
|
|
const { dispatch } = this.props;
|
2017-12-13 10:35:42 +00:00
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
dispatch(appNavigate(url));
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 23:07:25 +00:00
|
|
|
_toDisplayableItem: Object => Object;
|
2018-02-13 23:33:03 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Creates a displayable list item of a recent list entry.
|
2017-12-20 00:49:36 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-02-13 23:33:03 +00:00
|
|
|
* @param {Object} item - The recent list entry.
|
2017-12-20 00:49:36 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_toDisplayableItem(item) {
|
|
|
|
const { _defaultServerURL } = this.props;
|
|
|
|
const location = parseURIString(item.conference);
|
|
|
|
const baseURL = `${location.protocol}//${location.host}`;
|
|
|
|
const serverName = baseURL === _defaultServerURL ? null : location.host;
|
|
|
|
|
|
|
|
return {
|
|
|
|
colorBase: serverName,
|
|
|
|
key: `key-${item.conference}-${item.date}`,
|
|
|
|
lines: [
|
|
|
|
this._toDateString(item.date),
|
|
|
|
this._toDurationString(item.duration),
|
|
|
|
serverName
|
|
|
|
],
|
|
|
|
title: location.room,
|
|
|
|
url: item.conference
|
|
|
|
};
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 23:07:25 +00:00
|
|
|
_toDisplayableList: () => Array<Object>;
|
2018-02-13 23:33:03 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Transforms the history list to a displayable list
|
|
|
|
* with sections.
|
2017-12-20 00:49:36 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-02-13 23:33:03 +00:00
|
|
|
* @returns {Array<Object>}
|
2017-12-20 00:49:36 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_toDisplayableList() {
|
|
|
|
const { _recentList, t } = this.props;
|
2018-04-16 23:07:25 +00:00
|
|
|
const { createSection } = NavigateSectionList;
|
|
|
|
const todaySection = createSection(t('recentList.today'), 'today');
|
|
|
|
const yesterdaySection
|
|
|
|
= createSection(t('recentList.yesterday'), 'yesterday');
|
|
|
|
const earlierSection
|
|
|
|
= createSection(t('recentList.earlier'), 'earlier');
|
2018-02-13 23:33:03 +00:00
|
|
|
const today = new Date().toDateString();
|
|
|
|
const yesterdayDate = new Date();
|
|
|
|
|
|
|
|
yesterdayDate.setDate(yesterdayDate.getDate() - 1);
|
|
|
|
|
|
|
|
const yesterday = yesterdayDate.toDateString();
|
|
|
|
|
|
|
|
for (const item of _recentList) {
|
|
|
|
const itemDay = new Date(item.date).toDateString();
|
|
|
|
const displayableItem = this._toDisplayableItem(item);
|
|
|
|
|
|
|
|
if (itemDay === today) {
|
|
|
|
todaySection.data.push(displayableItem);
|
|
|
|
} else if (itemDay === yesterday) {
|
|
|
|
yesterdaySection.data.push(displayableItem);
|
|
|
|
} else {
|
|
|
|
earlierSection.data.push(displayableItem);
|
|
|
|
}
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
const displayableList = [];
|
|
|
|
|
|
|
|
if (todaySection.data.length) {
|
|
|
|
todaySection.data.reverse();
|
|
|
|
displayableList.push(todaySection);
|
|
|
|
}
|
|
|
|
if (yesterdaySection.data.length) {
|
|
|
|
yesterdaySection.data.reverse();
|
|
|
|
displayableList.push(yesterdaySection);
|
|
|
|
}
|
|
|
|
if (earlierSection.data.length) {
|
|
|
|
earlierSection.data.reverse();
|
|
|
|
displayableList.push(earlierSection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayableList;
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
2017-12-20 00:49:36 +00:00
|
|
|
|
2018-04-16 23:07:25 +00:00
|
|
|
_toDateString: number => string;
|
2018-02-13 23:33:03 +00:00
|
|
|
|
2017-12-20 00:49:36 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Generates a date string for the item.
|
2017-12-20 00:49:36 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-02-13 23:33:03 +00:00
|
|
|
* @param {number} itemDate - The item's timestamp.
|
|
|
|
* @returns {string}
|
2017-12-20 00:49:36 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_toDateString(itemDate) {
|
|
|
|
const date = new Date(itemDate);
|
|
|
|
const m = getLocalizedDateFormatter(itemDate);
|
|
|
|
|
|
|
|
if (date.toDateString() === new Date().toDateString()) {
|
|
|
|
// The date is today, we use fromNow format.
|
|
|
|
return m.fromNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.format('lll');
|
2017-12-20 00:49:36 +00:00
|
|
|
}
|
2018-02-27 20:21:28 +00:00
|
|
|
|
2018-04-16 23:07:25 +00:00
|
|
|
_toDurationString: number => string;
|
2018-02-13 23:33:03 +00:00
|
|
|
|
2018-02-27 20:21:28 +00:00
|
|
|
/**
|
2018-02-13 23:33:03 +00:00
|
|
|
* Generates a duration string for the item.
|
2018-02-27 20:21:28 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-02-13 23:33:03 +00:00
|
|
|
* @param {number} duration - The item's duration.
|
|
|
|
* @returns {string}
|
2018-02-27 20:21:28 +00:00
|
|
|
*/
|
2018-02-13 23:33:03 +00:00
|
|
|
_toDurationString(duration) {
|
|
|
|
if (duration) {
|
|
|
|
return getLocalizedDurationFormatter(duration).humanize();
|
2018-02-27 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:33:03 +00:00
|
|
|
/**
|
|
|
|
* Maps redux state to component props.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {{
|
2018-04-16 23:07:25 +00:00
|
|
|
* _defaultServerURL: string,
|
|
|
|
* _recentList: Array
|
2018-02-13 23:33:03 +00:00
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function _mapStateToProps(state: Object) {
|
|
|
|
return {
|
|
|
|
_defaultServerURL: state['features/app'].app._getDefaultURL(),
|
|
|
|
_recentList: state['features/recent-list']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(connect(_mapStateToProps)(RecentList));
|