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.

ConferenceTimer.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import { useSelector } from 'react-redux';
  3. import { getConferenceTimestamp } from '../../base/conference/functions';
  4. import { getLocalizedDurationFormatter } from '../../base/i18n/dateUtil';
  5. import { ConferenceTimerDisplay } from './index';
  6. /**
  7. * The type of the React {@code Component} props of {@link ConferenceTimer}.
  8. */
  9. interface IProps {
  10. /**
  11. * Style to be applied to the rendered text.
  12. */
  13. textStyle?: Object;
  14. }
  15. export interface IDisplayProps {
  16. /**
  17. * Style to be applied to text (native only).
  18. */
  19. textStyle?: Object;
  20. /**
  21. * String to display as time.
  22. */
  23. timerValue: string;
  24. }
  25. const ConferenceTimer = ({ textStyle }: IProps) => {
  26. const startTimestamp = useSelector(getConferenceTimestamp);
  27. const [ timerValue, setTimerValue ] = useState(getLocalizedDurationFormatter(0));
  28. const interval = useRef<number>();
  29. /**
  30. * Sets the current state values that will be used to render the timer.
  31. *
  32. * @param {number} refValueUTC - The initial UTC timestamp value.
  33. * @param {number} currentValueUTC - The current UTC timestamp value.
  34. *
  35. * @returns {void}
  36. */
  37. const setStateFromUTC = useCallback((refValueUTC, currentValueUTC) => {
  38. if (!refValueUTC || !currentValueUTC) {
  39. return;
  40. }
  41. if (currentValueUTC < refValueUTC) {
  42. return;
  43. }
  44. const timerMsValue = currentValueUTC - refValueUTC;
  45. const localizedTime = getLocalizedDurationFormatter(timerMsValue);
  46. setTimerValue(localizedTime);
  47. }, []);
  48. /**
  49. * Start conference timer.
  50. *
  51. * @returns {void}
  52. */
  53. const startTimer = useCallback(() => {
  54. if (!interval.current && startTimestamp) {
  55. setStateFromUTC(startTimestamp, new Date().getTime());
  56. interval.current = window.setInterval(() => {
  57. setStateFromUTC(startTimestamp, new Date().getTime());
  58. }, 1000);
  59. }
  60. }, [ startTimestamp, interval ]);
  61. /**
  62. * Stop conference timer.
  63. *
  64. * @returns {void}
  65. */
  66. const stopTimer = useCallback(() => {
  67. if (interval.current) {
  68. clearInterval(interval.current);
  69. interval.current = undefined;
  70. }
  71. setTimerValue(getLocalizedDurationFormatter(0));
  72. }, [ interval ]);
  73. useEffect(() => {
  74. startTimer();
  75. return () => stopTimer();
  76. }, [ startTimestamp ]);
  77. if (!startTimestamp) {
  78. return null;
  79. }
  80. return (<ConferenceTimerDisplay
  81. textStyle = { textStyle }
  82. timerValue = { timerValue } />);
  83. };
  84. export default ConferenceTimer;