2022-12-26 23:09:52 +01:00
|
|
|
import { Menu, Dropdown, Button } from 'antd';
|
2023-01-16 07:31:36 +01:00
|
|
|
|
2022-05-13 23:44:16 +02:00
|
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
2022-09-07 09:00:28 +02:00
|
|
|
import { FC, useState } from 'react';
|
2022-06-25 08:27:17 +02:00
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
2022-10-04 06:06:46 +02:00
|
|
|
import dynamic from 'next/dynamic';
|
2023-03-13 07:07:02 +01:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-05-26 05:38:40 +02:00
|
|
|
import {
|
|
|
|
chatVisibleToggleAtom,
|
2022-10-11 01:26:09 +02:00
|
|
|
currentUserAtom,
|
2022-05-26 05:38:40 +02:00
|
|
|
appStateAtom,
|
|
|
|
} from '../../stores/ClientConfigStore';
|
2022-09-07 09:00:28 +02:00
|
|
|
import styles from './UserDropdown.module.scss';
|
2022-05-26 05:38:40 +02:00
|
|
|
import { AppStateOptions } from '../../stores/application-state';
|
2023-03-13 07:07:02 +01:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2022-10-04 06:06:46 +02:00
|
|
|
|
|
|
|
// Lazy loaded components
|
2023-01-16 07:31:36 +01:00
|
|
|
|
|
|
|
const CaretDownOutlined = dynamic(() => import('@ant-design/icons/CaretDownOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const EditOutlined = dynamic(() => import('@ant-design/icons/EditOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const LockOutlined = dynamic(() => import('@ant-design/icons/LockOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const MessageOutlined = dynamic(() => import('@ant-design/icons/MessageOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const UserOutlined = dynamic(() => import('@ant-design/icons/UserOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2023-01-11 01:39:12 +01:00
|
|
|
const Modal = dynamic(() => import('../../ui/Modal/Modal').then(mod => mod.Modal), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
2022-10-04 06:06:46 +02:00
|
|
|
|
2023-01-11 01:39:12 +01:00
|
|
|
const NameChangeModal = dynamic(
|
|
|
|
() => import('../../modals/NameChangeModal/NameChangeModal').then(mod => mod.NameChangeModal),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
},
|
2022-10-04 06:06:46 +02:00
|
|
|
);
|
|
|
|
|
2023-01-11 01:39:12 +01:00
|
|
|
const AuthModal = dynamic(
|
|
|
|
() => import('../../modals/AuthModal/AuthModal').then(mod => mod.AuthModal),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
},
|
2022-10-04 06:06:46 +02:00
|
|
|
);
|
2022-04-30 00:09:53 +02:00
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export type UserDropdownProps = {
|
2022-05-13 23:44:16 +02:00
|
|
|
username?: string;
|
2022-09-07 09:00:28 +02:00
|
|
|
};
|
2022-04-28 08:19:20 +02:00
|
|
|
|
2022-09-07 09:00:28 +02:00
|
|
|
export const UserDropdown: FC<UserDropdownProps> = ({ username: defaultUsername = undefined }) => {
|
2022-05-14 00:07:49 +02:00
|
|
|
const [showNameChangeModal, setShowNameChangeModal] = useState<boolean>(false);
|
2022-08-21 01:13:31 +02:00
|
|
|
const [showAuthModal, setShowAuthModal] = useState<boolean>(false);
|
2022-05-26 05:38:40 +02:00
|
|
|
const [chatToggleVisible, setChatToggleVisible] = useRecoilState(chatVisibleToggleAtom);
|
|
|
|
const appState = useRecoilValue<AppStateOptions>(appStateAtom);
|
2022-04-30 00:09:53 +02:00
|
|
|
|
|
|
|
const toggleChatVisibility = () => {
|
2022-06-25 08:27:17 +02:00
|
|
|
setChatToggleVisible(!chatToggleVisible);
|
2022-04-30 00:09:53 +02:00
|
|
|
};
|
|
|
|
|
2022-05-14 00:07:49 +02:00
|
|
|
const handleChangeName = () => {
|
|
|
|
setShowNameChangeModal(true);
|
|
|
|
};
|
|
|
|
|
2022-06-25 08:27:17 +02:00
|
|
|
// Register keyboard shortcut for the space bar to toggle playback
|
|
|
|
useHotkeys(
|
|
|
|
'c',
|
|
|
|
toggleChatVisibility,
|
|
|
|
{
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
},
|
|
|
|
[chatToggleVisible],
|
|
|
|
);
|
|
|
|
|
2022-10-11 01:56:02 +02:00
|
|
|
const currentUser = useRecoilValue(currentUserAtom);
|
2023-01-11 01:51:06 +01:00
|
|
|
if (!currentUser) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-11 01:56:02 +02:00
|
|
|
|
|
|
|
const { displayName } = currentUser;
|
|
|
|
const username = defaultUsername || displayName;
|
2022-04-30 00:09:53 +02:00
|
|
|
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-08-21 01:13:31 +02:00
|
|
|
<Menu.Item key="1" icon={<LockOutlined />} onClick={() => setShowAuthModal(true)}>
|
2022-05-22 14:20:11 +02:00
|
|
|
Authenticate
|
|
|
|
</Menu.Item>
|
2022-05-26 05:38:40 +02:00
|
|
|
{appState.chatAvailable && (
|
2023-01-24 04:16:52 +01:00
|
|
|
<Menu.Item
|
|
|
|
key="3"
|
|
|
|
icon={<MessageOutlined />}
|
|
|
|
onClick={() => toggleChatVisibility()}
|
|
|
|
aria-expanded={chatToggleVisible}
|
|
|
|
>
|
2022-12-16 04:23:43 +01:00
|
|
|
{chatToggleVisible ? 'Hide Chat' : 'Show Chat'}
|
2022-04-30 00:09:53 +02:00
|
|
|
</Menu.Item>
|
|
|
|
)}
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2023-03-13 07:07:02 +01:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
|
|
|
componentName="UserDropdown"
|
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
2023-04-24 19:58:57 +02:00
|
|
|
<div id="user-menu" className={styles.root}>
|
2023-03-13 07:07:02 +01:00
|
|
|
<Dropdown overlay={menu} trigger={['click']}>
|
|
|
|
<Button type="primary" icon={<UserOutlined className={styles.userIcon} />}>
|
|
|
|
<span className={styles.username}>{username}</span>
|
|
|
|
<CaretDownOutlined />
|
|
|
|
</Button>
|
|
|
|
</Dropdown>
|
|
|
|
<Modal
|
|
|
|
title="Change Chat Display Name"
|
|
|
|
open={showNameChangeModal}
|
|
|
|
handleCancel={() => setShowNameChangeModal(false)}
|
|
|
|
>
|
|
|
|
<NameChangeModal />
|
|
|
|
</Modal>
|
|
|
|
<Modal
|
|
|
|
title="Authenticate"
|
|
|
|
open={showAuthModal}
|
|
|
|
handleCancel={() => setShowAuthModal(false)}
|
|
|
|
>
|
|
|
|
<AuthModal />
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
2022-04-30 00:09:53 +02:00
|
|
|
);
|
2022-05-13 23:44:16 +02:00
|
|
|
};
|