Skip to content

Commit 71a5a87

Browse files
authored
Merge pull request #2401 from oasisprotocol/lw/consistent-relative-time
Fix inconsistent relative time displays
2 parents b342df6 + c1fd212 commit 71a5a87

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

.changelog/2401.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix inconsistent relative time displays

src/app/components/TableCellAge/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { FC } from 'react'
22
import { Tooltip } from '@oasisprotocol/ui-library/src/components/tooltip'
33
import { formatDistanceToNow } from '../../utils/dateFormatter'
44
import { useFormattedTimestamp } from '../../hooks/useFormattedTimestamp'
5-
import { formatDistanceStrict } from 'date-fns/formatDistanceStrict'
65
import { useLocalSettings } from '../../hooks/useLocalSettings'
76
import { TableAgeType } from '../../../types/table-age-type'
87

@@ -26,9 +25,7 @@ export const TableCellAge: FC<{ sinceTimestamp: string }> = ({ sinceTimestamp })
2625
if (isNaN(date.getTime())) return null
2726

2827
const distance = formatDistanceToNow(date)
29-
const distanceWithSuffix = formatDistanceStrict(sinceTimestamp, new Date(), {
30-
addSuffix: true,
31-
})
28+
const distanceWithSuffix = formatDistanceToNow(date, { keepSuffix: true, style: 'long' })
3229
const title = (
3330
<>
3431
<div className="font-medium">{defaultFormatted}</div>

src/app/hooks/useFormattedTimestamp.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { formatDistanceStrict } from 'date-fns/formatDistanceStrict'
1+
import { formatDistanceToNow } from '../utils/dateFormatter'
22
import { useTranslation } from 'react-i18next'
33
import { useScreenSize } from './useScreensize'
44

@@ -41,8 +41,9 @@ export const useFormattedTimestampStringWithDistance = (
4141
const { isMobile } = useScreenSize()
4242
if (!timestampStr) return ''
4343
const timestamp = new Date(timestampStr)
44-
const distance = formatDistanceStrict(timestamp, new Date(), {
45-
addSuffix: true,
44+
const distance = formatDistanceToNow(timestamp, {
45+
keepSuffix: true,
46+
style: 'long',
4647
})
4748
return isMobile
4849
? distance

src/app/pages/RoflAppDetailsPage/LastActivity.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC } from 'react'
22
import { useTranslation } from 'react-i18next'
3-
import { formatDistanceStrict } from 'date-fns/formatDistanceStrict'
3+
import { formatDistanceToNow } from '../../utils/dateFormatter'
44
import { RuntimeTransaction } from '../../../oasis-nexus/api'
55
import { SearchScope } from '../../../types/searchScope'
66
import { TransactionLink } from '../../components/Transactions/TransactionLink'
@@ -26,8 +26,9 @@ export const LastActivity: FC<LastActivityProps> = ({ scope, transaction }) => {
2626
/>
2727
<span>
2828
(
29-
{formatDistanceStrict(transaction.timestamp, new Date(), {
30-
addSuffix: true,
29+
{formatDistanceToNow(new Date(transaction.timestamp), {
30+
keepSuffix: true,
31+
style: 'long',
3132
})}
3233
)
3334
</span>

src/app/utils/dateFormatter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export const intlDateFormat = (date: Date | number) => dateFormat.format(date)
1616
// TODO: Works only in en-US locale, as suffixes are hardcoded
1717
export const formatDistanceToNow = (
1818
date: Date | number,
19-
options: { baseDate?: Date | number; locale?: string; keepSuffix?: true } = {},
19+
options: { baseDate?: Date | number; locale?: string; keepSuffix?: true; style?: 'short' | 'long' } = {},
2020
) => {
21-
const { baseDate = new Date(), locale = 'en-US', keepSuffix = false } = options
21+
const { baseDate = new Date(), locale = 'en-US', keepSuffix = false, style = 'short' } = options
2222

2323
const diffInSeconds = differenceInSeconds(date, baseDate)
2424
let unit: Intl.RelativeTimeFormatUnit
@@ -41,7 +41,7 @@ export const formatDistanceToNow = (
4141

4242
const distanceWithSuffix = intlFormatDistance(date, baseDate, {
4343
unit,
44-
style: 'short',
44+
style,
4545
numeric: 'always',
4646
locale,
4747
})

0 commit comments

Comments
 (0)