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

90 lines
2.4 KiB
TypeScript
Raw Normal View History

import { Menu, Dropdown, Button, Space } from 'antd';
2022-05-22 14:20:11 +02:00
import {
CaretDownOutlined,
EditOutlined,
LockOutlined,
MessageOutlined,
UserOutlined,
} from '@ant-design/icons';
import { useRecoilState, useRecoilValue } from 'recoil';
2022-05-14 00:07:49 +02:00
import { useState } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
2022-05-14 00:07:49 +02:00
import Modal from '../../ui/Modal/Modal';
import {
chatVisibleToggleAtom,
chatDisplayNameAtom,
appStateAtom,
} from '../../stores/ClientConfigStore';
import s from './UserDropdown.module.scss';
2022-05-14 00:07:49 +02:00
import NameChangeModal from '../../modals/NameChangeModal';
import { AppStateOptions } from '../../stores/application-state';
interface Props {
username?: string;
}
export default function UserDropdown({ username: defaultUsername }: Props) {
const username = defaultUsername || useRecoilValue(chatDisplayNameAtom);
2022-05-14 00:07:49 +02:00
const [showNameChangeModal, setShowNameChangeModal] = useState<boolean>(false);
const [chatToggleVisible, setChatToggleVisible] = useRecoilState(chatVisibleToggleAtom);
const appState = useRecoilValue<AppStateOptions>(appStateAtom);
const toggleChatVisibility = () => {
setChatToggleVisible(!chatToggleVisible);
};
2022-05-14 00:07:49 +02: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 14:20:11 +02:00
<Menu.Item key="0" icon={<EditOutlined />} onClick={() => handleChangeName()}>
2022-05-14 00:07:49 +02:00
Change name
</Menu.Item>
2022-05-22 14:20:11 +02:00
<Menu.Item key="1" icon={<LockOutlined />}>
Authenticate
</Menu.Item>
{appState.chatAvailable && (
2022-05-22 14:20:11 +02: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 00:07:49 +02:00
<Modal
title="Change Chat Display Name"
visible={showNameChangeModal}
handleCancel={() => setShowNameChangeModal(false)}
>
<NameChangeModal />
</Modal>
</div>
);
}
UserDropdown.defaultProps = {
username: undefined,
};