-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: group timeline events by relative dates (#10708)
- Loading branch information
Showing
3 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |