Skip to content

Commit

Permalink
Pass locale to date-fns (#7)
Browse files Browse the repository at this point in the history
* Pass locale to date-fns

* Implement useDateFns
  • Loading branch information
selankon authored Jun 6, 2024
1 parent aa66e47 commit 198d5ca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/components/Blocks/BlockCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Box, Card, CardBody, Flex, Text } from '@chakra-ui/react'
import { formatDistance } from 'date-fns'
import { Trans } from 'react-i18next'
import { ReducedTextAndCopy } from '~components/CopyBtn'
import { useDateFns } from '~i18n/use-date-fns'

export const BlockCard = ({ height, time, proposer }: { height: number; time: string; proposer: string }) => {
const date = new Date(time)
const { formatDistance } = useDateFns()

return (
<Card>
Expand All @@ -13,7 +14,7 @@ export const BlockCard = ({ height, time, proposer }: { height: number; time: st
<Flex gap={3}>
<Text fontWeight='bold'># {height}</Text>
<Text fontWeight={100} color={'lighterText'}>
{formatDistance(date, new Date(), { addSuffix: true })}
{formatDistance(date, new Date())}
</Text>
</Flex>
<Box fontSize={'sm'}>
Expand Down
9 changes: 4 additions & 5 deletions src/components/Stats/ChainInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Flex, Heading, SkeletonText, Text } from '@chakra-ui/react'
import { formatDistance } from 'date-fns'
import { useTranslation } from 'react-i18next'
import { useChainInfo } from '~queries/stats'
import { useDateFns } from '~i18n/use-date-fns'

export const ChainInfo = () => {
const { t } = useTranslation()
const { data: stats, isLoading } = useChainInfo()
const { data: stats } = useChainInfo()
const { formatDistance } = useDateFns()

const syncing = stats?.syncing ? t('stats.syncing') : t('stats.in_sync')
const genesisBlockDate = stats?.genesisTime
? formatDistance(new Date(stats?.genesisTime), new Date(), { addSuffix: true })
: ''
const genesisBlockDate = stats?.genesisTime ? formatDistance(new Date(stats?.genesisTime), new Date()) : ''

const statsCards = [
{
Expand Down
15 changes: 15 additions & 0 deletions src/i18n/use-date-fns.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { format as dformat, formatDistance as dformatDistance } from 'date-fns'
import { useTranslation } from 'react-i18next'
import { datesLocale } from './locales'

export const useDateFns = () => {
const { i18n } = useTranslation()
return {
format: (date: Date | number, format: string) => dformat(date, format, { locale: datesLocale(i18n.language) }),
formatDistance: (date: Date | number, baseDate: Date | number) =>
dformatDistance(date, baseDate, {
addSuffix: true,
locale: datesLocale(i18n.language),
}),
}
}

2 comments on commit 198d5ca

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.