This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(severity-indicator): grouped timeline logic into single logic.ts…
… file as all individual utilities are very small yet depent on each other; cleaned up JSX a bit;
- Loading branch information
1 parent
adc0a17
commit c2d5c9b
Showing
7 changed files
with
81 additions
and
66 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/app/src/components/severity-indicator-tile/components/timeline/logic.ts
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,72 @@ | ||
import { middleOfDayInSeconds } from '@corona-dashboard/common'; | ||
import { createDateFromUnixTimestamp } from '~/utils/create-date-from-unix-timestamp'; | ||
import { SeverityIndicatorTimelineEventConfig } from './timeline'; | ||
|
||
/** | ||
* Calculated the difference, in days, between two dates. | ||
* @param start - the start date of a given range | ||
* @param end - the end date of a given range | ||
* @returns the number of days between two dates | ||
*/ | ||
export const getDifferenceInDays = (start: Date, end: Date): number => { | ||
return Math.floor((end.getTime() - start.getTime()) / (1000 * 3600 * 24)); | ||
}; | ||
|
||
/** | ||
* Calculates the date representing the middle of the day for passed in dates. | ||
* @param today - today's Date object | ||
* @param startDate - the start date of a given range | ||
* @param endDate - the end date of a given range | ||
* @returns the date representing the middle of the day for the passed in dates | ||
*/ | ||
export const getMiddleOfDayDates = (today: Date, startDate: number, endDate: number): Record<string, Date> => { | ||
const todayMiddleOfDaySeconds = middleOfDayInSeconds(today.getTime() / 1000); | ||
|
||
return { | ||
startOfIntervalDate: createDateFromUnixTimestamp(middleOfDayInSeconds(startDate)), | ||
endOfIntervalDate: createDateFromUnixTimestamp(middleOfDayInSeconds(endDate)), | ||
todayMiddleOfDayDate: createDateFromUnixTimestamp(todayMiddleOfDaySeconds), | ||
}; | ||
}; | ||
|
||
/** | ||
* Calculates the relative offset value for the 'Today' label on the timeline. | ||
* @param today - today's Date object | ||
* @param startDate - the start date of the timeline | ||
* @param endDate - the end date of the timeline | ||
* @returns the relative offset value for the 'Today' label on the timeline | ||
*/ | ||
export const getTimelineBarArrowOffset = (today: Date, startDate: number, endDate: number): number => { | ||
const { startOfIntervalDate, endOfIntervalDate, todayMiddleOfDayDate } = getMiddleOfDayDates(today, startDate, endDate); | ||
const numberOfDaysInInterval = getDifferenceInDays(startOfIntervalDate, endOfIntervalDate); | ||
const numberOfDaysFromStartToToday = getDifferenceInDays(startOfIntervalDate, todayMiddleOfDayDate); | ||
const arrowOffset = (numberOfDaysFromStartToToday / numberOfDaysInInterval) * 100; | ||
|
||
return arrowOffset; | ||
}; | ||
|
||
/** | ||
* Calculates the amount of relative width a timeline bar part covers. | ||
* @param daysInBarPart - the amount of days in a timeline bar part | ||
* @param totalDays - the total amount of days in a timeline | ||
* @returns the relative width of a timeline bar part | ||
*/ | ||
export const getTimelineBarPartWidth = (daysInBarPart: ReturnType<typeof getDifferenceInDays>, totalDays: number) => (daysInBarPart / totalDays) * 100; | ||
|
||
/** | ||
* Calculates the start and end date of a given timeline range. | ||
* @param timelineEvents - the timeline events/range to calculate the start and end dates for | ||
* @returns the start and end date of the given timeline range | ||
*/ | ||
export const getTimelineRangeDates = (timelineEvents: SeverityIndicatorTimelineEventConfig[]) => { | ||
const timelineEventDates = timelineEvents | ||
.flatMap((timelineEvent: SeverityIndicatorTimelineEventConfig) => [timelineEvent.start, timelineEvent.end]) | ||
.map((timelineEventDate: number) => { | ||
return createDateFromUnixTimestamp(timelineEventDate).getTime(); | ||
}); | ||
|
||
return { | ||
startDate: Math.min(...timelineEventDates), | ||
endDate: Math.max(...timelineEventDates), | ||
}; | ||
}; |
28 changes: 0 additions & 28 deletions
28
...onents/severity-indicator-tile/components/timeline/logic/get-timeline-bar-arrow-offset.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...omponents/severity-indicator-tile/components/timeline/logic/get-timeline-bar-part-days.ts
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
...mponents/severity-indicator-tile/components/timeline/logic/get-timeline-bar-part-width.ts
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
.../components/severity-indicator-tile/components/timeline/logic/get-timeline-range-dates.ts
This file was deleted.
Oops, something went wrong.
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