import React, { DOMAttributes, FunctionComponent, useEffect, useState } from 'react'; import { REQUEST_SETUP_TEARDOWN_COMPENSATION, REQUEST_TIME_TO_SHOW_COUNTER } from '../../common/constants'; interface Props { handleCancel: DOMAttributes['onClick']; loadStartTime: number; } export const ResponseTimer: FunctionComponent = ({ handleCancel, loadStartTime }) => { const [milliseconds, setMilliseconds] = useState(0); const isLoading = loadStartTime > 0; useEffect(() => { let interval: NodeJS.Timeout | null = null; if (isLoading) { interval = setInterval(() => { setMilliseconds(Date.now() - loadStartTime - REQUEST_SETUP_TEARDOWN_COMPENSATION); }, 100); } return () => { if (interval !== null) { setMilliseconds(0); clearInterval(interval); } }; }, [loadStartTime, isLoading]); if (!isLoading) { return null; } const seconds = milliseconds / 1000; return (

{seconds >= REQUEST_TIME_TO_SHOW_COUNTER ? `${seconds.toFixed(1)} seconds` : 'Loading'}...

); };