-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor timer code to hook and TimeViewer component, add it to mahjong
- Loading branch information
Showing
6 changed files
with
164 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import PropTypes from 'prop-types'; | ||
import { faPause, faPlay } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { Button, InputGroup } from 'react-bootstrap'; | ||
|
||
import { formatTime } from 'utils/timer'; | ||
|
||
export default function TimeViewer({ elapsedTime, running, onToggle }) { | ||
return ( | ||
<InputGroup> | ||
<InputGroup.Text className="font-monospace"> | ||
{formatTime(elapsedTime)} | ||
</InputGroup.Text> | ||
<Button variant="info" size="sm" onClick={onToggle}> | ||
<FontAwesomeIcon icon={running ? faPause : faPlay} fixedWidth /> | ||
</Button> | ||
</InputGroup> | ||
); | ||
} | ||
|
||
TimeViewer.propTypes = { | ||
elapsedTime: PropTypes.number.isRequired, | ||
running: PropTypes.bool.isRequired, | ||
onToggle: PropTypes.func.isRequired | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { differenceInSeconds } from 'date-fns'; | ||
import { useCallback, useEffect, useState, useMemo, useRef } from 'react'; | ||
|
||
export default function useTimer(run = true, interval = 1000) { | ||
const intervalRef = useRef(null); | ||
const startTime = useMemo(() => Date.now(), []); | ||
const [running, setRunning] = useState(run); | ||
const [currentTime, setCurrentTime] = useState(startTime); | ||
const startTimer = useCallback(() => setRunning(true), [setRunning]); | ||
const stopTimer = useCallback(() => setRunning(false), [setRunning]); | ||
const toggleTimer = useCallback( | ||
() => setRunning((prevVal) => !prevVal), | ||
[setRunning] | ||
); | ||
const resetTimer = useCallback( | ||
() => setCurrentTime(startTime), | ||
[setCurrentTime, startTime] | ||
); | ||
const incrementTimer = useCallback( | ||
(amount = interval) => setCurrentTime((prevVal) => prevVal + amount), | ||
[setCurrentTime] | ||
); | ||
const handleDocumentVisibilityChange = useCallback( | ||
() => (document.hidden ? stopTimer() : startTimer()), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
if (running) { | ||
intervalRef.current = setInterval(incrementTimer, interval); | ||
|
||
return () => clearInterval(intervalRef.current); | ||
} else if (intervalRef.current) { | ||
clearInterval(intervalRef.current); | ||
} | ||
}, [running]); | ||
|
||
useEffect(() => { | ||
// if we remove visibilitychange on unmount we lose it, so only set it up once | ||
document.addEventListener( | ||
'visibilitychange', | ||
handleDocumentVisibilityChange | ||
); | ||
}); | ||
|
||
return { | ||
elapsedTime: differenceInSeconds(currentTime, startTime), | ||
running, | ||
startTimer, | ||
stopTimer, | ||
toggleTimer, | ||
resetTimer, | ||
incrementTimer | ||
}; | ||
} |
Oops, something went wrong.