You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeetingsList.tsx 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { getLocalizedDateFormatter, getLocalizedDurationFormatter } from '../../../i18n/dateUtil';
  4. import { translate } from '../../../i18n/functions';
  5. import Icon from '../../../icons/components/Icon';
  6. import { IconTrash } from '../../../icons/svg';
  7. import Container from './Container';
  8. import Text from './Text';
  9. interface IMeeting {
  10. date: Date;
  11. duration?: number;
  12. elementAfter?: React.ReactElement;
  13. time: Date[];
  14. title: string;
  15. url: string;
  16. }
  17. interface IProps extends WithTranslation {
  18. /**
  19. * Indicates if the list is disabled or not.
  20. */
  21. disabled: boolean;
  22. /**
  23. * Indicates if the URL should be hidden or not.
  24. */
  25. hideURL?: boolean;
  26. /**
  27. * Rendered when the list is empty. Should be a rendered element.
  28. */
  29. listEmptyComponent: React.ReactNode;
  30. /**
  31. * An array of meetings.
  32. */
  33. meetings: IMeeting[];
  34. /**
  35. * Handler for deleting an item.
  36. */
  37. onItemDelete?: Function;
  38. /**
  39. * Function to be invoked when an item is pressed. The item's URL is passed.
  40. */
  41. onPress: Function;
  42. }
  43. /**
  44. * Generates a date string for a given date.
  45. *
  46. * @param {Object} date - The date.
  47. * @private
  48. * @returns {string}
  49. */
  50. function _toDateString(date: Date) {
  51. return getLocalizedDateFormatter(date).format('ll');
  52. }
  53. /**
  54. * Generates a time (interval) string for a given times.
  55. *
  56. * @param {Array<Date>} times - Array of times.
  57. * @private
  58. * @returns {string}
  59. */
  60. function _toTimeString(times: Date[]) {
  61. if (times && times.length > 0) {
  62. return (
  63. times
  64. .map(time => getLocalizedDateFormatter(time).format('LT'))
  65. .join(' - '));
  66. }
  67. return undefined;
  68. }
  69. /**
  70. * Implements a React/Web {@link Component} for displaying a list with
  71. * meetings.
  72. *
  73. * @augments Component
  74. */
  75. class MeetingsList extends Component<IProps> {
  76. /**
  77. * Constructor of the MeetingsList component.
  78. *
  79. * @inheritdoc
  80. */
  81. constructor(props: IProps) {
  82. super(props);
  83. this._onPress = this._onPress.bind(this);
  84. this._renderItem = this._renderItem.bind(this);
  85. }
  86. /**
  87. * Renders the content of this component.
  88. *
  89. * @returns {React.ReactNode}
  90. */
  91. render() {
  92. const { listEmptyComponent, meetings } = this.props;
  93. /**
  94. * If there are no recent meetings we don't want to display anything.
  95. */
  96. if (meetings) {
  97. return (
  98. <Container className = 'meetings-list'>
  99. {
  100. meetings.length === 0
  101. ? listEmptyComponent
  102. : meetings.map(this._renderItem)
  103. }
  104. </Container>
  105. );
  106. }
  107. return null;
  108. }
  109. /**
  110. * Returns a function that is used in the onPress callback of the items.
  111. *
  112. * @param {string} url - The URL of the item to navigate to.
  113. * @private
  114. * @returns {Function}
  115. */
  116. _onPress(url: string) {
  117. const { disabled, onPress } = this.props;
  118. if (!disabled && url && typeof onPress === 'function') {
  119. return () => onPress(url);
  120. }
  121. return undefined;
  122. }
  123. /**
  124. * Returns a function that is used in the onPress callback of the items.
  125. *
  126. * @param {string} url - The URL of the item to navigate to.
  127. * @private
  128. * @returns {Function}
  129. */
  130. _onKeyPress(url: string) {
  131. const { disabled, onPress } = this.props;
  132. if (!disabled && url && typeof onPress === 'function') {
  133. return (e: React.KeyboardEvent) => {
  134. if (e.key === ' ' || e.key === 'Enter') {
  135. onPress(url);
  136. }
  137. };
  138. }
  139. return undefined;
  140. }
  141. /**
  142. * Returns a function that is used on the onDelete callback.
  143. *
  144. * @param {Object} item - The item to be deleted.
  145. * @private
  146. * @returns {Function}
  147. */
  148. _onDelete(item: Object) {
  149. const { onItemDelete } = this.props;
  150. return (evt?: React.MouseEvent) => {
  151. evt?.stopPropagation();
  152. onItemDelete?.(item);
  153. };
  154. }
  155. /**
  156. * Returns a function that is used on the onDelete keypress callback.
  157. *
  158. * @param {Object} item - The item to be deleted.
  159. * @private
  160. * @returns {Function}
  161. */
  162. _onDeleteKeyPress(item: Object) {
  163. const { onItemDelete } = this.props;
  164. return (e: React.KeyboardEvent) => {
  165. if (onItemDelete && (e.key === ' ' || e.key === 'Enter')) {
  166. e.preventDefault();
  167. e.stopPropagation();
  168. onItemDelete(item);
  169. }
  170. };
  171. }
  172. /**
  173. * Renders an item for the list.
  174. *
  175. * @param {Object} meeting - Information about the meeting.
  176. * @param {number} index - The index of the item.
  177. * @returns {Node}
  178. */
  179. _renderItem(meeting: IMeeting, index: number) {
  180. const {
  181. date,
  182. duration,
  183. elementAfter,
  184. time,
  185. title,
  186. url
  187. } = meeting;
  188. const { hideURL = false, onItemDelete, t } = this.props;
  189. const onPress = this._onPress(url);
  190. const onKeyPress = this._onKeyPress(url);
  191. const rootClassName
  192. = `item ${
  193. onPress ? 'with-click-handler' : 'without-click-handler'}`;
  194. return (
  195. <Container
  196. className = { rootClassName }
  197. key = { index }
  198. onClick = { onPress }
  199. tabIndex = { 0 }>
  200. <Container className = 'right-column'>
  201. <Text
  202. className = 'title'
  203. onClick = { onPress }
  204. onKeyPress = { onKeyPress }
  205. role = 'button'>
  206. { title }
  207. </Text>
  208. {
  209. hideURL || !url ? null : (
  210. <Text>
  211. { url }
  212. </Text>)
  213. }
  214. {
  215. typeof duration === 'number' ? (
  216. <Text className = 'subtitle'>
  217. { getLocalizedDurationFormatter(duration) }
  218. </Text>) : null
  219. }
  220. </Container>
  221. <Container className = 'left-column'>
  222. <Text className = 'title'>
  223. { _toDateString(date) }
  224. </Text>
  225. <Text className = 'subtitle'>
  226. { _toTimeString(time) }
  227. </Text>
  228. </Container>
  229. <Container className = 'actions'>
  230. { elementAfter || null }
  231. { onItemDelete && <Icon
  232. ariaLabel = { t('welcomepage.recentListDelete') }
  233. className = 'delete-meeting'
  234. onClick = { this._onDelete(meeting) }
  235. onKeyPress = { this._onDeleteKeyPress(meeting) }
  236. role = 'button'
  237. src = { IconTrash }
  238. tabIndex = { 0 } />}
  239. </Container>
  240. </Container>
  241. );
  242. }
  243. }
  244. export default translate(MeetingsList);