| import { useTranslate } from 'i18n-calypso'; |
| import { useState, useMemo, useEffect } from 'react'; |
|
|
| interface TimeSinceProps { |
| |
| |
| |
| date: string; |
| |
| |
| |
| |
| |
| |
| dateFormat?: string; |
| |
| |
| |
| className?: string; |
| |
| |
| |
| locale?: string; |
| } |
|
|
| |
| |
| |
| function useRelativeTime( date: string, dateFormat = 'll' ) { |
| const [ now, setNow ] = useState( () => new Date() ); |
| const translate = useTranslate(); |
|
|
| useEffect( () => { |
| const intervalId = setInterval( () => setNow( new Date() ), 10000 ); |
| return () => clearInterval( intervalId ); |
| }, [] ); |
|
|
| return useMemo( () => { |
| const dateObj = new Date( date ); |
| const millisAgo = now.getTime() - dateObj.getTime(); |
|
|
| |
| if ( isNaN( dateObj.getTime() ) || millisAgo < 0 ) { |
| return formatDate( dateObj, dateFormat ); |
| } |
|
|
| const secondsAgo = Math.floor( millisAgo / 1000 ); |
| const minutesAgo = Math.floor( secondsAgo / 60 ); |
| const hoursAgo = Math.floor( minutesAgo / 60 ); |
| const daysAgo = Math.floor( hoursAgo / 24 ); |
|
|
| |
| if ( secondsAgo < 60 ) { |
| return translate( 'just now' ); |
| } |
|
|
| |
| if ( minutesAgo < 60 ) { |
| return translate( '%(minutes)dm ago', { |
| args: { minutes: minutesAgo }, |
| comment: 'example for a resulting string: 2m ago', |
| } ); |
| } |
|
|
| |
| if ( hoursAgo < 24 ) { |
| return translate( '%(hours)dh ago', { |
| args: { hours: hoursAgo }, |
| comment: 'example for a resulting string: 5h ago', |
| } ); |
| } |
|
|
| |
| if ( daysAgo < 7 ) { |
| return translate( '%(days)dd ago', { |
| args: { days: daysAgo }, |
| comment: 'example for a resulting string: 4d ago', |
| } ); |
| } |
|
|
| |
| return formatDate( dateObj, dateFormat, translate.localeSlug ); |
| }, [ now, date, dateFormat, translate ] ); |
| } |
|
|
| |
| |
| |
| function formatDate( date: Date, format: string, locale?: string ): string { |
| if ( ! date || isNaN( date.getTime() ) ) { |
| return ''; |
| } |
|
|
| const formatOptions: Intl.DateTimeFormatOptions = { |
| dateStyle: 'medium', |
| timeStyle: 'short', |
| }; |
|
|
| if ( format === 'll' ) { |
| formatOptions.dateStyle = 'medium'; |
| delete formatOptions.timeStyle; |
| } else if ( format === 'lll' ) { |
| formatOptions.dateStyle = undefined; |
| formatOptions.timeStyle = undefined; |
| formatOptions.day = 'numeric'; |
| formatOptions.month = 'short'; |
| formatOptions.year = 'numeric'; |
| } else if ( format === 'llll' ) { |
| formatOptions.dateStyle = 'full'; |
| formatOptions.timeStyle = 'medium'; |
| } |
|
|
| return new Intl.DateTimeFormat( locale, formatOptions ).format( date ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function TimeSince( { className, date, dateFormat = 'll' }: TimeSinceProps ) { |
| const translate = useTranslate(); |
| const humanDate = useRelativeTime( date, dateFormat ); |
| const fullDate = formatDate( new Date( date ), 'llll', translate.localeSlug ); |
|
|
| return ( |
| <time className={ className } dateTime={ date } title={ fullDate }> |
| { humanDate } |
| </time> |
| ); |
| } |
|
|
| export default TimeSince; |
|
|