import { Tooltip } from "@mui/joy"; import dayjs from "dayjs"; import { observer } from "mobx-react-lite"; import { memo, useMemo } from "react"; import { workspaceStore } from "@/store/v2"; import type { ActivityCalendarProps, CalendarDay } from "@/types/statistics"; import { cn } from "@/utils"; import { useTranslate } from "@/utils/i18n"; const getCellOpacity = (ratio: number): string => { if (ratio === 0) return ""; if (ratio > 0.75) return "bg-green-700/90 text-gray-50 dark:bg-green-400/80"; if (ratio > 0.5) return "bg-green-700/70 text-gray-100 dark:bg-green-400/60"; if (ratio > 0.25) return "bg-green-700/70 text-gray-100 dark:bg-green-400/40"; return "bg-green-700/50 text-gray-100 dark:bg-green-400/20"; }; const CalendarCell = memo( ({ dayInfo, count, maxCount, isToday, isSelected, onClick, tooltipText, }: { dayInfo: CalendarDay; count: number; maxCount: number; isToday: boolean; isSelected: boolean; onClick?: () => void; tooltipText: string; }) => { const cellContent = (
0 && "cursor-pointer hover:scale-110", )} onClick={count > 0 ? onClick : undefined} > {dayInfo.day}
); if (!dayInfo.isCurrentMonth) { return (
{dayInfo.day}
); } return ( {cellContent} ); }, ); export const ActivityCalendar = memo( observer((props: ActivityCalendarProps) => { const t = useTranslate(); const { month: monthStr, data, onClick } = props; const weekStartDayOffset = workspaceStore.state.generalSetting.weekStartDayOffset; const { days, weekDays, maxCount } = useMemo(() => { const yearValue = dayjs(monthStr).toDate().getFullYear(); const monthValue = dayjs(monthStr).toDate().getMonth(); const dayInMonth = new Date(yearValue, monthValue + 1, 0).getDate(); const firstDay = (((new Date(yearValue, monthValue, 1).getDay() - weekStartDayOffset) % 7) + 7) % 7; const lastDay = new Date(yearValue, monthValue, dayInMonth).getDay() - weekStartDayOffset; const prevMonthDays = new Date(yearValue, monthValue, 0).getDate(); const WEEK_DAYS = [t("days.sun"), t("days.mon"), t("days.tue"), t("days.wed"), t("days.thu"), t("days.fri"), t("days.sat")]; const weekDaysOrdered = WEEK_DAYS.slice(weekStartDayOffset).concat(WEEK_DAYS.slice(0, weekStartDayOffset)); const daysArray: CalendarDay[] = []; // Previous month's days for (let i = firstDay - 1; i >= 0; i--) { daysArray.push({ day: prevMonthDays - i, isCurrentMonth: false }); } // Current month's days for (let i = 1; i <= dayInMonth; i++) { const date = dayjs(`${yearValue}-${monthValue + 1}-${i}`).format("YYYY-MM-DD"); daysArray.push({ day: i, isCurrentMonth: true, date }); } // Next month's days for (let i = 1; i < 7 - lastDay; i++) { daysArray.push({ day: i, isCurrentMonth: false }); } const maxCountValue = Math.max(...Object.values(data), 1); return { year: yearValue, month: monthValue, days: daysArray, weekDays: weekDaysOrdered, maxCount: maxCountValue, }; }, [monthStr, data, weekStartDayOffset, t]); const today = useMemo(() => dayjs().format("YYYY-MM-DD"), []); const selectedDateFormatted = useMemo(() => dayjs(props.selectedDate).format("YYYY-MM-DD"), [props.selectedDate]); return (
{weekDays.map((day, index) => (
{day}
))} {days.map((dayInfo, index) => { if (!dayInfo.isCurrentMonth) { return ( ); } const date = dayInfo.date!; const count = data[date] || 0; const isToday = today === date; const isSelected = selectedDateFormatted === date; const tooltipText = count === 0 ? date : t("memo.count-memos-in-date", { count: count, memos: count === 1 ? t("common.memo") : t("common.memos"), date: date, }).toLowerCase(); return ( onClick?.(date)} tooltipText={tooltipText} /> ); })}
); }), ); ActivityCalendar.displayName = "ActivityCalendar";