owncast/web/components/common/UserDropdown/UserDropdown.tsx

99 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Menu, Dropdown, Button, Space } from 'antd';
2022-05-22 15:20:11 +03:00
import {
CaretDownOutlined,
EditOutlined,
LockOutlined,
MessageOutlined,
UserOutlined,
} from '@ant-design/icons';
import { useRecoilState, useRecoilValue } from 'recoil';
2022-05-14 01:07:49 +03:00
import { useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
2022-05-14 01:07:49 +03:00
import Modal from '../../ui/Modal/Modal';
import {
chatVisibleToggleAtom,
chatDisplayNameAtom,
appStateAtom,
} from '../../stores/ClientConfigStore';
import s from './UserDropdown.module.scss';
2022-05-14 01:07:49 +03:00
import NameChangeModal from '../../modals/NameChangeModal';
import { AppStateOptions } from '../../stores/application-state';
import AuthModal from '../../modals/AuthModal/AuthModal';
interface Props {
username?: string;
}
export default function UserDropdown({ username: defaultUsername }: Props) {
const username = defaultUsername || useRecoilValue(chatDisplayNameAtom);
2022-05-14 01:07:49 +03:00
const [showNameChangeModal, setShowNameChangeModal] = useState<boolean>(false);
const [showAuthModal, setShowAuthModal] = useState<boolean>(false);
const [chatToggleVisible, setChatToggleVisible] = useRecoilState(chatVisibleToggleAtom);
const appState = useRecoilValue<AppStateOptions>(appStateAtom);
const toggleChatVisibility = () => {
setChatToggleVisible(!chatToggleVisible);
};
2022-05-14 01:07:49 +03:00
const handleChangeName = () => {
setShowNameChangeModal(true);
};
// Register keyboard shortcut for the space bar to toggle playback
useHotkeys(
'c',
toggleChatVisibility,
{
enableOnContentEditable: false,
},
[chatToggleVisible],
);
const menu = (
<Menu>
2022-05-22 15:20:11 +03:00
<Menu.Item key="0" icon={<EditOutlined />} onClick={() => handleChangeName()}>
2022-05-14 01:07:49 +03:00
Change name
</Menu.Item>
<Menu.Item key="1" icon={<LockOutlined />} onClick={() => setShowAuthModal(true)}>
2022-05-22 15:20:11 +03:00
Authenticate
</Menu.Item>
{appState.chatAvailable && (
2022-05-22 15:20:11 +03:00
<Menu.Item key="3" icon={<MessageOutlined />} onClick={() => toggleChatVisibility()}>
Toggle chat
</Menu.Item>
)}
</Menu>
);
return (
<div className={`${s.root}`}>
<Dropdown overlay={menu} trigger={['click']}>
<Button icon={<UserOutlined style={{ marginRight: '.5rem' }} />}>
<Space>
{username}
<CaretDownOutlined />
</Space>
</Button>
</Dropdown>
2022-05-14 01:07:49 +03:00
<Modal
title="Change Chat Display Name"
visible={showNameChangeModal}
handleCancel={() => setShowNameChangeModal(false)}
>
<NameChangeModal />
</Modal>
<Modal
title="Authenticate"
visible={showAuthModal}
handleCancel={() => setShowAuthModal(false)}
>
<AuthModal />
</Modal>
</div>
);
}
UserDropdown.defaultProps = {
username: undefined,
};