From 5b4736aedab94458ca73a60b2a0ac5c4a82b3648 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Sat, 7 Dec 2024 22:58:49 -0800 Subject: [PATCH] feat: prevent rerender of SectionTableBody when hovered --- .../src/components/Calendar/CalendarRoot.tsx | 20 +++---- .../SectionTable/CourseInfoButton.tsx | 6 +-- .../SectionTable/SectionTableBody.tsx | 54 +++++++++++-------- .../src/stores/calendarizeHelpers.ts | 12 ++--- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/apps/antalmanac/src/components/Calendar/CalendarRoot.tsx b/apps/antalmanac/src/components/Calendar/CalendarRoot.tsx index 66d523d92..479b7c045 100644 --- a/apps/antalmanac/src/components/Calendar/CalendarRoot.tsx +++ b/apps/antalmanac/src/components/Calendar/CalendarRoot.tsx @@ -5,6 +5,7 @@ import { Box, ClickAwayListener, Popper } from '@material-ui/core'; import moment from 'moment'; import { memo, SyntheticEvent, useCallback, useEffect, useMemo, useState } from 'react'; import { Calendar, DateLocalizer, momentLocalizer, Views } from 'react-big-calendar'; +import { shallow } from 'zustand/shallow'; import CalendarToolbar from './CalendarToolbar'; import CourseCalendarEvent, { CalendarEvent, CourseEvent } from './CourseCalendarEvent'; @@ -29,10 +30,10 @@ export const ScheduleCalendar = memo(() => { const [scheduleNames, setScheduleNames] = useState(() => AppStore.getScheduleNames()); const { isMilitaryTime } = useTimeFormatStore(); - const [hoveredCalendarizedCourses, hoveredCalendarizedFinal] = useHoveredStore((store) => [ - store.hoveredCalendarizedCourses, - store.hoveredCalendarizedFinal, - ]); + const [hoveredCalendarizedCourses, hoveredCalendarizedFinal] = useHoveredStore( + (state) => [state.hoveredCalendarizedCourses, state.hoveredCalendarizedFinal], + shallow + ); const getEventsForCalendar = useCallback((): CalendarEvent[] => { if (showFinalsSchedule) @@ -49,6 +50,8 @@ export const ScheduleCalendar = memo(() => { showFinalsSchedule, ]); + const events = useMemo(() => getEventsForCalendar(), [getEventsForCalendar]); + const handleClosePopover = useCallback(() => { setAnchorEl(null); }, []); @@ -74,9 +77,9 @@ export const ScheduleCalendar = memo(() => { * @returns A date with the earliest time or 7AM */ const getStartTime = useCallback(() => { - const eventStartHours = getEventsForCalendar().map((event) => event.start.getHours()); + const eventStartHours = events.map((event) => event.start.getHours()); return new Date(2018, 0, 1, Math.min(7, Math.min(...eventStartHours))); - }, [getEventsForCalendar]); + }, [events]); const eventStyleGetter = useCallback((event: CalendarEvent) => { const style = { @@ -113,7 +116,6 @@ export const ScheduleCalendar = memo(() => { return Math.abs(bgBrightness - textBrightness) > minBrightnessDiff; }; - const events = useMemo(() => getEventsForCalendar(), [getEventsForCalendar]); const hasWeekendCourse = events.some((event) => event.start.getDay() === 0 || event.start.getDay() === 6); const calendarTimeFormat = isMilitaryTime ? 'HH:mm' : 'h:mm A'; @@ -124,8 +126,8 @@ export const ScheduleCalendar = memo(() => { const finalsDate = hoveredCalendarizedFinal ? getFinalsStartDateForTerm(hoveredCalendarizedFinal.term) : onlyCourseEvents.length > 0 - ? getFinalsStartDateForTerm(onlyCourseEvents[0].term) - : getDefaultFinalsStartDate(); + ? getFinalsStartDateForTerm(onlyCourseEvents[0].term) + : getDefaultFinalsStartDate(); const finalsDateFormat = finalsDate ? 'ddd MM/DD' : 'ddd'; const date = showFinalsSchedule && finalsDate ? finalsDate : new Date(2018, 0, 1); diff --git a/apps/antalmanac/src/components/RightPane/SectionTable/CourseInfoButton.tsx b/apps/antalmanac/src/components/RightPane/SectionTable/CourseInfoButton.tsx index a2372e3aa..0a9e2d9a2 100644 --- a/apps/antalmanac/src/components/RightPane/SectionTable/CourseInfoButton.tsx +++ b/apps/antalmanac/src/components/RightPane/SectionTable/CourseInfoButton.tsx @@ -72,11 +72,7 @@ function CourseInfoButton({ isMobileScreen || (scheduleManagementWidth && scheduleManagementWidth < theme.breakpoints.values.xs); return ( -
+