Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow useRelativeTime to be auto-memoized #8089

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions packages/sanity/src/core/hooks/useRelativeTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use no memo'

import {
differenceInDays,
differenceInHours,
Expand Down Expand Up @@ -50,16 +48,22 @@ export interface RelativeTimeOptions {

/** @internal */
export function useRelativeTime(time: Date | string, options: RelativeTimeOptions = {}): string {
const resolved = useFormatRelativeTime(time, options)

const [, forceUpdate] = useReducer((x) => x + 1, 0)
const [now, updateNow] = useReducer(
// We don't care about the action input, every update should use the current time as the new state
() => Date.now(),
// Since we use the third argument of `useReducer`, this `null` doesn't end up anywhere
null,
// By using the lazy init we ensure that `Date.now()` is only called once during init, and then only when `updateNow` is called, instead of on every render
() => Date.now(),
)
const resolved = useFormatRelativeTime(time, options.relativeTo || now, options)

useEffect(() => {
let timerId: number | null

function tick(interval: number) {
timerId = window.setTimeout(() => {
forceUpdate()
updateNow()
// avoid pile-up of setInterval callbacks,
// e.g. schedule the next update at `refreshInterval` *after* the previous one finishes
timerId = window.setTimeout(() => tick(interval), interval)
Expand All @@ -75,12 +79,16 @@ export function useRelativeTime(time: Date | string, options: RelativeTimeOption
clearTimeout(timerId)
}
}
}, [forceUpdate, resolved.refreshInterval])
}, [resolved.refreshInterval])

return resolved.timestamp
}

function useFormatRelativeTime(date: Date | string, opts: RelativeTimeOptions = {}): TimeSpec {
function useFormatRelativeTime(
date: Date | string,
now: Date | number,
opts: Omit<RelativeTimeOptions, 'relativeTo'> = {},
): TimeSpec {
const {t} = useTranslation()
const currentLocale = useCurrentLocale().id

Expand Down Expand Up @@ -121,12 +129,11 @@ function useFormatRelativeTime(date: Date | string, opts: RelativeTimeOptions =
}
}

const now = opts.relativeTo || Date.now()
const diffMonths = differenceInMonths(now, parsedDate)
const diffYears = differenceInYears(now, parsedDate)

if (diffMonths || diffYears) {
if (opts.minimal && diffYears === 0) {
if (minimal && diffYears === 0) {
// same year
return {
timestamp: intlCache
Expand All @@ -136,7 +143,7 @@ function useFormatRelativeTime(date: Date | string, opts: RelativeTimeOptions =
}
}

if (opts.minimal) {
if (minimal) {
return {
timestamp: intlCache
.dateTimeFormat(currentLocale, {...DATE_ONLY_FORMAT, timeZone})
Expand Down
Loading