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 | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 22 | import { useNavigate } from 'react-router'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 24 | import { SocketContext } from '../contexts/Socket'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 25 | import { useAccountQuery } from '../services/Account'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 26 | import { useConversationQuery, useMessagesQuery, useSendMessageMutation } from '../services/Conversation'; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 27 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
| 28 | import { AddParticipantButton, ShowOptionsMenuButton, StartAudioCallButton, StartVideoCallButton } from './Button'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 29 | import LoadingPage from './Loading'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 30 | import MessageList from './MessageList'; |
| 31 | import SendMessageForm from './SendMessageForm'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 32 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 33 | type ConversationViewProps = { |
| 34 | accountId: string; |
| 35 | conversationId: string; |
| 36 | }; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 37 | const ConversationView = ({ accountId, conversationId }: ConversationViewProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 38 | const socket = useContext(SocketContext); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 39 | const [account, setAccount] = useState<Account | undefined>(); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 40 | const [conversation, setConversation] = useState<Conversation | undefined>(); |
| 41 | const [messages, setMessages] = useState<Message[]>([]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 42 | const [isLoading, setIsLoading] = useState(true); |
| 43 | const [error, setError] = useState(false); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 44 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 45 | const accountQuery = useAccountQuery(accountId); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 46 | const conversationQuery = useConversationQuery(accountId, conversationId); |
| 47 | const messagesQuery = useMessagesQuery(accountId, conversationId); |
| 48 | const sendMessageMutation = useSendMessageMutation(accountId, conversationId); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 49 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 50 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 51 | if (accountQuery.isSuccess) { |
| 52 | setAccount(Account.from(accountQuery.data)); |
| 53 | } |
| 54 | }, [accountQuery.isSuccess, accountQuery.data]); |
| 55 | |
| 56 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 57 | if (conversationQuery.isSuccess) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 58 | const conversation = Conversation.from(accountId, conversationQuery.data); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | setConversation(conversation); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 60 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 61 | }, [accountId, conversationQuery.isSuccess, conversationQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 62 | |
| 63 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 64 | if (messagesQuery.isSuccess) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 65 | const sortedMessages = sortMessages(messagesQuery.data); |
| 66 | setMessages(sortedMessages); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 67 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 68 | }, [messagesQuery.isSuccess, messagesQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 69 | |
| 70 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 71 | setIsLoading(accountQuery.isLoading || conversationQuery.isLoading || messagesQuery.isLoading); |
| 72 | }, [accountQuery.isLoading, conversationQuery.isLoading, messagesQuery.isLoading]); |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 73 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 74 | useEffect(() => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 75 | setError(accountQuery.isLoading || conversationQuery.isError || messagesQuery.isError); |
| 76 | }, [accountQuery.isLoading, conversationQuery.isError, messagesQuery.isError]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 77 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 78 | const sendMessage = useCallback((message: string) => sendMessageMutation.mutate(message), [sendMessageMutation]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 79 | |
| 80 | useEffect(() => { |
| 81 | if (!conversation) return; |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 82 | console.log(`io set conversation ${conversationId} ` + socket); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 83 | if (socket) { |
| 84 | socket.emit('conversation', { |
| 85 | accountId, |
| 86 | conversationId, |
| 87 | }); |
| 88 | socket.off('newMessage'); |
| 89 | socket.on('newMessage', (data) => { |
| 90 | console.log('newMessage'); |
| 91 | setMessages((messages) => addMessage(messages, data)); |
| 92 | }); |
| 93 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 94 | }, [accountId, conversation, conversationId, socket]); |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 95 | |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 96 | if (isLoading) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 97 | return <LoadingPage />; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 98 | } else if (error || !account || !conversation) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 99 | return <div>Error loading {conversationId}</div>; |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 100 | } |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 101 | |
| 102 | return ( |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 103 | <Stack height="100%"> |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame^] | 104 | <ConversationHeader |
| 105 | account={account} |
| 106 | members={conversation.getMembers()} |
| 107 | adminTitle={conversation.infos.title as string} |
| 108 | conversationId={conversationId} |
| 109 | /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 110 | <Divider |
| 111 | sx={{ |
| 112 | borderTop: '1px solid #E5E5E5', |
| 113 | }} |
| 114 | /> |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame^] | 115 | <MessageList account={account} members={conversation.getMembers()} messages={messages} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 116 | <Divider |
| 117 | sx={{ |
| 118 | margin: '30px 16px 0px 16px', |
| 119 | borderTop: '1px solid #E5E5E5', |
| 120 | }} |
| 121 | /> |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame^] | 122 | <SendMessageForm account={account} members={conversation.getMembers()} onSend={sendMessage} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 123 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 124 | ); |
| 125 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 126 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 127 | type ConversationHeaderProps = { |
| 128 | account: Account; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 129 | conversationId: string; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 130 | members: ConversationMember[]; |
| 131 | adminTitle: string | undefined; |
| 132 | }; |
| 133 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 134 | const ConversationHeader = ({ account, members, adminTitle, conversationId }: ConversationHeaderProps) => { |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 135 | const { t } = useTranslation(); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 136 | const navigate = useNavigate(); |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 137 | |
| 138 | const title = useMemo(() => { |
| 139 | if (adminTitle) { |
| 140 | return adminTitle; |
| 141 | } |
| 142 | |
| 143 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 144 | elementPartialKey: 'member', |
| 145 | getElementValue: (member) => getMemberName(member), |
| 146 | translaters: [ |
| 147 | () => |
| 148 | // The user is chatting with themself |
| 149 | t('conversation_title_one', { member0: account?.getDisplayName() }), |
| 150 | (interpolations) => t('conversation_title_one', interpolations), |
| 151 | (interpolations) => t('conversation_title_two', interpolations), |
| 152 | (interpolations) => t('conversation_title_three', interpolations), |
| 153 | (interpolations) => t('conversation_title_four', interpolations), |
| 154 | (interpolations) => t('conversation_title_more', interpolations), |
| 155 | ], |
| 156 | }; |
| 157 | |
| 158 | return translateEnumeration<ConversationMember>(members, options); |
| 159 | }, [account, members, adminTitle, t]); |
| 160 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 161 | const startCall = (withVideo = false) => { |
Michelle Sepkap Sime | b3dd312 | 2022-11-03 02:12:39 -0400 | [diff] [blame] | 162 | let url = `/deprecated-account/${account.getId()}/call/${conversationId}`; |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 163 | if (withVideo) { |
| 164 | url += '?video=true'; |
| 165 | } |
| 166 | navigate(url); |
| 167 | }; |
| 168 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 169 | return ( |
idillon | 02f579d | 2022-11-06 21:26:55 -0500 | [diff] [blame^] | 170 | <Stack direction="row" padding="16px"> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 171 | <Stack flex={1} justifyContent="center" whiteSpace="nowrap" overflow="hidden"> |
| 172 | <Typography variant="h3" textOverflow="ellipsis"> |
| 173 | {title} |
| 174 | </Typography> |
| 175 | </Stack> |
| 176 | <Stack direction="row" spacing="20px"> |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 177 | <StartAudioCallButton onClick={() => startCall(false)} /> |
| 178 | <StartVideoCallButton onClick={() => startCall(true)} /> |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 179 | <AddParticipantButton /> |
| 180 | <ShowOptionsMenuButton /> |
| 181 | </Stack> |
| 182 | </Stack> |
| 183 | ); |
| 184 | }; |
| 185 | |
| 186 | const getMemberName = (member: ConversationMember) => { |
| 187 | const contact = member.contact; |
| 188 | return contact.getDisplayName(); |
| 189 | }; |
| 190 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 191 | const addMessage = (sortedMessages: Message[], message: Message) => { |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 192 | if (sortedMessages.length === 0) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 193 | return [message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 194 | } else if (message.id === sortedMessages[sortedMessages.length - 1].linearizedParent) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 195 | return [...sortedMessages, message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 196 | } else if (message.linearizedParent === sortedMessages[0].id) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 197 | return [message, ...sortedMessages]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 198 | } else { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 199 | console.log("Can't insert message " + message.id); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 200 | return sortedMessages; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 201 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 202 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 203 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 204 | const sortMessages = (messages: Message[]) => { |
| 205 | let sortedMessages: Message[] = []; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 206 | messages.forEach((message) => (sortedMessages = addMessage(sortedMessages, message))); |
| 207 | return sortedMessages; |
| 208 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 209 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 210 | export default ConversationView; |