-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
542 additions
and
683 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import { useMediaQuery, useTheme } from "@mui/material"; | ||
import { FC } from "react"; | ||
import { FC, useEffect } from "react"; | ||
|
||
interface BrowserViewProps { | ||
onMobileView?: () => void; | ||
onBrowserView?: () => void; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const BrowserView: FC<BrowserViewProps> = ({ children }) => { | ||
export const BrowserView: FC<BrowserViewProps> = ({ | ||
onMobileView, | ||
onBrowserView, | ||
children, | ||
}) => { | ||
const theme = useTheme(); | ||
const isMobileView = useMediaQuery(theme.breakpoints.down("md")); | ||
|
||
// Only run callbacks after the component has rendered | ||
useEffect(() => { | ||
if (isMobileView) onMobileView?.(); | ||
else onBrowserView?.(); | ||
}, [isMobileView]); | ||
|
||
return isMobileView ? null : <>{children}</>; | ||
}; |
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,195 @@ | ||
import { useTheme } from "@mui/material"; | ||
import { FC, useContext, useMemo } from "react"; | ||
import { ScanContext } from "../Overview"; | ||
import "./heatmap.css"; | ||
import { Rect } from "./Rect"; | ||
import { DayData, HeatmapVariant } from "./types"; | ||
import { | ||
DATE_FORMATTER, | ||
DAYS_IN_WEEK, | ||
formatData, | ||
getColumnCountMonths, | ||
getMondayIndexedDay, | ||
isDayVariant, | ||
MILLISECONDS_IN_DAY, | ||
styleMonth, | ||
WEEKS_IN_MONTH, | ||
} from "./utils"; | ||
|
||
interface DayProps { | ||
startDate: Date; | ||
endDate: Date; | ||
columnCount: number; | ||
transform: string; | ||
isSmallView: boolean; | ||
variant: HeatmapVariant; | ||
} | ||
|
||
export const Day: FC<DayProps> = ({ | ||
startDate, | ||
endDate, | ||
columnCount, | ||
transform, | ||
isSmallView, | ||
variant, | ||
}) => { | ||
const theme = useTheme(); | ||
const { scans } = useContext(ScanContext); | ||
|
||
const data = useMemo<DayData>(() => { | ||
const normalizedScans = [...scans]; | ||
normalizedScans.forEach((scan) => scan.scanTime.setHours(0, 0, 0, 0)); | ||
const formattedScans = formatData(normalizedScans); | ||
|
||
const start = new Date( | ||
startDate.getTime() - | ||
startDate.getDay() * MILLISECONDS_IN_DAY + | ||
MILLISECONDS_IN_DAY | ||
); | ||
|
||
const startDates = [ | ||
...Array(getColumnCountMonths(startDate, endDate)), | ||
].map((_, idx) => { | ||
const newStartDate = new Date(startDate); | ||
if (idx === 0) { | ||
while (newStartDate.getDay() !== 1) { | ||
newStartDate.setDate(newStartDate.getDate() - 1); | ||
} | ||
} else { | ||
newStartDate.setMonth(newStartDate.getMonth() + idx); | ||
newStartDate.setDate(1); | ||
while (newStartDate.getDay() !== 1) { | ||
newStartDate.setDate(newStartDate.getDate() + 1); | ||
} | ||
} | ||
|
||
return newStartDate; | ||
}); | ||
|
||
const endWeek = new Date( | ||
endDate.getTime() + | ||
MILLISECONDS_IN_DAY * | ||
(DAYS_IN_WEEK - | ||
(getMondayIndexedDay(endDate) % DAYS_IN_WEEK)) | ||
); | ||
|
||
return { | ||
data: formattedScans, | ||
start, | ||
endWeek, | ||
startDates, | ||
}; | ||
}, [scans, startDate, endDate]); | ||
|
||
return ( | ||
<g transform={transform}> | ||
{[...Array(columnCount)].map((_, idx) => { | ||
return ( | ||
<g key={idx}> | ||
{isDayVariant(variant) | ||
? [...Array(DAYS_IN_WEEK)].map((_, cidx) => { | ||
const currentDate = new Date( | ||
data.start.getTime() + | ||
MILLISECONDS_IN_DAY * | ||
(idx * DAYS_IN_WEEK + cidx) | ||
); | ||
|
||
if ( | ||
currentDate.getTime() < | ||
startDate.getTime() | ||
) | ||
return null; | ||
|
||
if (currentDate.getTime() > endDate.getTime()) | ||
return null; | ||
|
||
let colors = theme.heatmap.colorInActive; | ||
if (data.data[currentDate.getTime()]) | ||
colors = theme.heatmap.colorActive; | ||
|
||
const dataTooltipContent = `${ | ||
data.data[currentDate.getTime()] | ||
? "Present" | ||
: "Absent" | ||
} on ${DATE_FORMATTER.format(currentDate)}`; | ||
|
||
return ( | ||
<Rect | ||
key={cidx} | ||
idx={idx} | ||
cidx={cidx} | ||
isSmallView={isSmallView} | ||
colors={colors} | ||
dataTooltipContent={ | ||
dataTooltipContent | ||
} | ||
/> | ||
); | ||
}) | ||
: [...Array(WEEKS_IN_MONTH)].map((_, cidx) => { | ||
const currentDate = new Date( | ||
data.startDates[idx].getTime() + | ||
MILLISECONDS_IN_DAY * | ||
cidx * | ||
DAYS_IN_WEEK | ||
); | ||
|
||
// Week is no longer in the month | ||
if ( | ||
currentDate.getMonth() > | ||
startDate.getMonth() + idx && | ||
getMondayIndexedDay(currentDate) <= | ||
currentDate.getDate() - 1 | ||
) | ||
return null; | ||
|
||
// Week is after end date | ||
if ( | ||
currentDate.getTime() >= | ||
data.endWeek.getTime() | ||
) | ||
return null; | ||
|
||
const count = [...Array(DAYS_IN_WEEK)] | ||
.map( | ||
(_, i) => | ||
new Date( | ||
currentDate.getTime() + | ||
i * MILLISECONDS_IN_DAY | ||
) | ||
) | ||
.filter( | ||
(date) => | ||
date.getTime() <= | ||
endDate.getTime() && | ||
data.data[date.getTime()] | ||
).length; | ||
|
||
const colors = | ||
styleMonth[Math.min(count, 5)](theme); // Can be higher than 5 if multiple scans in a day or scanned during the weekend | ||
|
||
const dataTooltipContent = `${count} scan${ | ||
count !== 1 ? "s" : "" | ||
} in the week of ${DATE_FORMATTER.format( | ||
currentDate | ||
)}`; | ||
|
||
return ( | ||
<Rect | ||
key={cidx} | ||
idx={idx} | ||
cidx={cidx} | ||
isSmallView={isSmallView} | ||
colors={colors} | ||
dataTooltipContent={ | ||
dataTooltipContent | ||
} | ||
/> | ||
); | ||
})} | ||
</g> | ||
); | ||
})} | ||
</g> | ||
); | ||
}; |
Oops, something went wrong.