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.

LocalRecordingInfoDialog.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* @flow */
  2. import moment from 'moment';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../base/i18n';
  6. import {
  7. PARTICIPANT_ROLE,
  8. getLocalParticipant
  9. } from '../../base/participants';
  10. import { statsUpdate } from '../actions';
  11. import { recordingController } from '../controller';
  12. /**
  13. * The type of the React {@code Component} props of
  14. * {@link LocalRecordingInfoDialog}.
  15. */
  16. type Props = {
  17. /**
  18. * Redux store dispatch function.
  19. */
  20. dispatch: Dispatch<*>,
  21. /**
  22. * Current encoding format.
  23. */
  24. encodingFormat: string,
  25. /**
  26. * Whether the local user is the moderator.
  27. */
  28. isModerator: boolean,
  29. /**
  30. * Whether local recording is engaged.
  31. */
  32. isOn: boolean,
  33. /**
  34. * The start time of the current local recording session.
  35. * Used to calculate the duration of recording.
  36. */
  37. recordingStartedAt: Date,
  38. /**
  39. * Stats of all the participant.
  40. */
  41. stats: Object,
  42. /**
  43. * Invoked to obtain translated strings.
  44. */
  45. t: Function
  46. }
  47. /**
  48. * The type of the React {@code Component} state of
  49. * {@link LocalRecordingInfoDialog}.
  50. */
  51. type State = {
  52. /**
  53. * The recording duration string to be displayed on the UI.
  54. */
  55. durationString: string
  56. }
  57. /**
  58. * A React Component with the contents for a dialog that shows information about
  59. * local recording. For users with moderator rights, this is also the "control
  60. * panel" for starting/stopping local recording on all clients.
  61. *
  62. * @extends Component
  63. */
  64. class LocalRecordingInfoDialog extends Component<Props, State> {
  65. /**
  66. * Saves a handle to the timer for UI updates,
  67. * so that it can be cancelled when the component unmounts.
  68. */
  69. _timer: ?IntervalID;
  70. /**
  71. * Constructor.
  72. */
  73. constructor() {
  74. super();
  75. this.state = {
  76. durationString: 'N/A'
  77. };
  78. }
  79. /**
  80. * Implements React's {@link Component#componentWillMount()}.
  81. *
  82. * @returns {void}
  83. */
  84. componentWillMount() {
  85. this._timer = setInterval(
  86. () => {
  87. this.setState((_prevState, props) => {
  88. const nowTime = new Date(Date.now());
  89. return {
  90. durationString: this._getDuration(nowTime,
  91. props.recordingStartedAt)
  92. };
  93. });
  94. try {
  95. this.props.dispatch(
  96. statsUpdate(recordingController
  97. .getParticipantsStats()));
  98. } catch (e) {
  99. // do nothing
  100. }
  101. },
  102. 1000
  103. );
  104. }
  105. /**
  106. * Implements React's {@link Component#componentWillUnmount()}.
  107. *
  108. * @returns {void}
  109. */
  110. componentWillUnmount() {
  111. if (this._timer) {
  112. clearInterval(this._timer);
  113. this._timer = null;
  114. }
  115. }
  116. /**
  117. * Returns React elements for displaying the local recording stats of
  118. * each participant.
  119. *
  120. * @returns {ReactElement}
  121. */
  122. renderStats() {
  123. const { stats, t } = this.props;
  124. if (stats === undefined) {
  125. return <ul />;
  126. }
  127. const ids = Object.keys(stats);
  128. return (
  129. <ul>
  130. {ids.map((id, i) =>
  131. // FIXME: a workaround, as arrow functions without `return`
  132. // keyword need to be wrapped in parenthesis.
  133. /* eslint-disable no-extra-parens */
  134. (<li key = { i }>
  135. <span>{stats[id].displayName || id}: </span>
  136. <span>{stats[id].recordingStats
  137. ? `${stats[id].recordingStats.isRecording
  138. ? t('localRecording.clientState.on')
  139. : t('localRecording.clientState.off')} `
  140. + `(${stats[id]
  141. .recordingStats.currentSessionToken})`
  142. : t('localRecording.clientState.unknown')}</span>
  143. </li>)
  144. /* eslint-enable no-extra-parens */
  145. )}
  146. </ul>
  147. );
  148. }
  149. /**
  150. * Implements React's {@link Component#render()}.
  151. *
  152. * @inheritdoc
  153. * @returns {ReactElement}
  154. */
  155. render() {
  156. const { isModerator, encodingFormat, isOn, t } = this.props;
  157. const { durationString } = this.state;
  158. return (
  159. <div
  160. className = 'info-dialog' >
  161. <div className = 'info-dialog-column'>
  162. <h4 className = 'info-dialog-icon'>
  163. <i className = 'icon-info' />
  164. </h4>
  165. </div>
  166. <div className = 'info-dialog-column'>
  167. <div className = 'info-dialog-title'>
  168. { t('localRecording.localRecording') }
  169. </div>
  170. <div>
  171. <span className = 'info-label'>
  172. {`${t('localRecording.moderator')}:`}
  173. </span>
  174. <span className = 'spacer'>&nbsp;</span>
  175. <span className = 'info-value'>
  176. { isModerator
  177. ? t('localRecording.yes')
  178. : t('localRecording.no') }
  179. </span>
  180. </div>
  181. { isOn && <div>
  182. <span className = 'info-label'>
  183. {`${t('localRecording.duration')}:`}
  184. </span>
  185. <span className = 'spacer'>&nbsp;</span>
  186. <span className = 'info-value'>
  187. { durationString }
  188. </span>
  189. </div>
  190. }
  191. {isOn
  192. && <div>
  193. <span className = 'info-label'>
  194. {`${t('localRecording.encoding')}:`}
  195. </span>
  196. <span className = 'spacer'>&nbsp;</span>
  197. <span className = 'info-value'>
  198. { encodingFormat }
  199. </span>
  200. </div>
  201. }
  202. {
  203. isModerator
  204. && <div>
  205. <div>
  206. <span className = 'info-label'>
  207. {`${t('localRecording.participantStats')}:`}
  208. </span>
  209. </div>
  210. { this.renderStats() }
  211. </div>
  212. }
  213. {
  214. isModerator
  215. && <div className = 'info-dialog-action-links'>
  216. <div className = 'info-dialog-action-link'>
  217. {isOn ? <a
  218. onClick = { this._onStop }>
  219. { t('localRecording.stop') }
  220. </a>
  221. : <a
  222. onClick = { this._onStart }>
  223. { t('localRecording.start') }
  224. </a>
  225. }
  226. </div>
  227. </div>
  228. }
  229. </div>
  230. </div>
  231. );
  232. }
  233. /**
  234. * Creates a duration string "HH:MM:SS" from two Date objects.
  235. *
  236. * @param {Date} now - Current time.
  237. * @param {Date} prev - Previous time, the time to be subtracted.
  238. * @returns {string}
  239. */
  240. _getDuration(now, prev) {
  241. // Still a hack, as moment.js does not support formatting of duration
  242. // (i.e. TimeDelta). Only works if total duration < 24 hours.
  243. // But who is going to have a 24-hour long conference?
  244. return moment(now - prev).utc()
  245. .format('HH:mm:ss');
  246. }
  247. /**
  248. * Callback function for the Start UI action.
  249. *
  250. * @private
  251. * @returns {void}
  252. */
  253. _onStart() {
  254. recordingController.startRecording();
  255. }
  256. /**
  257. * Callback function for the Stop UI action.
  258. *
  259. * @private
  260. * @returns {void}
  261. */
  262. _onStop() {
  263. recordingController.stopRecording();
  264. }
  265. }
  266. /**
  267. * Maps (parts of) the Redux state to the associated props for the
  268. * {@code LocalRecordingInfoDialog} component.
  269. *
  270. * @param {Object} state - The Redux state.
  271. * @private
  272. * @returns {{
  273. * encodingFormat: string,
  274. * isModerator: boolean,
  275. * isOn: boolean,
  276. * recordingStartedAt: Date,
  277. * stats: Object
  278. * }}
  279. */
  280. function _mapStateToProps(state) {
  281. const {
  282. encodingFormat,
  283. isEngaged: isOn,
  284. recordingStartedAt,
  285. stats
  286. } = state['features/local-recording'];
  287. const isModerator
  288. = getLocalParticipant(state).role === PARTICIPANT_ROLE.MODERATOR;
  289. return {
  290. encodingFormat,
  291. isModerator,
  292. isOn,
  293. recordingStartedAt,
  294. stats
  295. };
  296. }
  297. export default translate(connect(_mapStateToProps)(LocalRecordingInfoDialog));