|
1 | | -import dayjs from "dayjs"; |
2 | 1 | import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { CalendarPopover } from "@/components/ActivityCalendar"; |
| 4 | +import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; |
| 5 | +import useCurrentUser from "@/hooks/useCurrentUser"; |
| 6 | +import { useFilteredMemoStats } from "@/hooks/useFilteredMemoStats"; |
3 | 7 | import i18n from "@/i18n"; |
| 8 | +import { addMonths, formatMonth, getMonthFromDate, getYearFromDate, setYearAndMonth } from "@/lib/calendar-utils"; |
4 | 9 | import type { MonthNavigatorProps } from "@/types/statistics"; |
5 | 10 |
|
6 | 11 | export const MonthNavigator = ({ visibleMonth, onMonthChange }: MonthNavigatorProps) => { |
7 | | - const currentMonth = dayjs(visibleMonth).toDate(); |
| 12 | + const currentUser = useCurrentUser(); |
| 13 | + const [isOpen, setIsOpen] = useState(false); |
| 14 | + const currentMonth = new Date(visibleMonth); |
| 15 | + const currentYear = getYearFromDate(visibleMonth); |
| 16 | + const currentMonthNum = getMonthFromDate(visibleMonth); |
| 17 | + |
| 18 | + const { statistics } = useFilteredMemoStats({ |
| 19 | + userName: currentUser?.name, |
| 20 | + }); |
8 | 21 |
|
9 | 22 | const handlePrevMonth = () => { |
10 | | - onMonthChange(dayjs(visibleMonth).subtract(1, "month").format("YYYY-MM")); |
| 23 | + onMonthChange(addMonths(visibleMonth, -1)); |
11 | 24 | }; |
12 | 25 |
|
13 | 26 | const handleNextMonth = () => { |
14 | | - onMonthChange(dayjs(visibleMonth).add(1, "month").format("YYYY-MM")); |
| 27 | + onMonthChange(addMonths(visibleMonth, 1)); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleDateClick = (date: string) => { |
| 31 | + onMonthChange(formatMonth(date)); |
| 32 | + setIsOpen(false); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleYearChange = (year: number) => { |
| 36 | + onMonthChange(setYearAndMonth(year, currentMonthNum)); |
15 | 37 | }; |
16 | 38 |
|
17 | 39 | return ( |
18 | 40 | <div className="w-full mb-1 flex flex-row justify-between items-center gap-1"> |
19 | | - <span className="relative text-sm text-muted-foreground"> |
20 | | - {currentMonth.toLocaleString(i18n.language, { year: "numeric", month: "long" })} |
21 | | - </span> |
| 41 | + <Popover open={isOpen} onOpenChange={setIsOpen}> |
| 42 | + <PopoverTrigger asChild> |
| 43 | + <span className="relative text-sm text-muted-foreground cursor-pointer hover:text-foreground transition-colors"> |
| 44 | + {currentMonth.toLocaleString(i18n.language, { year: "numeric", month: "long" })} |
| 45 | + </span> |
| 46 | + </PopoverTrigger> |
| 47 | + <PopoverContent className="p-0" align="start"> |
| 48 | + <CalendarPopover |
| 49 | + selectedYear={currentYear} |
| 50 | + data={statistics.activityStats} |
| 51 | + onYearChange={handleYearChange} |
| 52 | + onDateClick={handleDateClick} |
| 53 | + /> |
| 54 | + </PopoverContent> |
| 55 | + </Popover> |
22 | 56 | <div className="flex justify-end items-center shrink-0 gap-1"> |
23 | 57 | <button className="cursor-pointer hover:opacity-80 transition-opacity" onClick={handlePrevMonth} aria-label="Previous month"> |
24 | 58 | <ChevronLeftIcon className="w-5 h-auto shrink-0 opacity-40" /> |
|
0 commit comments