From 04ff9a0016ebccb1ddc427bb68e35fa159614249 Mon Sep 17 00:00:00 2001 From: cak Date: Thu, 5 Sep 2024 05:20:23 -0400 Subject: [PATCH] small performance optimizations --- src/components/event/Event.tsx | 6 +- src/components/schedule/EventCell.tsx | 50 +++++----- src/components/schedule/EventsTable.tsx | 117 +++++++++++++++--------- 3 files changed, 105 insertions(+), 68 deletions(-) diff --git a/src/components/event/Event.tsx b/src/components/event/Event.tsx index fccae46..6841b73 100644 --- a/src/components/event/Event.tsx +++ b/src/components/event/Event.tsx @@ -117,7 +117,11 @@ function Event({
{eventTags - ?.sort((a, b) => (a.sort_order > b.sort_order ? 1 : -1)) + .sort((a, b) => + a.sort_order !== b.sort_order + ? a.sort_order - b.sort_order + : a.label.localeCompare(b.label) + ) ?.map((tag) => (
{ + router.push(eventUrl); + }; + return ( router.push(eventUrl)} + onClick={handleRowClick} >
@@ -32,32 +36,32 @@ export default function EventCell({

{eventTimeTable(event.end)}

- + +

+ {event.title} +

+

{event.speakers}

+

{event.location}

-
- {event.tags - ?.sort((a, b) => (a.sort_order > b.sort_order ? 1 : -1)) - ?.map((tag) => ( -
- -

{tag.label}

-
- ))} -
+ {event.tags + ?.sort((a, b) => + a.sort_order !== b.sort_order + ? a.sort_order - b.sort_order + : a.label.localeCompare(b.label) + ) + .map((tag) => ( +
+ +

{tag.label}

+
+ ))}
); diff --git a/src/components/schedule/EventsTable.tsx b/src/components/schedule/EventsTable.tsx index c17c6d4..9271dcf 100644 --- a/src/components/schedule/EventsTable.tsx +++ b/src/components/schedule/EventsTable.tsx @@ -7,9 +7,27 @@ import { TableRow, } from "../ui/table"; import EventCell from "./EventCell"; -import React, { useEffect, useRef, useState } from "react"; +import React, { useEffect, useRef, useState, useMemo } from "react"; import { Button } from "../ui/button"; +const DayButton = ({ + day, + isActive, + onClick, +}: { + day: string; + isActive: boolean; + onClick: () => void; +}) => ( + +); + export default function EventsTable({ dateGroup, conf, @@ -22,6 +40,17 @@ export default function EventsTable({ const dayRefs = useRef(new Map()); const [activeDay, setActiveDay] = useState(null); + const filteredDateGroup = useMemo(() => { + return Array.from(dateGroup) + .map(([day, events]) => { + const filteredEvents = events.filter( + (e) => selectedTag === 0 || e.tags?.some((t) => t.id === selectedTag) + ); + return { day, filteredEvents }; + }) + .filter(({ filteredEvents }) => filteredEvents.length > 0); + }, [dateGroup, selectedTag]); + useEffect(() => { const observer = new IntersectionObserver( (entries) => { @@ -43,48 +72,40 @@ export default function EventsTable({ return () => { observer.disconnect(); }; - }, []); + }, [filteredDateGroup]); + const scrollToDay = (day: string) => { const dayRow = dayRefs.current.get(day); - if (dayRow) { - const headerOffset = 120; - const elementPosition = dayRow.getBoundingClientRect().top; - const offsetPosition = elementPosition + window.scrollY - headerOffset; - - window.scrollTo({ - top: offsetPosition, - behavior: "smooth", - }); - } - }; + if (!dayRow) return; // Ensure dayRow exists - const filteredDateGroup = Array.from(dateGroup) - .map(([day, events]) => { - const filteredEvents = events.filter( - (e) => selectedTag === 0 || e.tags?.some((t) => t.id == selectedTag) - ); + const headerOffset = 120; + const elementPosition = dayRow.getBoundingClientRect().top; + const offsetPosition = elementPosition + window.scrollY - headerOffset; - return { day, filteredEvents }; - }) - .filter(({ filteredEvents }) => filteredEvents.length > 0); + window.scrollTo({ + top: offsetPosition, + behavior: "smooth", + }); + }; return (
+ {/* Day buttons */}
{filteredDateGroup.map(({ day }) => ( - + /> ))}
+ + {/* Event day sections */} {filteredDateGroup.map(({ day, filteredEvents }) => (
- - - - - Start - End - Event - Location - Tags - - - - {filteredEvents.map((htEvent) => ( - - ))} - -
+ + {/* Event table */} + {filteredEvents.length > 0 && ( + + + + + Start + End + Event + Location + Tags + + + + {filteredEvents.map((htEvent) => ( + + ))} + +
+ )}
))}