您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConferenceTimer.tsx 2.7KB

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