2023-05-20 02:30:24 +08:00
|
|
|
import React from 'react';
|
2023-07-08 04:46:36 +08:00
|
|
|
import { useMutation, useQuery } from '@apollo/client';
|
2023-05-20 02:30:24 +08:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2023-09-14 00:37:40 +08:00
|
|
|
import { GET_CHAT_DATA, GetChatDataResponse, CLOSE_PRIVATE_CHAT_MUTATION } from './queries';
|
|
|
|
import closePrivateChat from './services';
|
2023-05-20 02:30:24 +08:00
|
|
|
import { layoutSelect, layoutDispatch } from '../../../layout/context';
|
2023-08-15 04:52:35 +08:00
|
|
|
import { useShortcut } from '../../../../core/hooks/useShortcut';
|
2023-05-20 02:30:24 +08:00
|
|
|
import { Layout } from '../../../layout/layoutTypes';
|
2023-07-08 04:46:36 +08:00
|
|
|
import { ACTIONS, PANELS } from '../../../layout/enums';
|
2023-09-14 00:37:40 +08:00
|
|
|
import ChatActions from './chat-actions/component';
|
2024-02-23 03:19:50 +08:00
|
|
|
import { ChatHeader as Header } from '../chat-message-list/page/chat-message/styles';
|
2023-05-20 02:30:24 +08:00
|
|
|
|
|
|
|
interface ChatHeaderProps {
|
|
|
|
chatId: string;
|
|
|
|
isPublicChat: boolean;
|
|
|
|
title: string;
|
2024-02-23 03:19:50 +08:00
|
|
|
isRTL: boolean;
|
2023-05-20 02:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
closeChatLabel: {
|
|
|
|
id: 'app.chat.closeChatLabel',
|
|
|
|
description: 'aria-label for closing chat button',
|
|
|
|
},
|
|
|
|
hideChatLabel: {
|
|
|
|
id: 'app.chat.hideChatLabel',
|
|
|
|
description: 'aria-label for hiding chat button',
|
|
|
|
},
|
|
|
|
titlePublic: {
|
|
|
|
id: 'app.chat.titlePublic',
|
|
|
|
description: 'Public chat title',
|
|
|
|
},
|
|
|
|
titlePrivate: {
|
|
|
|
id: 'app.chat.titlePrivate',
|
|
|
|
description: 'Private chat title',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-02-23 03:34:32 +08:00
|
|
|
const ChatHeader: React.FC<ChatHeaderProps> = ({
|
|
|
|
chatId, isPublicChat, title, isRTL,
|
|
|
|
}) => {
|
2023-08-15 04:52:35 +08:00
|
|
|
const HIDE_CHAT_AK = useShortcut('hideprivatechat');
|
|
|
|
const CLOSE_CHAT_AK = useShortcut('closeprivatechat');
|
2023-05-20 02:30:24 +08:00
|
|
|
const layoutContextDispatch = layoutDispatch();
|
|
|
|
const intl = useIntl();
|
2023-07-08 04:46:36 +08:00
|
|
|
const [updateVisible] = useMutation(CLOSE_PRIVATE_CHAT_MUTATION);
|
2023-05-20 02:30:24 +08:00
|
|
|
return (
|
|
|
|
<Header
|
2024-02-23 03:19:50 +08:00
|
|
|
isRTL={isRTL}
|
2023-07-08 04:46:36 +08:00
|
|
|
data-test="chatTitle"
|
|
|
|
leftButtonProps={{
|
|
|
|
accessKey: chatId !== 'public' ? HIDE_CHAT_AK : null,
|
|
|
|
'aria-label': intl.formatMessage(intlMessages.hideChatLabel, { 0: title }),
|
|
|
|
'data-test': isPublicChat ? 'hidePublicChat' : 'hidePrivateChat',
|
|
|
|
label: title,
|
|
|
|
onClick: () => {
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_SIDEBAR_CONTENT_IS_OPEN,
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_ID_CHAT_OPEN,
|
|
|
|
value: '',
|
|
|
|
});
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_SIDEBAR_CONTENT_PANEL,
|
|
|
|
value: PANELS.NONE,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
rightButtonProps={{
|
|
|
|
accessKey: CLOSE_CHAT_AK,
|
|
|
|
'aria-label': intl.formatMessage(intlMessages.closeChatLabel, { 0: title }),
|
|
|
|
'data-test': 'closePrivateChat',
|
|
|
|
icon: 'close',
|
|
|
|
label: intl.formatMessage(intlMessages.closeChatLabel, { 0: title }),
|
|
|
|
onClick: () => {
|
2024-07-05 04:00:06 +08:00
|
|
|
updateVisible({ variables: { chatId, visible: false } });
|
2023-07-08 04:46:36 +08:00
|
|
|
closePrivateChat(chatId);
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_SIDEBAR_CONTENT_IS_OPEN,
|
|
|
|
value: false,
|
|
|
|
});
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_ID_CHAT_OPEN,
|
|
|
|
value: '',
|
|
|
|
});
|
|
|
|
layoutContextDispatch({
|
|
|
|
type: ACTIONS.SET_SIDEBAR_CONTENT_PANEL,
|
|
|
|
value: PANELS.NONE,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
customRightButton={isPublicChat ? <ChatActions /> : null}
|
|
|
|
/>
|
2023-05-20 02:30:24 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isChatResponse = (data: unknown): data is GetChatDataResponse => {
|
|
|
|
return (data as GetChatDataResponse).chat !== undefined;
|
|
|
|
};
|
|
|
|
|
2023-07-08 04:46:36 +08:00
|
|
|
const ChatHeaderContainer: React.FC = () => {
|
2023-05-20 02:30:24 +08:00
|
|
|
const intl = useIntl();
|
|
|
|
const idChatOpen = layoutSelect((i: Layout) => i.idChatOpen);
|
2024-02-23 03:19:50 +08:00
|
|
|
const isRTL = layoutSelect((i: Layout) => i.isRTL);
|
|
|
|
|
2023-05-20 02:30:24 +08:00
|
|
|
const {
|
|
|
|
data: chatData,
|
|
|
|
loading: chatDataLoading,
|
|
|
|
error: chatDataError,
|
|
|
|
} = useQuery<GetChatDataResponse>(GET_CHAT_DATA, {
|
|
|
|
variables: { chatId: idChatOpen },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (chatDataLoading) return null;
|
2023-09-14 00:37:40 +08:00
|
|
|
if (chatDataError) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
Error:
|
|
|
|
{JSON.stringify(chatDataError)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!isChatResponse(chatData)) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
Error:
|
|
|
|
{JSON.stringify(chatData)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-05-20 02:30:24 +08:00
|
|
|
const isPublicChat = chatData.chat[0]?.public;
|
|
|
|
const title = isPublicChat ? intl.formatMessage(intlMessages.titlePublic)
|
|
|
|
: intl.formatMessage(intlMessages.titlePrivate, { 0: chatData?.chat[0]?.participant?.name });
|
|
|
|
return (
|
|
|
|
<ChatHeader
|
|
|
|
chatId={idChatOpen}
|
|
|
|
isPublicChat={isPublicChat}
|
|
|
|
title={title}
|
2024-02-23 03:19:50 +08:00
|
|
|
isRTL={isRTL}
|
2023-05-20 02:30:24 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-14 00:37:40 +08:00
|
|
|
export default ChatHeaderContainer;
|