-
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.
- Loading branch information
1 parent
8a5c840
commit 8c95a92
Showing
6 changed files
with
193 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Button } from '@mui/material'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import Timers from '../UI_AND_UX/timing'; | ||
|
||
function TelemetryPage() { | ||
const navigate = useNavigate(); | ||
|
||
return ( | ||
<div className="h-full flex flex-row gap-x-4 items-center justify-center"> | ||
<Timers /> | ||
<Button | ||
onClick={() => { | ||
navigate('/'); | ||
}} | ||
> | ||
Back to Home | ||
</Button> | ||
</div> | ||
); | ||
} | ||
|
||
export default TelemetryPage; |
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,108 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
/* eslint-disable react/jsx-no-constructed-context-values */ | ||
/* eslint-disable react/function-component-definition */ | ||
import React, { | ||
createContext, | ||
useState, | ||
useContext, | ||
FunctionComponent, | ||
useCallback, | ||
ReactNode, | ||
useEffect, | ||
} from 'react'; | ||
|
||
interface StopwatchContextType { | ||
time: number; | ||
isRunning: boolean; | ||
formattedTime: string; | ||
handleStartStop: () => void; | ||
handleReset: () => void; | ||
} | ||
|
||
const defaultContext: StopwatchContextType = { | ||
time: 0, | ||
isRunning: false, | ||
formattedTime: '00:00:00', | ||
handleStartStop: () => {}, | ||
handleReset: () => {}, | ||
}; | ||
|
||
interface StopwatchProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
const StopwatchContext = createContext<StopwatchContextType>(defaultContext); | ||
|
||
export const useStopwatch = () => useContext(StopwatchContext); | ||
|
||
export const StopwatchProvider: FunctionComponent<StopwatchProviderProps> = ({ | ||
children, | ||
}) => { | ||
const [startTime, setStartTime] = useState<number | null>(null); | ||
const [time, setTime] = useState<number>(0); | ||
const [isRunning, setIsRunning] = useState<boolean>(false); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
const formatTime = useCallback((time: number): string => { | ||
const hours = Math.floor(time / 3600000); | ||
const minutes = Math.floor((time % 3600000) / 60000); | ||
const seconds = Math.floor((time % 60000) / 1000); | ||
return `${hours.toString().padStart(2, '0')}:${minutes | ||
.toString() | ||
.padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; | ||
}, []); | ||
|
||
useEffect(() => { | ||
let interval: number | null = null; | ||
|
||
if (isRunning) { | ||
interval = window.setInterval(() => { | ||
if (startTime !== null) { | ||
const now = Date.now(); | ||
setTime(now - startTime); | ||
} | ||
}, 1000); | ||
} else if (!isRunning && interval !== null) { | ||
window.clearInterval(interval); | ||
} | ||
|
||
return () => { | ||
if (interval !== null) { | ||
window.clearInterval(interval); | ||
} | ||
}; | ||
}, [isRunning, startTime]); | ||
|
||
const handleStartStop = useCallback(() => { | ||
if (isRunning) { | ||
setIsRunning(false); | ||
} else { | ||
setStartTime((prevStartTime) => | ||
prevStartTime !== null ? prevStartTime : Date.now() - time, | ||
); | ||
setIsRunning(true); | ||
} | ||
}, [isRunning, time, startTime]); | ||
|
||
const handleReset = useCallback(() => { | ||
setIsRunning(false); | ||
setTime(0); | ||
setStartTime(null); | ||
}, []); | ||
|
||
const formattedTime = formatTime(time); | ||
|
||
return ( | ||
<StopwatchContext.Provider | ||
value={{ | ||
time, | ||
isRunning, | ||
formattedTime, | ||
handleStartStop, | ||
handleReset, | ||
}} | ||
> | ||
{children} | ||
</StopwatchContext.Provider> | ||
); | ||
}; |
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