import { Box, config, Icon, Menu, MenuItem, PopOut, RectCords, Text } from 'folds'; import React, { MouseEventHandler, ReactNode, useMemo, useState } from 'react'; import FocusTrap from 'focus-trap-react'; import { stopPropagation } from '../utils/keyboard'; import { getRoomNotificationModeIcon, RoomNotificationMode, useSetRoomNotificationPreference, } from '../hooks/useRoomsNotificationPreferences'; import { AsyncStatus } from '../hooks/useAsyncCallback'; const useRoomNotificationModes = (): RoomNotificationMode[] => useMemo( () => [ RoomNotificationMode.Unset, RoomNotificationMode.AllMessages, RoomNotificationMode.SpecialMessages, RoomNotificationMode.Mute, ], [] ); const useRoomNotificationModeStr = (): Record => useMemo( () => ({ [RoomNotificationMode.Unset]: 'Default', [RoomNotificationMode.AllMessages]: 'All Messages', [RoomNotificationMode.SpecialMessages]: 'Mention & Keywords', [RoomNotificationMode.Mute]: 'Mute', }), [] ); type NotificationModeSwitcherProps = { roomId: string; value?: RoomNotificationMode; children: ( handleOpen: MouseEventHandler, opened: boolean, changing: boolean ) => ReactNode; }; export function RoomNotificationModeSwitcher({ roomId, value = RoomNotificationMode.Unset, children, }: NotificationModeSwitcherProps) { const modes = useRoomNotificationModes(); const modeToStr = useRoomNotificationModeStr(); const { modeState, setMode } = useSetRoomNotificationPreference(roomId); const changing = modeState.status === AsyncStatus.Loading; const [menuCords, setMenuCords] = useState(); const handleOpenMenu: MouseEventHandler = (evt) => { setMenuCords(evt.currentTarget.getBoundingClientRect()); }; const handleClose = () => { setMenuCords(undefined); }; const handleSelect = (mode: RoomNotificationMode) => { if (changing) return; setMode(mode, value); handleClose(); }; return ( evt.key === 'ArrowDown' || evt.key === 'ArrowRight', isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp' || evt.key === 'ArrowLeft', escapeDeactivates: stopPropagation, }} > {modes.map((mode) => ( handleSelect(mode)} before={ } > {mode === value ? {modeToStr[mode]} : modeToStr[mode]} ))} } > {children(handleOpenMenu, !!menuCords, changing)} ); }