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 | */ |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 18 | import { Box, Stack, Typography } from '@mui/material'; |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 19 | import { Conversation, Message } from 'jami-web-common'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import { useCallback, useContext, useEffect, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 22 | import { SocketContext } from '../contexts/Socket'; |
| 23 | import { useConversationQuery, useMessagesQuery, useSendMessageMutation } from '../services/Conversation'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 24 | import ConversationAvatar from './ConversationAvatar'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 25 | import LoadingPage from './Loading'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 26 | import MessageList from './MessageList'; |
| 27 | import SendMessageForm from './SendMessageForm'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 28 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 29 | type ConversationViewProps = { |
| 30 | accountId: string; |
| 31 | conversationId: string; |
| 32 | }; |
| 33 | const ConversationView = ({ accountId, conversationId, ...props }: ConversationViewProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 34 | const socket = useContext(SocketContext); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 35 | const [conversation, setConversation] = useState<Conversation | undefined>(); |
| 36 | const [messages, setMessages] = useState<Message[]>([]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | const [isLoading, setIsLoading] = useState(true); |
| 38 | const [error, setError] = useState(false); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 39 | |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 40 | const conversationQuery = useConversationQuery(accountId, conversationId); |
| 41 | const messagesQuery = useMessagesQuery(accountId, conversationId); |
| 42 | const sendMessageMutation = useSendMessageMutation(accountId, conversationId); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 43 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 44 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 45 | if (conversationQuery.isSuccess) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 46 | const conversation = Conversation.from(accountId, conversationQuery.data); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | setConversation(conversation); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 48 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 49 | }, [accountId, conversationQuery.isSuccess, conversationQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 50 | |
| 51 | useEffect(() => { |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 52 | if (messagesQuery.isSuccess) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | const sortedMessages = sortMessages(messagesQuery.data); |
| 54 | setMessages(sortedMessages); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 55 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 56 | }, [messagesQuery.isSuccess, messagesQuery.data]); |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 57 | |
| 58 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | setIsLoading(conversationQuery.isLoading || messagesQuery.isLoading); |
| 60 | }, [conversationQuery.isLoading, messagesQuery.isLoading]); |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 61 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 62 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 63 | setError(conversationQuery.isError || messagesQuery.isError); |
| 64 | }, [conversationQuery.isError, messagesQuery.isError]); |
| 65 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 66 | const sendMessage = useCallback((message: string) => sendMessageMutation.mutate(message), [sendMessageMutation]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 67 | |
| 68 | useEffect(() => { |
| 69 | if (!conversation) return; |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 70 | console.log(`io set conversation ${conversationId} ` + socket); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 71 | if (socket) { |
| 72 | socket.emit('conversation', { |
| 73 | accountId, |
| 74 | conversationId, |
| 75 | }); |
| 76 | socket.off('newMessage'); |
| 77 | socket.on('newMessage', (data) => { |
| 78 | console.log('newMessage'); |
| 79 | setMessages((messages) => addMessage(messages, data)); |
| 80 | }); |
| 81 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 82 | }, [accountId, conversation, conversationId, socket]); |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 83 | |
idillon | ea46560 | 2022-09-13 19:58:51 -0400 | [diff] [blame] | 84 | if (isLoading) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 85 | return <LoadingPage />; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 86 | } else if (error) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 87 | return <div>Error loading {conversationId}</div>; |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 88 | } |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 89 | |
| 90 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 91 | <Stack flexGrow={1} height="100%"> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 92 | <Stack direction="row" flexGrow={0}> |
| 93 | <Box style={{ margin: 16, flexShrink: 0 }}> |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 94 | <ConversationAvatar displayName={conversation?.getDisplayNameNoFallback()} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 95 | </Box> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 96 | <Box style={{ flex: '1 1 auto', overflow: 'hidden' }}> |
| 97 | <Typography className="title" variant="h6"> |
| 98 | {conversation?.getDisplayName()} |
| 99 | </Typography> |
| 100 | <Typography className="subtitle" variant="subtitle1"> |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 101 | {conversationId} |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 102 | </Typography> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 103 | </Box> |
| 104 | </Stack> |
idillon | aedab94 | 2022-09-01 14:29:43 -0400 | [diff] [blame] | 105 | <Stack flexGrow={1} overflow="auto" direction="column-reverse"> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 106 | <MessageList messages={messages} /> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 107 | </Stack> |
| 108 | <Stack flexGrow={0}> |
| 109 | <SendMessageForm onSend={sendMessage} /> |
| 110 | </Stack> |
| 111 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 112 | ); |
| 113 | }; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 114 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 115 | const addMessage = (sortedMessages: Message[], message: Message) => { |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 116 | if (sortedMessages.length === 0) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 117 | return [message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 118 | } else if (message.id === sortedMessages[sortedMessages.length - 1].linearizedParent) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 119 | return [...sortedMessages, message]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 120 | } else if (message.linearizedParent === sortedMessages[0].id) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 121 | return [message, ...sortedMessages]; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 122 | } else { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 123 | console.log("Can't insert message " + message.id); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 124 | return sortedMessages; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 125 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 126 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 127 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 128 | const sortMessages = (messages: Message[]) => { |
| 129 | let sortedMessages: Message[] = []; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 130 | messages.forEach((message) => (sortedMessages = addMessage(sortedMessages, message))); |
| 131 | return sortedMessages; |
| 132 | }; |
idillon | 08f7717 | 2022-09-13 19:14:17 -0400 | [diff] [blame] | 133 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 134 | export default ConversationView; |