-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actually, we should update this continually
- Loading branch information
Showing
3 changed files
with
50 additions
and
19 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,38 @@ | ||
import { useEffect, useState } from 'react' | ||
import { formatDistance } from 'date-fns' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import type { TFunction } from 'i18next' | ||
|
||
const UPDATE_TIME_INTERVAL_MS = 60000 | ||
|
||
// Given the last run timestamp, update the time since the last run on an interval. | ||
export function useUpdatedLastRunTime(lastRun: string | undefined): string { | ||
const { t } = useTranslation(['protocol_info']) | ||
|
||
const [updatedLastRun, setUpdatedLastRun] = useState(() => | ||
computeLastRunFromNow(lastRun, t as TFunction) | ||
) | ||
useEffect(() => { | ||
const timer = setInterval(() => { | ||
setUpdatedLastRun(computeLastRunFromNow(lastRun, t as TFunction)) | ||
}, UPDATE_TIME_INTERVAL_MS) | ||
|
||
return () => { | ||
clearInterval(timer) | ||
} | ||
}, [lastRun, t]) | ||
|
||
return updatedLastRun | ||
} | ||
|
||
function computeLastRunFromNow( | ||
lastRun: string | undefined, | ||
t: TFunction | ||
): string { | ||
return lastRun != null | ||
? formatDistance(new Date(lastRun), new Date(), { | ||
addSuffix: true, | ||
}).replace('about ', '') | ||
: t('no_history') | ||
} |
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