Skip to content

Commit

Permalink
fix(CalendarsSpy): track calendar visibility change
Browse files Browse the repository at this point in the history
Fixes #246
  • Loading branch information
maxpatiiuk committed Jun 2, 2024
1 parent 4cdeb7f commit aba7fae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
33 changes: 20 additions & 13 deletions src/src/components/Contexts/useVisibilityChangeSpy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,26 @@ export function useVisibilityChangeSpy(
() =>
sideBar === undefined || calendars === undefined
? undefined
: listen(sideBar, 'click', ({ target }) => {
const element = target as HTMLInputElement;
if (element.tagName !== 'INPUT' || element.type !== 'checkbox')
return;
const data = parseCheckbox(calendars, element)?.[0];
if (data === undefined) return;
const [calendarId, checked] = data;
setVisibleCalendars(
checked
? visibleCalendarsRef.current?.filter((id) => id !== calendarId)
: [...(visibleCalendarsRef.current ?? []), calendarId],
);
}),
: listen(
sideBar,
'change',
({ target }) => {
const element = target as HTMLInputElement;
if (element.tagName !== 'INPUT' || element.type !== 'checkbox')
return;
const data = parseCheckbox(calendars, element);
if (data === undefined) return;
const [calendarId, isNowChecked] = data;
setVisibleCalendars(
isNowChecked
? [...(visibleCalendarsRef.current ?? []), calendarId]
: visibleCalendarsRef.current?.filter(
(id) => id !== calendarId,
),
);
},
{ passive: true },
),
[sideBar, calendars, setVisibleCalendars],
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/src/components/Molecules/KeyboardShortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function SetKeyboardShortcut({
event.preventDefault();
event.stopPropagation();
},
true,
{ capture: true },
);
}
return undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export function listen<EVENT_NAME extends keyof GlobalEventHandlersEventMap>(
element: EventTarget,
eventName: EVENT_NAME,
callback: (event: GlobalEventHandlersEventMap[EVENT_NAME]) => void,
catchAll = false,
options: AddEventListenerOptions,
): () => void {
element.addEventListener(
eventName,
callback as (event: Event) => void,
catchAll,
options,
);
return (): void =>
element.removeEventListener(
eventName,
callback as (event: Event) => void,
catchAll,
options,
);
}

0 comments on commit aba7fae

Please sign in to comment.