simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 18 | import { Divider, Stack, Typography } from '@mui/material'; |
| 19 | import { Account, Conversation, ConversationMember, Message } from 'jami-web-common'; |
| 20 | import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
| 21 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 23 | import { SocketContext } from '../contexts/Socket'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 24 | import { useAccountQuery } from '../services/Account'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 25 | import { useConversationQuery, useMessagesQuery, useSendMessageMutation } from '../services/Conversation'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 26 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
| 27 | import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 28 | import LoadingPage from './Loading'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 29 | import MessageList from './MessageList'; |
| 30 | import SendMessageForm from './SendMessageForm'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 31 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 32 | type ConversationViewProps = { |
| 33 | accountId: string; |
| 34 | conversationId: string; |
| 35 | }; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 36 | const ConversationView = ({ accountId, conversationId }: ConversationViewProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | const socket = useContext(SocketContext); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 38 | const [account, setAccount] = useState<Account | undefined>(); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 39 | const [conversation, setConversation] = useState<Conversation | undefined>(); |
| 40 | const [messages, setMessages] = useState<Message[]>([]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 41 | const [isLoading, setIsLoading] = useState(true); |
| 42 | const [error, setError] = useState(false); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 43 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 44 | const accountQuery = useAccountQuery(accountId); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 45 | const conversationQuery = useConversationQuery(accountId, conversationId); |
| 46 | const messagesQuery = useMessagesQuery(accountId, conversationId); |
| 47 | const sendMessageMutation = useSendMessageMutation(accountId, conversationId); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 48 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 49 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 50 | if (accountQuery.isSuccess) { |
| 51 | setAccount(Account.from(accountQuery.data)); |
| 52 | } |
| 53 | }, [accountQuery.isSuccess, accountQuery.data]); |
| 54 | |
| 55 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 56 | if (conversationQuery.isSuccess) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 57 | const conversation = Conversation.from(accountId, conversationQuery.data); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 58 | setConversation(conversation); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 59 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 60 | }, [accountId, conversationQuery.isSuccess, conversationQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 61 | |
| 62 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 63 | if (messagesQuery.isSuccess) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 64 | const sortedMessages = sortMessages(messagesQuery.data); |
| 65 | setMessages(sortedMessages); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 66 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 67 | }, [messagesQuery.isSuccess, messagesQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 68 | |
| 69 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 70 | setIsLoading(accountQuery.isLoading || conversationQuery.isLoading || messagesQuery.isLoading); |
| 71 | }, [accountQuery.isLoading, conversationQuery.isLoading, messagesQuery.isLoading]); |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 72 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 73 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 74 | setError(accountQuery.isLoading || conversationQuery.isError || messagesQuery.isError); |
| 75 | }, [accountQuery.isLoading, conversationQuery.isError, messagesQuery.isError]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 76 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 77 | const sendMessage = useCallback((message: string) => sendMessageMutation.mutate(message), [sendMessageMutation]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | |
| 79 | useEffect(() => { |
| 80 | if (!conversation) return; |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 81 | console.log(`io set conversation ${conversationId} ` + socket); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 82 | if (socket) { |
| 83 | socket.emit('conversation', { |
| 84 | accountId, |
| 85 | conversationId, |
| 86 | }); |
| 87 | socket.off('newMessage'); |
| 88 | socket.on('newMessage', (data) => { |
| 89 | console.log('newMessage'); |
| 90 | setMessages((messages) => addMessage(messages, data)); |
| 91 | }); |
| 92 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 93 | }, [accountId, conversation, conversationId, socket]); |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 94 | |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 95 | if (isLoading) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 96 | return <LoadingPage />; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 97 | } else if (error || !account || !conversation) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 98 | return <div>Error loading {conversationId}</div>; |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 99 | } |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 100 | |
| 101 | return ( |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 102 | <Stack height="100%"> |
| 103 | <Stack padding="16px"> |
| 104 | <ConversationHeader |
| 105 | account={account} |
| 106 | members={conversation.getMembers()} |
| 107 | adminTitle={conversation.infos.title as string} |
| 108 | /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 109 | </Stack> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 110 | <Divider |
| 111 | sx={{ |
| 112 | borderTop: '1px solid #E5E5E5', |
| 113 | }} |
| 114 | /> |
| 115 | <Stack flex={1} overflow="auto" direction="column-reverse" padding="0px 16px"> |
| 116 | <MessageList account={account} members={conversation.getMembers()} messages={messages} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 117 | </Stack> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 118 | <Divider |
| 119 | sx={{ |
| 120 | margin: '30px 16px 0px 16px', |
| 121 | borderTop: '1px solid #E5E5E5', |
| 122 | }} |
| 123 | /> |
| 124 | <Stack padding="16px"> |
| 125 | <SendMessageForm account={account} members={conversation.getMembers()} onSend={sendMessage} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 126 | </Stack> |
| 127 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 128 | ); |
| 129 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 130 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 131 | type ConversationHeaderProps = { |
| 132 | account: Account; |
| 133 | members: ConversationMember[]; |
| 134 | adminTitle: string | undefined; |
| 135 | }; |
| 136 | |
| 137 | const ConversationHeader = ({ account, members, adminTitle }: ConversationHeaderProps) => { |
| 138 | const { t } = useTranslation(); |
| 139 | |
| 140 | const title = useMemo(() => { |
| 141 | if (adminTitle) { |
| 142 | return adminTitle; |
| 143 | } |
| 144 | |
| 145 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 146 | elementPartialKey: 'member', |
| 147 | getElementValue: (member) => getMemberName(member), |
| 148 | translaters: [ |
| 149 | () => |
| 150 | // The user is chatting with themself |
| 151 | t('conversation_title_one', { member0: account?.getDisplayName() }), |
| 152 | (interpolations) => t('conversation_title_one', interpolations), |
| 153 | (interpolations) => t('conversation_title_two', interpolations), |
| 154 | (interpolations) => t('conversation_title_three', interpolations), |
| 155 | (interpolations) => t('conversation_title_four', interpolations), |
| 156 | (interpolations) => t('conversation_title_more', interpolations), |
| 157 | ], |
| 158 | }; |
| 159 | |
| 160 | return translateEnumeration<ConversationMember>(members, options); |
| 161 | }, [account, members, adminTitle, t]); |
| 162 | |
| 163 | return ( |
| 164 | <Stack direction="row"> |
| 165 | <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden"> |
| 166 | <Typography variant="h3" textOverflow="ellipsis"> |
| 167 | {title} |
| 168 | </Typography> |
| 169 | </Stack> |
| 170 | <Stack direction="row" spacing="20px"> |
| 171 | <StartAudioCallButton /> |
| 172 | <StartVideoCallButton /> |
| 173 | <AddParticipantButton /> |
| 174 | <ShowOptionsMenuButton /> |
| 175 | </Stack> |
| 176 | </Stack> |
| 177 | ); |
| 178 | }; |
| 179 | |
| 180 | const getMemberName = (member: ConversationMember) => { |
| 181 | const contact = member.contact; |
| 182 | return contact.getDisplayName(); |
| 183 | }; |
| 184 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 185 | const addMessage = (sortedMessages: Message[], message: Message) => { |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 186 | if (sortedMessages.length === 0) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 187 | return [message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 188 | } else if (message.id === sortedMessages[sortedMessages.length - 1].linearizedParent) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 189 | return [...sortedMessages, message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 190 | } else if (message.linearizedParent === sortedMessages[0].id) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 191 | return [message, ...sortedMessages]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 192 | } else { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 193 | console.log("Can't insert message " + message.id); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 194 | return sortedMessages; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 195 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 196 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 197 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 198 | const sortMessages = (messages: Message[]) => { |
| 199 | let sortedMessages: Message[] = []; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 200 | messages.forEach((message) => (sortedMessages = addMessage(sortedMessages, message))); |
| 201 | return sortedMessages; |
| 202 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 203 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 204 | export default ConversationView; |