Skip to content

Commit

Permalink
make eventintersection a bit safer
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Jul 29, 2023
1 parent cfcc130 commit 96fc7a8
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/Schedulely/src/providers/EventIntersectionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const EventIntersectionProvider = ({

const [eventVisibility, setEventVisibility] = useState<
Record<string, InternalCalendarEvent>
>({});
>(Object.assign({}, ...eventsInWeek.map((x) => ({ [x.id]: x }))));

const getEventsOnDate = useCallback(
(date: Date) =>
Expand All @@ -61,8 +61,6 @@ export const EventIntersectionProvider = ({
const checkIntersection: IntersectionObserverCallback = useCallback(
(entries) =>
entries.map((x) => {
var eventId = x.target.attributes.getNamedItem('data-eventid')?.value;

const currentStyle =
x.target
.getAttribute('style')
Expand All @@ -76,22 +74,23 @@ export const EventIntersectionProvider = ({
x.target.setAttribute('style', currentStyle.join(';'));
}

const matchingEvent = eventsInWeek.find((x) => x.id === eventId);
if (matchingEvent === undefined) return;

// this controls the event data that is sent back to the DayComponent for event visibility
setEventVisibility((current) => {
current[matchingEvent.id] = matchingEvent;
current[matchingEvent.id].visible = x.isIntersecting;
var eventId = x.target.attributes.getNamedItem('data-eventid')?.value;
if (!eventId) return { ...current };

if (!current[eventId]) {
const matchingEvent = eventsInWeek.find((x) => x.id === eventId)!;
current[eventId] = matchingEvent;
}
current[eventId].visible = x.isIntersecting;
return { ...current };
});
}),
[eventsInWeek]
);

useEffect(() => {
// clear visibility dictionary to avoid stale entries building up
setEventVisibility({});

observerRef.current = new IntersectionObserver(checkIntersection, {
root: parentContainerRef,
rootMargin: '0px 0px -15% 0px',
Expand Down

0 comments on commit 96fc7a8

Please sign in to comment.