Skip to content

Commit

Permalink
Merge pull request #176 from dappforce/deploy/stats
Browse files Browse the repository at this point in the history
Fix timezone issues in Stats
  • Loading branch information
olehmell authored Jan 18, 2024
2 parents 89db09c + 435b48c commit cd232c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/components/statistics/Statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function InnerStatistics(props: FormProps) {
const AFTER_PARACHAIN_MIGRATION_DATE = new Date(2022, 8, 1)
const MAX_DAY_DIFF = dayjs(new Date()).diff(AFTER_PARACHAIN_MIGRATION_DATE, 'days')

const cachedData: Record<number, StatType[]> = {}
export function Statistics(props: FormProps) {
const address = useMyAddress()
const getActivityCountStat = useGetActivityCountStat()
Expand Down Expand Up @@ -182,12 +183,18 @@ export function Statistics(props: FormProps) {
combineOldLikesAndSuperLikes(statisticsDataArr, superLikeData)

if (isMounted) {
cachedData[constrainedPeriod] = statisticsDataArr
setData(statisticsDataArr)
setIsLoaded(true)
}
}

load()
if (cachedData[constrainedPeriod]) {
setData(cachedData[constrainedPeriod])
setIsLoaded(true)
} else {
load()
}

return () => {
isMounted = false
Expand Down Expand Up @@ -251,7 +258,7 @@ function combineOldLikesAndSuperLikes(
likesStat.statisticsData.forEach(stat => allLikesMap.set(stat.format_date, stat))

superLikesStats.data.forEach(stat => {
const dateFormat = dayjs(stat.dayUnixTimestamp * 1000).format('YYYY-MM-DD')
const dateFormat = dayjs.utc(stat.dayUnixTimestamp * 1000).format('YYYY-MM-DD')
const existingStat = allLikesMap.get(dateFormat)
if (existingStat) {
existingStat.count += stat.count
Expand Down
17 changes: 3 additions & 14 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@ import dayjs from 'dayjs'

export type DateIntervalType = 'day' | 'month' | 'year'
export function getDateWithOffset(offset: number, interval: DateIntervalType = 'day') {
const now = new Date()
const date = now.getDate()
const month = now.getMonth()
const year = now.getFullYear()

const multipliers: { [key in DateIntervalType]: number } = {
day: 1,
month: 30,
year: 365,
}
let multiplier = multipliers[interval]

const offsetDate = date - offset * multiplier
return new Date(year, month, offsetDate).toISOString()
const now = dayjs.utc().startOf('day')
const offsetted = now.subtract(offset, interval)
return offsetted.toISOString()
}

const DIVISIONS: { amount: number; name: Intl.RelativeTimeFormatUnit }[] = [
Expand Down

0 comments on commit cd232c5

Please sign in to comment.