From 0d7e0b839d3862379776c307a7cc4437114613da Mon Sep 17 00:00:00 2001 From: 1emu Date: Thu, 7 Sep 2023 18:36:19 -0300 Subject: [PATCH] fix: use Dayjs to calculate dates for voting stats --- src/components/Home/TopVoters.tsx | 11 ++++++----- src/hooks/useVotingStats.ts | 6 ++++-- src/utils/date/aMonthAgo.ts | 6 ++++++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 src/utils/date/aMonthAgo.ts diff --git a/src/components/Home/TopVoters.tsx b/src/components/Home/TopVoters.tsx index 8ac3dd420..11c808553 100644 --- a/src/components/Home/TopVoters.tsx +++ b/src/components/Home/TopVoters.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useMemo } from 'react' import { Card } from 'decentraland-ui/dist/components/Card/Card' import { Header } from 'decentraland-ui/dist/components/Header/Header' @@ -8,6 +8,7 @@ import { VOTES_VP_THRESHOLD } from '../../constants' import useFormatMessage from '../../hooks/useFormatMessage' import useTopVoters from '../../hooks/useTopVoters' import Time from '../../utils/date/Time' +import { getAMonthAgo } from '../../utils/date/aMonthAgo' import Helper from '../Helper/Helper' import HomeLoader from './HomeLoader' @@ -18,12 +19,12 @@ const createRow = ({ address, votes }: { address: string; votes: number }, idx: return } -const now = Time() -const aMonthAgo = now.subtract(1, 'month').toDate() - function TopVoters() { + const now = useMemo(() => Time().toDate(), []) + const aMonthAgo = useMemo(() => getAMonthAgo(now), []) + const t = useFormatMessage() - const { topVoters, isLoadingTopVoters } = useTopVoters(aMonthAgo, now.toDate()) + const { topVoters, isLoadingTopVoters } = useTopVoters(aMonthAgo, now) return ( diff --git a/src/hooks/useVotingStats.ts b/src/hooks/useVotingStats.ts index fe68f5361..d2fd36057 100644 --- a/src/hooks/useVotingStats.ts +++ b/src/hooks/useVotingStats.ts @@ -7,6 +7,8 @@ import { getQueryTimestamp } from '../clients/SnapshotGraphql' import { SnapshotProposal, SnapshotVote } from '../clients/SnapshotGraphqlTypes' import { calculateMatch, getChecksumAddress, outcomeMatch } from '../entities/Snapshot/utils' import { getFormattedPercentage } from '../helpers' +import Time from '../utils/date/Time' +import { getAMonthAgo } from '../utils/date/aMonthAgo' import { DEFAULT_QUERY_STALE_TIME } from './constants' @@ -47,8 +49,8 @@ function getParticipation( } export default function useVotingStats(address: string, userAddress: string | null) { - const now = useMemo(() => new Date(), []) - const aMonthAgo = useMemo(() => new Date(now.getFullYear(), now.getMonth() - 1, now.getDay()), [now]) + const now = useMemo(() => Time().toDate(), []) + const aMonthAgo = useMemo(() => getAMonthAgo(now), []) const { data: last30DaysProposals, isLoading: isLoadingProposals } = useQuery({ queryKey: [`last30DaysProposals#${aMonthAgo.getTime()}`], diff --git a/src/utils/date/aMonthAgo.ts b/src/utils/date/aMonthAgo.ts new file mode 100644 index 000000000..1abc4c066 --- /dev/null +++ b/src/utils/date/aMonthAgo.ts @@ -0,0 +1,6 @@ +import Time from './Time' + +export function getAMonthAgo(now: Date) { + const nowDayjs = Time(now) + return nowDayjs.subtract(1, 'month').toDate() +}