Skip to content

Commit

Permalink
fix: use Dayjs to calculate dates for voting stats
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Sep 7, 2023
1 parent 608fb88 commit 0d7e0b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/components/Home/TopVoters.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -18,12 +19,12 @@ const createRow = ({ address, votes }: { address: string; votes: number }, idx:
return <TopVotersRow key={idx} address={address} votes={votes} rank={idx + 1} />
}

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 (
<Card className="TopVoters">
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useVotingStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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()}`],
Expand Down
6 changes: 6 additions & 0 deletions src/utils/date/aMonthAgo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Time from './Time'

export function getAMonthAgo(now: Date) {
const nowDayjs = Time(now)
return nowDayjs.subtract(1, 'month').toDate()
}

0 comments on commit 0d7e0b8

Please sign in to comment.