-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
165 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use client' | ||
|
||
import {useLayoutEffect, useState} from 'react' | ||
|
||
export function TimeSince({label, since}: {label: string; since: string}) { | ||
const [from, setFrom] = useState<null | Date>(null) | ||
const [now, setNow] = useState<null | Date>(null) | ||
useLayoutEffect(() => { | ||
setFrom(new Date(since)) | ||
const interval = setInterval(() => setNow(new Date()), 1000) | ||
return () => clearInterval(interval) | ||
}, [since]) | ||
|
||
let timeSince = '…' | ||
if (from && now) { | ||
timeSince = formatTimeSince(from, now) | ||
} | ||
|
||
return ( | ||
<div className="bg-theme-button text-theme-button absolute left-2 top-2 block rounded text-xs"> | ||
<span className="inline-block py-1 pl-2 pr-0.5">{label}:</span> | ||
<span className="bg-theme text-theme mr-0.5 inline-block rounded-r-sm px-1 py-0.5 tabular-nums"> | ||
rendered {timeSince} | ||
</span> | ||
</div> | ||
) | ||
} | ||
|
||
const rtf = new Intl.RelativeTimeFormat('en', {style: 'short'}) | ||
export function formatTimeSince(from: Date, to: Date): string { | ||
const seconds = Math.floor((from.getTime() - to.getTime()) / 1000) | ||
if (seconds > -60) { | ||
return rtf.format(Math.min(seconds, -1), 'second') | ||
} | ||
const minutes = Math.ceil(seconds / 60) | ||
if (minutes > -60) { | ||
return rtf.format(minutes, 'minute') | ||
} | ||
const hours = Math.ceil(minutes / 60) | ||
if (hours > -24) { | ||
return rtf.format(hours, 'hour') | ||
} | ||
const days = Math.ceil(hours / 24) | ||
return rtf.format(days, 'day') | ||
} |
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
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
Oops, something went wrong.