import { Box, config, Icon, Icons, IconSrc, 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 { ThreadNotificationMode } from '../utils/threadNotifications'; import { useSetThreadNotificationMode } from '../hooks/useThreadNotifications'; import { AsyncStatus } from '../hooks/useAsyncCallback'; export const getThreadNotificationModeIcon = (mode?: ThreadNotificationMode): IconSrc => { if (mode === ThreadNotificationMode.Mute) return Icons.BellMute; if (mode === ThreadNotificationMode.MentionsOnly) return Icons.BellPing; if (mode === ThreadNotificationMode.All) return Icons.BellRing; return Icons.Bell; }; const useThreadNotificationModes = (): ThreadNotificationMode[] => useMemo( () => [ ThreadNotificationMode.Default, ThreadNotificationMode.All, ThreadNotificationMode.MentionsOnly, ThreadNotificationMode.Mute, ], [], ); const useThreadNotificationModeStr = (): Record => useMemo( () => ({ [ThreadNotificationMode.Default]: 'Default (participating)', [ThreadNotificationMode.All]: 'All replies', [ThreadNotificationMode.MentionsOnly]: 'Mentions only', [ThreadNotificationMode.Mute]: 'Mute', }), [], ); type ThreadNotificationModeSwitcherProps = { roomId: string; threadId: string; value?: ThreadNotificationMode; children: ( handleOpen: MouseEventHandler, opened: boolean, changing: boolean, ) => ReactNode; }; export function ThreadNotificationModeSwitcher({ roomId, threadId, value = ThreadNotificationMode.Default, children, }: ThreadNotificationModeSwitcherProps) { const modes = useThreadNotificationModes(); const modeToStr = useThreadNotificationModeStr(); const { modeState, setMode } = useSetThreadNotificationMode(roomId, threadId); 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: ThreadNotificationMode) => { if (changing) return; setMode(mode); 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)} ); }