import { h, createContext } from '/js/web_modules/preact.js'; import htm from '/js/web_modules/htm.js'; import { useState, useEffect, useRef } from '/js/web_modules/preact/hooks.js'; import UsernameForm from './username.js'; import { ChatIcon, UserIcon, CaretDownIcon } from '../icons/index.js'; const html = htm.bind(h); const moderatorFlag = html` `; const Context = createContext(); export const ChatMenu = (props) => { const { username, isModerator, chatDisabled, noVideoContent, handleChatPanelToggle, onUsernameChange, onFocus, onBlur, } = props; const [chatMenuOpen, setChatMenuOpen] = useState(false); const [view, setView] = useState('main'); const chatMenuRef = useRef(undefined); closeOnOutsideClick(chatMenuRef, setChatMenuOpen); useEffect(() => { if (chatMenuOpen) setView('main'); }, [chatMenuOpen]); return html` <${Context.Provider} value=${props}>
${ chatMenuOpen && html`
${view === 'main' && html``} ${view != 'main' && html`<${SubMenuView} view=${view} setView=${setView} />`}
` }
`; }; const SubMenuView = ({ view, setView }) => { return html`
${view === 'change_username' && html`<${ChangeUsernameView} />`}
`; }; function closeOnOutsideClick(ref, setter) { useEffect(() => { function handleClickOutside(event) { if (ref.current && !ref.current.contains(event.target)) { setter(undefined); } } document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [ref]); }