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]} ))} } /> ); }