Force callers of `getTimeAgo` to pass in the value for "now" (#4560)
* Remove icky hook for now * Force callers of getTimeAgo to pass in the 'now' value * Update usage in Newskie dialogzio/stable
parent
fb76265fcc
commit
983d85384b
|
@ -30,12 +30,13 @@ export function NewskieDialog({
|
||||||
const moderation = moderateProfile(profile, moderationOpts)
|
const moderation = moderateProfile(profile, moderationOpts)
|
||||||
return sanitizeDisplayName(name, moderation.ui('displayName'))
|
return sanitizeDisplayName(name, moderation.ui('displayName'))
|
||||||
}, [moderationOpts, profile])
|
}, [moderationOpts, profile])
|
||||||
|
const [now] = React.useState(Date.now())
|
||||||
const timeAgo = useGetTimeAgo()
|
const timeAgo = useGetTimeAgo()
|
||||||
const createdAt = profile.createdAt as string | undefined
|
const createdAt = profile.createdAt as string | undefined
|
||||||
const daysOld = React.useMemo(() => {
|
const daysOld = React.useMemo(() => {
|
||||||
if (!createdAt) return Infinity
|
if (!createdAt) return Infinity
|
||||||
return differenceInSeconds(new Date(), new Date(createdAt)) / 86400
|
return differenceInSeconds(now, new Date(createdAt)) / 86400
|
||||||
}, [createdAt])
|
}, [createdAt, now])
|
||||||
|
|
||||||
if (!createdAt || daysOld > 7) return null
|
if (!createdAt || daysOld > 7) return null
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ export function NewskieDialog({
|
||||||
<Text style={[a.text_md]}>
|
<Text style={[a.text_md]}>
|
||||||
<Trans>
|
<Trans>
|
||||||
{profileName} joined Bluesky{' '}
|
{profileName} joined Bluesky{' '}
|
||||||
{timeAgo(createdAt, {format: 'long'})} ago
|
{timeAgo(createdAt, now, {format: 'long'})} ago
|
||||||
</Trans>
|
</Trans>
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {useCallback, useMemo} from 'react'
|
import {useCallback} from 'react'
|
||||||
import {msg, plural} from '@lingui/macro'
|
import {msg, plural} from '@lingui/macro'
|
||||||
import {I18nContext, useLingui} from '@lingui/react'
|
import {I18nContext, useLingui} from '@lingui/react'
|
||||||
import {differenceInSeconds} from 'date-fns'
|
import {differenceInSeconds} from 'date-fns'
|
||||||
|
@ -12,25 +12,16 @@ 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})
|
||||||
},
|
},
|
||||||
[_],
|
[_],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useTimeAgo(
|
|
||||||
date: number | string | Date,
|
|
||||||
options?: Omit<TimeAgoOptions, 'lingui'>,
|
|
||||||
): string {
|
|
||||||
const timeAgo = useGetTimeAgo()
|
|
||||||
return useMemo(() => {
|
|
||||||
return timeAgo(date, {...options})
|
|
||||||
}, [date, options, timeAgo])
|
|
||||||
}
|
|
||||||
|
|
||||||
const NOW = 5
|
const NOW = 5
|
||||||
const MINUTE = 60
|
const MINUTE = 60
|
||||||
const HOUR = MINUTE * 60
|
const HOUR = MINUTE * 60
|
||||||
|
|
|
@ -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})
|
||||||
|
|
|
@ -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) ? (
|
||||||
|
|
Loading…
Reference in New Issue