Skip to content

Commit

Permalink
Refactor with existing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tianrunhe committed Jan 20, 2025
1 parent fe5f53a commit 2973018
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
37 changes: 1 addition & 36 deletions packages/client/components/TimelineFeedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {useMemo} from 'react'
import {usePaginationFragment} from 'react-relay'
import {Link} from 'react-router-dom'
import useLoadNextOnScrollBottom from '~/hooks/useLoadNextOnScrollBottom'
import {TimelineGroup, getTimeGroup} from '~/utils/date/timelineGroups'
import {TimelineFeedListPaginationQuery} from '../__generated__/TimelineFeedListPaginationQuery.graphql'
import {TimelineFeedList_query$key} from '../__generated__/TimelineFeedList_query.graphql'
import TimelineEvent from './TimelineEvent'
Expand All @@ -13,42 +14,6 @@ const ResultScroller = styled('div')({
overflow: 'auto'
})

interface TimelineGroup {
date: Date
events: any[]
label: string
}

const getTimeGroup = (date: Date): {date: Date; label: string} => {
const now = new Date()
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const yesterday = new Date(today)
yesterday.setDate(yesterday.getDate() - 1)
const lastWeek = new Date(today)
lastWeek.setDate(lastWeek.getDate() - 7)
const lastMonth = new Date(today)
lastMonth.setMonth(lastMonth.getMonth() - 1)
const last3Months = new Date(today)
last3Months.setMonth(last3Months.getMonth() - 3)
const last6Months = new Date(today)
last6Months.setMonth(last6Months.getMonth() - 6)

if (date >= today) {
return {date: today, label: 'πŸŒ… Today'}
} else if (date >= yesterday) {
return {date: yesterday, label: 'πŸŒ™ Yesterday'}
} else if (date >= lastWeek) {
return {date: lastWeek, label: 'πŸ“… This week'}
} else if (date >= lastMonth) {
return {date: lastMonth, label: 'πŸ“† This month'}
} else if (date >= last3Months) {
return {date: last3Months, label: 'πŸ—“οΈ Past 3 months'}
} else if (date >= last6Months) {
return {date: last6Months, label: 'πŸ“š Past 6 months'}
}
return {date: last6Months, label: 'πŸ›οΈ Ancient history'}
}

interface Props {
queryRef: TimelineFeedList_query$key
}
Expand Down
12 changes: 6 additions & 6 deletions packages/client/utils/date/relativeDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import humanizeDuration from 'humanize-duration'
import plural from '../plural'

const SECOND = 1000
const MIN = SECOND * 60
const HOUR = MIN * 60
const DAY = HOUR * 24
const YEAR = DAY * 365
const MONTH = DAY * 30
export const SECOND = 1000
export const MIN = SECOND * 60
export const HOUR = MIN * 60
export const DAY = HOUR * 24
export const YEAR = DAY * 365
export const MONTH = DAY * 30

interface Opts {
max?: number
Expand Down
45 changes: 45 additions & 0 deletions packages/client/utils/date/timelineGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {DAY, MONTH} from './relativeDate'

interface TimelineGroup {
date: Date
events: any[]
label: string
}

export interface TimelineGrouping {
date: Date
label: string
}

const getStartOfDay = (date: Date): Date => {
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
}

export const getTimeGroup = (date: Date): TimelineGrouping => {
const now = new Date()
const today = getStartOfDay(now)
const yesterday = new Date(today.getTime() - DAY)
const lastWeek = new Date(today.getTime() - (7 * DAY))
const lastMonth = new Date(today.getTime() - (30 * DAY))
const last3Months = new Date(today.getTime() - (3 * MONTH))
const last6Months = new Date(today.getTime() - (6 * MONTH))

const compareDate = getStartOfDay(date)

if (compareDate >= today) {
return {date: today, label: 'πŸŒ… Today'}
} else if (compareDate >= yesterday) {
return {date: yesterday, label: 'πŸŒ™ Yesterday'}
} else if (compareDate >= lastWeek) {
return {date: lastWeek, label: 'πŸ“… This week'}
} else if (compareDate >= lastMonth) {
return {date: lastMonth, label: 'πŸ“† This month'}
} else if (compareDate >= last3Months) {
return {date: last3Months, label: 'πŸ—“οΈ Past 3 months'}
} else if (compareDate >= last6Months) {
return {date: last6Months, label: 'πŸ“š Past 6 months'}
}
return {date: last6Months, label: 'πŸ›οΈ Ancient history'}
}

export type {TimelineGroup}

0 comments on commit 2973018

Please sign in to comment.