Force callers of getTimeAgo to pass in the 'now' value

This commit is contained in:
Eric Bailey
2024-06-18 13:11:00 -05:00
parent 674d99207b
commit 24ef2c73e8
3 changed files with 10 additions and 5 deletions
+3 -2
View File
@@ -12,10 +12,11 @@ export function useGetTimeAgo() {
const {_} = useLingui() const {_} = useLingui()
return useCallback( return useCallback(
( (
date: number | string | Date, earlier: number | string | Date,
later: number | string | Date,
options?: Omit<TimeAgoOptions, 'lingui'>, options?: Omit<TimeAgoOptions, 'lingui'>,
) => { ) => {
return dateDiff(date, Date.now(), {lingui: _, format: options?.format}) return dateDiff(earlier, later, {lingui: _, format: options?.format})
}, },
[_], [_],
) )
+4 -2
View File
@@ -15,12 +15,14 @@ export function TimeElapsed({
const ago = useGetTimeAgo() const ago = useGetTimeAgo()
const format = timeToString ?? ago const format = timeToString ?? ago
const tick = useTickEveryMinute() const tick = useTickEveryMinute()
const [timeElapsed, setTimeAgo] = React.useState(() => format(timestamp)) const [timeElapsed, setTimeAgo] = React.useState(() =>
format(timestamp, tick),
)
const [prevTick, setPrevTick] = React.useState(tick) const [prevTick, setPrevTick] = React.useState(tick)
if (prevTick !== tick) { if (prevTick !== tick) {
setPrevTick(tick) setPrevTick(tick)
setTimeAgo(format(timestamp)) setTimeAgo(format(timestamp, tick))
} }
return children({timeElapsed}) return children({timeElapsed})
+3 -1
View File
@@ -7,6 +7,7 @@ import {useFocusEffect} from '@react-navigation/native'
import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo' import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
import {getEntries} from '#/logger/logDump' import {getEntries} from '#/logger/logDump'
import {useTickEveryMinute} from '#/state/shell'
import {useSetMinimalShellMode} from '#/state/shell' import {useSetMinimalShellMode} from '#/state/shell'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {CommonNavigatorParams, NativeStackScreenProps} from 'lib/routes/types' import {CommonNavigatorParams, NativeStackScreenProps} from 'lib/routes/types'
@@ -24,6 +25,7 @@ export function LogScreen({}: NativeStackScreenProps<
const setMinimalShellMode = useSetMinimalShellMode() const setMinimalShellMode = useSetMinimalShellMode()
const [expanded, setExpanded] = React.useState<string[]>([]) const [expanded, setExpanded] = React.useState<string[]>([])
const timeAgo = useGetTimeAgo() const timeAgo = useGetTimeAgo()
const tick = useTickEveryMinute()
useFocusEffect( useFocusEffect(
React.useCallback(() => { React.useCallback(() => {
@@ -72,7 +74,7 @@ export function LogScreen({}: NativeStackScreenProps<
/> />
) : undefined} ) : undefined}
<Text type="sm" style={[styles.ts, pal.textLight]}> <Text type="sm" style={[styles.ts, pal.textLight]}>
{timeAgo(entry.timestamp)} {timeAgo(entry.timestamp, tick)}
</Text> </Text>
</TouchableOpacity> </TouchableOpacity>
{expanded.includes(entry.id) ? ( {expanded.includes(entry.id) ? (