2022-12-27 01:09:52 +03:00
|
|
|
import { Menu, Dropdown, Button } from 'antd';
|
2023-05-23 04:56:44 +03:00
|
|
|
import classnames from 'classnames';
|
2023-01-16 09:31:36 +03:00
|
|
|
|
2022-05-14 00:44:16 +03:00
|
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FC, useState } from 'react';
|
2022-06-25 09:27:17 +03:00
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
2022-10-04 07:06:46 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2023-03-13 09:07:02 +03:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-05-26 06:38:40 +03:00
|
|
|
import {
|
2023-06-26 19:00:27 +03:00
|
|
|
ChatState,
|
|
|
|
chatStateAtom,
|
2022-10-11 02:26:09 +03:00
|
|
|
currentUserAtom,
|
2022-05-26 06:38:40 +03:00
|
|
|
appStateAtom,
|
|
|
|
} from '../../stores/ClientConfigStore';
|
2022-09-07 10:00:28 +03:00
|
|
|
import styles from './UserDropdown.module.scss';
|
2022-05-26 06:38:40 +03:00
|
|
|
import { AppStateOptions } from '../../stores/application-state';
|
2023-03-13 09:07:02 +03:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2022-10-04 07:06:46 +03:00
|
|
|
|
|
|
|
// Lazy loaded components
|
2023-01-16 09:31:36 +03: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,
|
|
|
|
});
|
|
|
|
|
2023-06-26 19:00:27 +03:00
|
|
|
const ShrinkOutlined = dynamic(() => import('@ant-design/icons/ShrinkOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const ExpandAltOutlined = dynamic(() => import('@ant-design/icons/ExpandAltOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
const MessageOutlined = dynamic(() => import('@ant-design/icons/MessageOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const UserOutlined = dynamic(() => import('@ant-design/icons/UserOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2023-01-11 03:39:12 +03:00
|
|
|
const Modal = dynamic(() => import('../../ui/Modal/Modal').then(mod => mod.Modal), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
2022-10-04 07:06:46 +03:00
|
|
|
|
2023-01-11 03:39:12 +03:00
|
|
|
const NameChangeModal = dynamic(
|
|
|
|
() => import('../../modals/NameChangeModal/NameChangeModal').then(mod => mod.NameChangeModal),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
},
|
2022-10-04 07:06:46 +03:00
|
|
|
);
|
|
|
|
|
2023-01-11 03:39:12 +03:00
|
|
|
const AuthModal = dynamic(
|
|
|
|
() => import('../../modals/AuthModal/AuthModal').then(mod => mod.AuthModal),
|
|
|
|
{
|
|
|
|
ssr: false,
|
|
|
|
},
|
2022-10-04 07:06:46 +03:00
|
|
|
);
|
2022-04-30 01:09:53 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type UserDropdownProps = {
|
2023-05-23 04:56:44 +03:00
|
|
|
id: string;
|
2022-05-14 00:44:16 +03:00
|
|
|
username?: string;
|
2023-05-23 04:56:44 +03:00
|
|
|
hideTitleOnMobile?: boolean;
|
|
|
|
showToggleChatOption?: boolean;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2023-05-23 04:56:44 +03:00
|
|
|
export const UserDropdown: FC<UserDropdownProps> = ({
|
|
|
|
id,
|
|
|
|
username: defaultUsername = undefined,
|
|
|
|
hideTitleOnMobile = false,
|
|
|
|
showToggleChatOption: showHideChatOption = true,
|
|
|
|
}) => {
|
2022-05-14 01:07:49 +03:00
|
|
|
const [showNameChangeModal, setShowNameChangeModal] = useState<boolean>(false);
|
2022-08-21 02:13:31 +03:00
|
|
|
const [showAuthModal, setShowAuthModal] = useState<boolean>(false);
|
2023-06-26 19:00:27 +03:00
|
|
|
const [chatState, setChatState] = useRecoilState(chatStateAtom);
|
|
|
|
const [popupWindow, setPopupWindow] = useState<Window>(null);
|
2022-05-26 06:38:40 +03:00
|
|
|
const appState = useRecoilValue<AppStateOptions>(appStateAtom);
|
2022-04-30 01:09:53 +03:00
|
|
|
|
|
|
|
const toggleChatVisibility = () => {
|
2023-05-23 04:56:44 +03:00
|
|
|
// If we don't support the hide chat option then don't do anything.
|
|
|
|
if (!showHideChatOption) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-26 19:00:27 +03:00
|
|
|
setChatState(chatState === ChatState.VISIBLE ? ChatState.HIDDEN : ChatState.VISIBLE);
|
2022-04-30 01:09:53 +03:00
|
|
|
};
|
|
|
|
|
2022-05-14 01:07:49 +03:00
|
|
|
const handleChangeName = () => {
|
|
|
|
setShowNameChangeModal(true);
|
|
|
|
};
|
|
|
|
|
2023-06-13 08:54:12 +03:00
|
|
|
const closeChangeNameModal = () => {
|
|
|
|
setShowNameChangeModal(false);
|
|
|
|
};
|
|
|
|
|
2023-06-26 19:00:27 +03:00
|
|
|
const closeChatPopup = () => {
|
|
|
|
if (popupWindow) {
|
|
|
|
popupWindow.close();
|
|
|
|
}
|
|
|
|
setPopupWindow(null);
|
|
|
|
setChatState(ChatState.VISIBLE);
|
|
|
|
};
|
|
|
|
|
|
|
|
const openChatPopup = () => {
|
|
|
|
// close popup (if any) to prevent multiple popup windows.
|
|
|
|
closeChatPopup();
|
|
|
|
const w = window.open('/embed/chat/readwrite', '_blank', 'popup');
|
|
|
|
w.addEventListener('beforeunload', closeChatPopup);
|
|
|
|
setPopupWindow(w);
|
|
|
|
setChatState(ChatState.POPPED_OUT);
|
|
|
|
};
|
|
|
|
|
|
|
|
const canShowHideChat =
|
|
|
|
showHideChatOption &&
|
|
|
|
appState.chatAvailable &&
|
|
|
|
(chatState === ChatState.HIDDEN || chatState === ChatState.VISIBLE);
|
|
|
|
const canShowChatPopup =
|
|
|
|
showHideChatOption &&
|
|
|
|
appState.chatAvailable &&
|
|
|
|
(chatState === ChatState.HIDDEN ||
|
|
|
|
chatState === ChatState.VISIBLE ||
|
|
|
|
chatState === ChatState.POPPED_OUT);
|
|
|
|
|
2022-06-25 09:27:17 +03:00
|
|
|
// Register keyboard shortcut for the space bar to toggle playback
|
|
|
|
useHotkeys(
|
|
|
|
'c',
|
|
|
|
toggleChatVisibility,
|
|
|
|
{
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
},
|
2023-06-26 19:00:27 +03:00
|
|
|
[chatState === ChatState.VISIBLE],
|
2022-06-25 09:27:17 +03:00
|
|
|
);
|
|
|
|
|
2022-10-11 02:56:02 +03:00
|
|
|
const currentUser = useRecoilValue(currentUserAtom);
|
2023-01-11 03:51:06 +03:00
|
|
|
if (!currentUser) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-11 02:56:02 +03:00
|
|
|
|
|
|
|
const { displayName } = currentUser;
|
|
|
|
const username = defaultUsername || displayName;
|
2022-04-30 01:09:53 +03:00
|
|
|
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>
|
2022-08-21 02:13:31 +03:00
|
|
|
<Menu.Item key="1" icon={<LockOutlined />} onClick={() => setShowAuthModal(true)}>
|
2022-05-22 15:20:11 +03:00
|
|
|
Authenticate
|
|
|
|
</Menu.Item>
|
2023-06-26 19:00:27 +03:00
|
|
|
{canShowHideChat && (
|
2023-01-24 06:16:52 +03:00
|
|
|
<Menu.Item
|
|
|
|
key="3"
|
|
|
|
icon={<MessageOutlined />}
|
|
|
|
onClick={() => toggleChatVisibility()}
|
2023-06-26 19:00:27 +03:00
|
|
|
aria-expanded={chatState === ChatState.VISIBLE}
|
2023-05-23 04:56:44 +03:00
|
|
|
className={styles.chatToggle}
|
2023-01-24 06:16:52 +03:00
|
|
|
>
|
2023-06-26 19:00:27 +03:00
|
|
|
{chatState === ChatState.VISIBLE ? 'Hide Chat' : 'Show Chat'}
|
2022-04-30 01:09:53 +03:00
|
|
|
</Menu.Item>
|
|
|
|
)}
|
2023-06-26 19:00:27 +03:00
|
|
|
{canShowChatPopup &&
|
|
|
|
(popupWindow ? (
|
|
|
|
<Menu.Item key="4" icon={<ShrinkOutlined />} onClick={closeChatPopup}>
|
|
|
|
Put chat back
|
|
|
|
</Menu.Item>
|
|
|
|
) : (
|
|
|
|
<Menu.Item key="4" icon={<ExpandAltOutlined />} onClick={openChatPopup}>
|
|
|
|
Pop out chat
|
|
|
|
</Menu.Item>
|
|
|
|
))}
|
2022-04-30 01:09:53 +03:00
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2023-03-13 09:07:02 +03:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
|
|
|
componentName="UserDropdown"
|
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
>
|
2023-05-24 02:51:44 +03:00
|
|
|
<div className={styles.root}>
|
2023-03-13 09:07:02 +03:00
|
|
|
<Dropdown overlay={menu} trigger={['click']}>
|
2023-05-24 02:51:44 +03:00
|
|
|
<Button id={id} type="primary" icon={<UserOutlined className={styles.userIcon} />}>
|
2023-05-23 04:56:44 +03:00
|
|
|
<span
|
|
|
|
className={classnames([
|
|
|
|
styles.username,
|
|
|
|
hideTitleOnMobile && styles.hideTitleOnMobile,
|
|
|
|
])}
|
|
|
|
>
|
|
|
|
{username}
|
|
|
|
</span>
|
2023-03-13 09:07:02 +03:00
|
|
|
<CaretDownOutlined />
|
|
|
|
</Button>
|
|
|
|
</Dropdown>
|
|
|
|
<Modal
|
|
|
|
title="Change Chat Display Name"
|
|
|
|
open={showNameChangeModal}
|
2023-06-13 08:54:12 +03:00
|
|
|
handleCancel={closeChangeNameModal}
|
2023-03-13 09:07:02 +03:00
|
|
|
>
|
2023-06-13 08:54:12 +03:00
|
|
|
<NameChangeModal closeModal={closeChangeNameModal} />
|
2023-03-13 09:07:02 +03:00
|
|
|
</Modal>
|
|
|
|
<Modal
|
|
|
|
title="Authenticate"
|
|
|
|
open={showAuthModal}
|
|
|
|
handleCancel={() => setShowAuthModal(false)}
|
|
|
|
>
|
|
|
|
<AuthModal />
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
2022-04-30 01:09:53 +03:00
|
|
|
);
|
2022-05-14 00:44:16 +03:00
|
|
|
};
|