import dayjs from 'dayjs'; import customParseFormat from 'dayjs/plugin/customParseFormat'; import { useCallback, useEffect, useMemo, useRef, useState, type ReactElement } from 'react'; import Measure, { BoundingRect, type MeasuredComponentProps } from 'react-measure'; import clsx from 'clsx'; import './rds-comp-contribution.scss'; import SvgIcon from '@mui/material/SvgIcon'; dayjs.extend(customParseFormat); const COLUMNS = 53; export interface RdsCompContributionProps { showMonthLabels?: boolean; showWeekLabels?: boolean; weekNames?: string[]; monthNames?: string[]; panelColors?: string[]; values: { [date: string]: number }; until: string; dateFormat?: string; weekLabelAttributes?: React.SVGProps; monthLabelAttributes?: React.SVGProps; panelAttributes?: React.SVGProps; monthLabelHeight?: number; weekLabelWidth?: number; panelSize?: number; panelMargin?: number; } const RdsCompContribution = ({ showMonthLabels = true, showWeekLabels = false, weekNames, monthNames, panelColors, values, until, dateFormat = 'YYYY-MM-DD', weekLabelAttributes, monthLabelAttributes, panelAttributes, monthLabelHeight = 28, weekLabelWidth = 24, panelSize = 11, panelMargin = 2, }) => { const columns = COLUMNS; const [viewportWidth, setViewportWidth] = useState(() => (typeof window === 'undefined' ? 1024 : window.innerWidth)); const handleResizeRef = useRef<() => void>(null!); handleResizeRef.current = () => { setViewportWidth(window.innerWidth); }; useEffect(() => { const handler = () => handleResizeRef.current(); handler(); window.addEventListener('resize', handler); return () => { window.removeEventListener('resize', handler); }; }, []); const isMobile = viewportWidth <= 414; const { dynamicPanelSize, dynamicPanelMargin } = useMemo(() => { if (viewportWidth <= 320) { return { dynamicPanelSize: 8, dynamicPanelMargin: 1.2 }; } if (viewportWidth <= 414) { return { dynamicPanelSize: 9, dynamicPanelMargin: 1.2 }; } if (viewportWidth <= 834) { return { dynamicPanelSize: 8, dynamicPanelMargin: 2 }; } return { dynamicPanelSize: panelSize, dynamicPanelMargin: panelMargin }; }, [panelMargin, panelSize, viewportWidth]); const getPanelPosition = useCallback((colIndex: number, rowIndex: number) => { const bounds = dynamicPanelSize + dynamicPanelMargin; return { x: weekLabelWidth + bounds * colIndex, y: monthLabelHeight + bounds * rowIndex, }; }, [dynamicPanelMargin, dynamicPanelSize, monthLabelHeight, weekLabelWidth]); const makeCalendarData = useCallback((history: { [k: string]: number }, lastDay: string, columnCount: number) => { const d = dayjs(lastDay, dateFormat); const lastWeekend = d.endOf('week'); const endDate = d.endOf('day'); const result: ({ value: number; month: number; date: string } | null)[][] = []; for (let i = 0; i < columnCount; i++) { result[i] = []; for (let j = 0; j < 7; j++) { const date = lastWeekend.subtract((columnCount - i - 1) * 7 + (6 - j), 'day'); if (date.isBefore(endDate) || date.isSame(endDate)) { result[i][j] = { value: history[date.format(dateFormat)] || 0, month: date.month(), date: date.format(dateFormat), }; } else { result[i][j] = null; } } } return result; }, [dateFormat]); const updateSize = useCallback((size?: BoundingRect) => { if (!size) return; setViewportWidth(size.width); }, []); const contributions = useMemo(() => { if (!values || !until || !panelColors) return null; return makeCalendarData(values, until, columns); }, [values, until, columns, panelColors, makeCalendarData]); const weekLabels = useMemo(() => { if (!showWeekLabels) return null; if (!weekNames || weekNames.length < 7) return null; return Array.from({ length: 7 }, (_, j) => ( {weekNames[j] || ''} )); }, [ dynamicPanelMargin, dynamicPanelSize, monthLabelHeight, showWeekLabels, weekLabelAttributes, weekLabelWidth, weekNames, ]); const contributionPanels = useMemo(() => { if (!contributions || !panelColors) return []; const panels: ReactElement[] = []; for (let i = 0; i < columns; i++) { for (let j = 0; j < 7; j++) { const contribution = contributions[i][j]; if (contribution === null) continue; const pos = getPanelPosition(i, j); const numOfColors = panelColors?.length ?? 0; const color = contribution.value >= numOfColors ? panelColors?.[numOfColors - 1] : panelColors?.[contribution.value]; panels.push( ); } } return panels; }, [ columns, contributions, dynamicPanelSize, panelAttributes, panelColors, getPanelPosition, ]); const monthLabels = useMemo(() => { if (!contributions) return null; if (!monthNames || monthNames.length !== 12) return null; if (!showMonthLabels) return null; let janIndex = -1; let decIndex = -1; const monthPositions: { month: number; position: number }[] = []; for (let i = 0; i < columns; i++) { const c = contributions[i][0]; if (c === null) continue; if (c.month === 0 && janIndex === -1) janIndex = i; if (c.month === 11) decIndex = i; } if (janIndex === -1) janIndex = 0; if (decIndex === -1) decIndex = columns - 1; let prevMonth = -1; for (let i = janIndex; i <= decIndex; i++) { const c = contributions[i][0]; if (c === null) continue; if (c.month !== prevMonth) { monthPositions.push({ month: c.month, position: i }); prevMonth = c.month; } } if (monthPositions.length && monthPositions[0].month !== 0) { monthPositions.unshift({ month: 0, position: 0 }); } if (monthPositions.length && monthPositions[monthPositions.length - 1].month !== 11) { monthPositions.push({ month: 11, position: columns - 1 }); } const getMonthLabel = (month: number) => { if (isMobile) { return (monthNames[month] || '').slice(0, 3); } return monthNames[month] || ''; }; return monthPositions.map((monthPos, i) => { const { month, position } = monthPos; const nextPosition = i < monthPositions.length - 1 ? monthPositions[i + 1].position : columns; const midPosition = position + Math.floor((nextPosition - position) / 2); const textBasePos = getPanelPosition(midPosition, 0); return ( {getMonthLabel(month)} ); }); }, [ columns, contributions, dynamicPanelSize, getPanelPosition, isMobile, monthLabelAttributes, monthLabelHeight, monthNames, showMonthLabels, ]); if (!panelColors) { return null; } if (!values || !until) { return null; } if (!contributions) { return null; } const calculatedSvgWidth = columns * (dynamicPanelSize + dynamicPanelMargin) + weekLabelWidth + dynamicPanelSize; const svgWidth = Math.max(calculatedSvgWidth, 280); const svgHeight = 7 * (dynamicPanelSize + dynamicPanelMargin) + monthLabelHeight; return ( updateSize(rect.bounds)}> {({ measureRef }: MeasuredComponentProps) => (
{weekLabels} {contributionPanels} {monthLabels}
)}
); }; RdsCompContribution.displayName = "RdsCompContribution"; export default RdsCompContribution;