Refactor TimeDisplay component to improve time difference calculation

- Updated the time difference logic to handle both past and future dates more accurately.
- Simplified the calculation process by introducing base and compare dates.
- Enhanced the output format to include "in" for future dates and improved readability of time strings.
This commit is contained in:
Tom Butcher 2025-12-28 01:09:11 +00:00
parent cefe77bc0e
commit 0bf16d844e

View File

@ -8,25 +8,33 @@ const { Text } = Typography
const formatTimeDifference = (dateTime) => { const formatTimeDifference = (dateTime) => {
const now = dayjs() const now = dayjs()
const diff = dayjs(dateTime) const target = dayjs(dateTime)
const isFuture = target.isAfter(now)
const years = now.diff(diff, 'year') // If future, calculate from target to now; if past, from now to target
const months = now.diff(diff.add(years, 'year'), 'month') const baseDate = isFuture ? target : now
const weeks = now.diff(diff.add(years, 'year').add(months, 'month'), 'week') const compareDate = isFuture ? now : target
const days = now.diff(
diff.add(years, 'year').add(months, 'month').add(weeks, 'week'), const years = baseDate.diff(compareDate, 'year')
const months = baseDate.diff(compareDate.add(years, 'year'), 'month')
const weeks = baseDate.diff(
compareDate.add(years, 'year').add(months, 'month'),
'week'
)
const days = baseDate.diff(
compareDate.add(years, 'year').add(months, 'month').add(weeks, 'week'),
'day' 'day'
) )
const hours = now.diff( const hours = baseDate.diff(
diff compareDate
.add(years, 'year') .add(years, 'year')
.add(months, 'month') .add(months, 'month')
.add(weeks, 'week') .add(weeks, 'week')
.add(days, 'day'), .add(days, 'day'),
'hour' 'hour'
) )
const minutes = now.diff( const minutes = baseDate.diff(
diff compareDate
.add(years, 'year') .add(years, 'year')
.add(months, 'month') .add(months, 'month')
.add(weeks, 'week') .add(weeks, 'week')
@ -34,8 +42,8 @@ const formatTimeDifference = (dateTime) => {
.add(hours, 'hour'), .add(hours, 'hour'),
'minute' 'minute'
) )
const seconds = now.diff( const seconds = baseDate.diff(
diff compareDate
.add(years, 'year') .add(years, 'year')
.add(months, 'month') .add(months, 'month')
.add(weeks, 'week') .add(weeks, 'week')
@ -45,21 +53,24 @@ const formatTimeDifference = (dateTime) => {
'second' 'second'
) )
let timeStr = ''
if (years > 0) { if (years > 0) {
return `${years}y` timeStr = `${years} ${years === 1 ? 'year' : 'years'}`
} else if (months > 0) { } else if (months > 0) {
return `${months}mo` timeStr = `${months} ${months === 1 ? 'month' : 'months'}`
} else if (weeks > 0) { } else if (weeks > 0) {
return `${weeks}w` timeStr = `${weeks} ${weeks === 1 ? 'week' : 'weeks'}`
} else if (days > 0) { } else if (days > 0) {
return `${days}d` timeStr = `${days} ${days === 1 ? 'day' : 'days'}`
} else if (hours > 0) { } else if (hours > 0) {
return `${hours}h` timeStr = `${hours} ${hours === 1 ? 'hour' : 'hours'}`
} else if (minutes > 0) { } else if (minutes > 0) {
return `${minutes}m` timeStr = `${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`
} else { } else {
return `${seconds}s` timeStr = `${seconds} ${seconds === 1 ? 'second' : 'seconds'}`
} }
return isFuture ? `in ${timeStr} time` : `${timeStr} ago`
} }
const TimeDisplay = ({ const TimeDisplay = ({
@ -98,7 +109,7 @@ const TimeDisplay = ({
return ( return (
<Flex align={'center'} gap={'small'}> <Flex align={'center'} gap={'small'}>
<Text type={type}>{formattedDate}</Text> <Text type={type}>{formattedDate}</Text>
{showSince ? <Tag style={{ margin: 0 }}>{timeAgo + ' ago'}</Tag> : null} {showSince ? <Tag style={{ margin: 0 }}>{timeAgo}</Tag> : null}
</Flex> </Flex>
) )
} }