From 3fbfd13e27b93aefb52c1669def934cdf01e602c Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Wed, 5 Aug 2020 10:40:18 +1000 Subject: [PATCH] Added visual timer component --- src/components/party/Party.js | 2 ++ src/components/party/Timer.js | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/components/party/Timer.js diff --git a/src/components/party/Party.js b/src/components/party/Party.js index 3578dd5..6dce999 100644 --- a/src/components/party/Party.js +++ b/src/components/party/Party.js @@ -7,6 +7,7 @@ import ChangeNicknameButton from "./ChangeNicknameButton"; import StartStreamButton from "./StartStreamButton"; import SettingsButton from "../SettingsButton"; import StartTimerButton from "./StartTimerButton"; +import Timer from "./Timer"; function Party({ nickname, @@ -57,6 +58,7 @@ function Party({ stream={partyStreams[id]} /> ))} + diff --git a/src/components/party/Timer.js b/src/components/party/Timer.js new file mode 100644 index 0000000..251be69 --- /dev/null +++ b/src/components/party/Timer.js @@ -0,0 +1,63 @@ +import React, { useEffect, useState, useRef } from "react"; +import ReactDOM from "react-dom"; +import { Box, Progress } from "theme-ui"; + +import usePortal from "../../helpers/usePortal"; + +function getTimerDuration(t) { + if (!t) { + return 0; + } + return t.hour * 3600000 + t.minute * 60000 + t.second * 1000; +} + +function Timer({ timer }) { + const [progress, setProgress] = useState(0); + const [maxDuration, setMaxDuration] = useState(0); + + const previousTimerRef = useRef(timer); + useEffect(() => { + if (!previousTimerRef.current && timer) { + setMaxDuration(getTimerDuration(timer)); + } + previousTimerRef.current = timer; + }); + + useEffect(() => { + setProgress(getTimerDuration(timer)); + }, [timer]); + + const timerContainer = usePortal("root"); + + return ReactDOM.createPortal( + + + , + timerContainer + ); +} + +export default Timer;