|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import useToggle from "../../hooks/useToggle"; |
| 3 | +import { |
| 4 | + StyledCalendarDay, |
| 5 | + StyledCalendarDays, |
| 6 | + StyledCalendarDaysFrame, |
| 7 | + StyledCalendarLayout, |
| 8 | + StyledCalendarNavigation, |
| 9 | + StyledCalendarNavigationSection, |
| 10 | + StyledNavigationButton, |
| 11 | +} from "./styles"; |
| 12 | +import useSystemSettings from "../../stores/systemSettingsStore"; |
| 13 | + |
| 14 | +interface CalendarProps {} |
| 15 | + |
| 16 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 17 | +function Calendar(_props: CalendarProps) { |
| 18 | + const sidebarToggle = useToggle(); |
| 19 | + const calendarRef = useRef<HTMLDivElement>(null); |
| 20 | + const [mainColor, accentColor, fontColor] = useSystemSettings((s) => [ |
| 21 | + s.mainColor, |
| 22 | + s.accentColor, |
| 23 | + s.fontColor, |
| 24 | + ]); |
| 25 | + |
| 26 | + const now = new Date(); |
| 27 | + const [year, setYear] = useState(now.getFullYear()); |
| 28 | + const [month, setMonth] = useState(now.getMonth()); |
| 29 | + const prevYear = () => setYear((x) => x - 1); |
| 30 | + const nextYear = () => setYear((x) => x + 1); |
| 31 | + const wrapMonth = (m: number) => ((m % 12) + 12) % 12; |
| 32 | + const nextMonth = () => setMonth((x) => wrapMonth(x + 1)); |
| 33 | + const prevMonth = () => setMonth((x) => wrapMonth(x - 1)); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + console.log("sidebar toggle", sidebarToggle.state); |
| 37 | + }, [sidebarToggle.state]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (!calendarRef.current) return; |
| 41 | + const el = calendarRef.current; |
| 42 | + |
| 43 | + function onResize(entries: ResizeObserverEntry[]) { |
| 44 | + const entry = entries[0]; |
| 45 | + if (!entry) return; |
| 46 | + |
| 47 | + // Most reliable in modern browsers |
| 48 | + const width = entry.contentRect.width; |
| 49 | + |
| 50 | + const wide = width >= 600; |
| 51 | + sidebarToggle.setState(wide); |
| 52 | + } |
| 53 | + |
| 54 | + const observer = new ResizeObserver(onResize); |
| 55 | + |
| 56 | + observer.observe(el); |
| 57 | + |
| 58 | + return () => { |
| 59 | + if (el) observer.unobserve(el); |
| 60 | + observer.disconnect(); |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + const dayName = ( |
| 65 | + dayIndex: number, |
| 66 | + locale = "en-US", |
| 67 | + format: "long" | "short" = "short", |
| 68 | + ) => |
| 69 | + new Intl.DateTimeFormat(locale, { weekday: format }).format( |
| 70 | + new Date(2021, 0, dayIndex + 3), |
| 71 | + ); |
| 72 | + const monthName = ( |
| 73 | + monthIndex: number, |
| 74 | + locale = "en-US", |
| 75 | + format: "long" | "short" = "long", |
| 76 | + ) => |
| 77 | + new Intl.DateTimeFormat(locale, { month: format }).format( |
| 78 | + new Date(2021, monthIndex, 1), |
| 79 | + ); |
| 80 | + |
| 81 | + const buildGrid = () => { |
| 82 | + const daysInMonth = new Date(year, month + 1, 0).getDate(); |
| 83 | + const firstDayOfMonth = new Date(year, month, 0).getDay(); // 0 - 6 |
| 84 | + const calendarCellCount = 7 * 6; // 7 cols (1 per day), 6 rows |
| 85 | + |
| 86 | + const elements: React.ReactNode[] = []; |
| 87 | + |
| 88 | + for (let i = 0; i < calendarCellCount; i++) { |
| 89 | + const dayNumber = i - firstDayOfMonth + 1; |
| 90 | + const cellDayName = dayName(i + 1); |
| 91 | + |
| 92 | + if (dayNumber < 1 || dayNumber > daysInMonth) { |
| 93 | + // Empty cell |
| 94 | + elements.push( |
| 95 | + <StyledCalendarDay |
| 96 | + backgroundColor={mainColor} |
| 97 | + color={fontColor} |
| 98 | + key={i} |
| 99 | + currentMonth={false} |
| 100 | + > |
| 101 | + {cellDayName} |
| 102 | + </StyledCalendarDay>, |
| 103 | + ); |
| 104 | + } else { |
| 105 | + // Valid day |
| 106 | + elements.push( |
| 107 | + <StyledCalendarDay |
| 108 | + backgroundColor={mainColor} |
| 109 | + color={fontColor} |
| 110 | + key={i} |
| 111 | + currentMonth |
| 112 | + > |
| 113 | + <span>{cellDayName}</span> |
| 114 | + <span>{dayNumber}</span> |
| 115 | + </StyledCalendarDay>, |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + return elements; |
| 120 | + }; |
| 121 | + |
| 122 | + return ( |
| 123 | + <StyledCalendarLayout |
| 124 | + ref={calendarRef} |
| 125 | + sidebarOpen={sidebarToggle.state} |
| 126 | + className="calendar" |
| 127 | + > |
| 128 | + <StyledCalendarNavigation className="calendar__nav"> |
| 129 | + <StyledCalendarNavigationSection> |
| 130 | + <StyledNavigationButton |
| 131 | + className="calendar__nav-button" |
| 132 | + onClick={prevMonth} |
| 133 | + >{`<`}</StyledNavigationButton> |
| 134 | + <StyledNavigationButton className="calendar__nav-button"> |
| 135 | + {monthName(month)} |
| 136 | + </StyledNavigationButton> |
| 137 | + <StyledNavigationButton |
| 138 | + className="calendar__nav-button" |
| 139 | + onClick={nextMonth} |
| 140 | + >{`>`}</StyledNavigationButton> |
| 141 | + </StyledCalendarNavigationSection> |
| 142 | + |
| 143 | + <StyledCalendarNavigationSection> |
| 144 | + <StyledNavigationButton |
| 145 | + className="calendar__nav-button" |
| 146 | + onClick={prevYear} |
| 147 | + >{`<`}</StyledNavigationButton> |
| 148 | + <StyledNavigationButton className="calendar__nav-button"> |
| 149 | + {year} |
| 150 | + </StyledNavigationButton> |
| 151 | + <StyledNavigationButton |
| 152 | + className="calendar__nav-button" |
| 153 | + onClick={nextYear} |
| 154 | + >{`>`}</StyledNavigationButton> |
| 155 | + </StyledCalendarNavigationSection> |
| 156 | + </StyledCalendarNavigation> |
| 157 | + <StyledCalendarDaysFrame frameColor={accentColor}> |
| 158 | + <StyledCalendarDays |
| 159 | + borderColor={accentColor} |
| 160 | + className="calendar__days" |
| 161 | + > |
| 162 | + {buildGrid()} |
| 163 | + </StyledCalendarDays> |
| 164 | + </StyledCalendarDaysFrame> |
| 165 | + </StyledCalendarLayout> |
| 166 | + ); |
| 167 | +} |
| 168 | + |
| 169 | +export default Calendar; |
0 commit comments