-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29a2752
commit 068aa76
Showing
3 changed files
with
63 additions
and
5 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
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,58 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import { useEffect, useState } from "react" | ||
|
||
// Set numeric to auto so we get 'yesterday' instead of '1 day ago' | ||
const rtf = new Intl.RelativeTimeFormat(undefined, { | ||
numeric: 'auto' | ||
}) | ||
|
||
const second = 1000 | ||
const minute = 60 * second | ||
const hour = 60 * minute | ||
const day = 24 * hour | ||
const week = 7 * day | ||
const year = 365 * day | ||
|
||
const durations = { | ||
second, minute, hour, day, week, year | ||
} | ||
|
||
// Formats a time relative to now as the rounded number of units. | ||
export const getRelativeTime = (time: Date | number) => { | ||
const now = Date.now() | ||
const then = new Date(time).getTime() | ||
const diff = then - now | ||
|
||
const [unit, duration] = (Object.entries(durations) as any).findLast(([, duration]: [string, number]) => Math.abs(diff) > duration) ?? ['second', 1000] | ||
const amount = Math.floor(diff / duration) | ||
return { | ||
formatted: rtf.format(amount, unit ), | ||
updatesIn: duration - (Math.abs(diff - amount)), | ||
} | ||
} | ||
|
||
export default function useRelativeTime(time: Date | number) { | ||
const [formatted, setFormatted] = useState('') | ||
|
||
useEffect(() => { | ||
if (!time) return | ||
let timeout: NodeJS.Timeout | ||
const update = () => { | ||
const { formatted, updatesIn } = getRelativeTime(time) | ||
setFormatted(formatted) | ||
setTimeout(update, updatesIn) | ||
} | ||
|
||
update() | ||
|
||
return () => { | ||
clearTimeout(timeout) | ||
} | ||
}, [time]) | ||
|
||
return formatted | ||
} |