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.

AbstractRecordingLabel.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { IReduxState } from '../../app/types';
  4. import { JitsiRecordingConstants } from '../../base/lib-jitsi-meet';
  5. import { getSessionStatusToShow } from '../functions';
  6. /**
  7. * NOTE: Web currently renders multiple indicators if multiple recording
  8. * sessions are running. This is however may not be a good UX as it's not
  9. * obvious why there are multiple similar 'REC' indicators rendered. Mobile
  10. * only renders one indicator if there is at least one recording session
  11. * running. These boolean are shared across the two components to make it
  12. * easier to align web's behaviour to mobile's later if necessary.
  13. */
  14. interface IProps extends WithTranslation {
  15. /**
  16. * Whether this is the Jibri recorder participant.
  17. */
  18. _iAmRecorder: boolean;
  19. /**
  20. * The status of the highermost priority session.
  21. */
  22. _status?: string;
  23. /**
  24. * An object containing the CSS classes.
  25. */
  26. classes?: { [ key: string]: string; };
  27. /**
  28. * The recording mode this indicator should display.
  29. */
  30. mode: string;
  31. }
  32. /**
  33. * State of the component.
  34. */
  35. interface IState {
  36. /**
  37. * True if the label status is stale, so it needs to be removed.
  38. */
  39. staleLabel: boolean;
  40. }
  41. /**
  42. * The timeout after a label is considered stale. See {@code _updateStaleStatus}
  43. * for more details.
  44. */
  45. const STALE_TIMEOUT = 10 * 1000;
  46. /**
  47. * Abstract class for the {@code RecordingLabel} component.
  48. */
  49. export default class AbstractRecordingLabel
  50. extends Component<IProps, IState> {
  51. _mounted: boolean;
  52. /**
  53. * Implements {@code Component#getDerivedStateFromProps}.
  54. *
  55. * @inheritdoc
  56. */
  57. static getDerivedStateFromProps(props: IProps, prevState: IState) {
  58. return {
  59. staleLabel: props._status !== JitsiRecordingConstants.status.OFF
  60. && prevState.staleLabel ? false : prevState.staleLabel
  61. };
  62. }
  63. /**
  64. * Initializes a new {@code AbstractRecordingLabel} component.
  65. *
  66. * @inheritdoc
  67. */
  68. constructor(props: IProps) {
  69. super(props);
  70. this.state = {
  71. staleLabel: true
  72. };
  73. this._updateStaleStatus(undefined, props);
  74. }
  75. /**
  76. * Implements React {@code Component}'s componentDidMount.
  77. *
  78. * @inheritdoc
  79. */
  80. componentDidMount() {
  81. this._mounted = true;
  82. }
  83. /**
  84. * Implements React {@code Component}'s componentWillUnmount.
  85. *
  86. * @inheritdoc
  87. */
  88. componentWillUnmount() {
  89. this._mounted = false;
  90. }
  91. /**
  92. * Implements {@code Component#componentDidUpdate}.
  93. *
  94. * @inheritdoc
  95. */
  96. componentDidUpdate(prevProps: IProps) {
  97. this._updateStaleStatus(prevProps, this.props);
  98. }
  99. /**
  100. * Implements React {@code Component}'s render.
  101. *
  102. * @inheritdoc
  103. */
  104. render() {
  105. return this.props._status && !this.state.staleLabel && !this.props._iAmRecorder
  106. ? this._renderLabel() : null;
  107. }
  108. /**
  109. * Returns the label key that this indicator should render.
  110. *
  111. * @protected
  112. * @returns {?string}
  113. */
  114. _getLabelKey() {
  115. switch (this.props.mode) {
  116. case JitsiRecordingConstants.mode.STREAM:
  117. return 'recording.live';
  118. case JitsiRecordingConstants.mode.FILE:
  119. return 'recording.rec';
  120. default:
  121. // Invalid mode is passed to the component.
  122. return undefined;
  123. }
  124. }
  125. /**
  126. * Renders the platform specific label component.
  127. *
  128. * @protected
  129. * @returns {React$Element}
  130. */
  131. _renderLabel(): React.ReactNode | null {
  132. return null;
  133. }
  134. /**
  135. * Updates the stale status of the label on a prop change. A label is stale
  136. * if it's in a {@code _status} that doesn't need to be rendered anymore.
  137. *
  138. * @param {IProps} oldProps - The previous props of the component.
  139. * @param {IProps} newProps - The new props of the component.
  140. * @returns {void}
  141. */
  142. _updateStaleStatus(oldProps: IProps | undefined, newProps: IProps) {
  143. if (newProps._status === JitsiRecordingConstants.status.OFF) {
  144. if (oldProps?._status !== JitsiRecordingConstants.status.OFF) {
  145. setTimeout(() => {
  146. if (!this._mounted) {
  147. return;
  148. }
  149. // Only if it's still OFF.
  150. if (this.props._status
  151. === JitsiRecordingConstants.status.OFF) {
  152. this.setState({
  153. staleLabel: true
  154. });
  155. }
  156. }, STALE_TIMEOUT);
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Maps (parts of) the Redux state to the associated
  163. * {@code AbstractRecordingLabel}'s props.
  164. *
  165. * @param {Object} state - The Redux state.
  166. * @param {IProps} ownProps - The component's own props.
  167. * @private
  168. * @returns {{
  169. * _status: ?string
  170. * }}
  171. */
  172. export function _mapStateToProps(state: IReduxState, ownProps: IProps) {
  173. const { mode } = ownProps;
  174. return {
  175. _iAmRecorder: state['features/base/config'].iAmRecorder,
  176. _status: getSessionStatusToShow(state, mode)
  177. };
  178. }