From bda619800c260aa8a655f1cc4d1ef321ede82d5f Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:30:58 +0530 Subject: [PATCH] add notification mode switcher component --- .../NotificationModeSwitcher.tsx | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/app/features/settings/notifications/NotificationModeSwitcher.tsx diff --git a/src/app/features/settings/notifications/NotificationModeSwitcher.tsx b/src/app/features/settings/notifications/NotificationModeSwitcher.tsx new file mode 100644 index 00000000..fe008390 --- /dev/null +++ b/src/app/features/settings/notifications/NotificationModeSwitcher.tsx @@ -0,0 +1,117 @@ +import { + Box, + Button, + config, + Icon, + Icons, + Menu, + MenuItem, + PopOut, + RectCords, + Spinner, + Text, +} from 'folds'; +import { IPushRule } from 'matrix-js-sdk'; +import React, { MouseEventHandler, useMemo, useState } from 'react'; +import FocusTrap from 'focus-trap-react'; +import { NotificationMode, useNotificationActionsMode } from '../../../hooks/useNotificationMode'; +import { stopPropagation } from '../../../utils/keyboard'; +import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; + +export const useNotificationModes = (): NotificationMode[] => + useMemo(() => [NotificationMode.NotifyLoud, NotificationMode.Notify, NotificationMode.OFF], []); + +const useNotificationModeStr = (): Record => + useMemo( + () => ({ + [NotificationMode.OFF]: 'Disable', + [NotificationMode.Notify]: 'Notify Silent', + [NotificationMode.NotifyLoud]: 'Notify Loud', + }), + [] + ); + +type NotificationModeSwitcherProps = { + pushRule: IPushRule; + onChange: (mode: NotificationMode) => Promise; +}; +export function NotificationModeSwitcher({ pushRule, onChange }: NotificationModeSwitcherProps) { + const modes = useNotificationModes(); + const modeToStr = useNotificationModeStr(); + const selectedMode = useNotificationActionsMode(pushRule.actions); + const [changeState, change] = useAsyncCallback(onChange); + const changing = changeState.status === AsyncStatus.Loading; + + const [menuCords, setMenuCords] = useState(); + + const handleMenu: MouseEventHandler = (evt) => { + setMenuCords(evt.currentTarget.getBoundingClientRect()); + }; + + const handleSelect = (mode: NotificationMode) => { + setMenuCords(undefined); + change(mode); + }; + + return ( + <> + + setMenuCords(undefined), + clickOutsideDeactivates: true, + isKeyForward: (evt: KeyboardEvent) => + evt.key === 'ArrowDown' || evt.key === 'ArrowRight', + isKeyBackward: (evt: KeyboardEvent) => + evt.key === 'ArrowUp' || evt.key === 'ArrowLeft', + escapeDeactivates: stopPropagation, + }} + > + + + {modes.map((mode) => ( + handleSelect(mode)} + > + + {modeToStr[mode]} + + + ))} + + + + } + /> + + ); +}