idillon | 6847e25 | 2022-11-04 11:50: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 | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 18 | import { Box, Divider, Fade, Stack, Typography } from '@mui/material'; |
| 19 | import { motion } from 'framer-motion'; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 20 | import { ConversationMessage, Message, WebSocketMessageType } from 'jami-web-common'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 21 | import { useCallback, useEffect, useMemo, useState } from 'react'; |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 22 | import { useDropzone } from 'react-dropzone'; |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 23 | import { useTranslation } from 'react-i18next'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 24 | |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 25 | import { FilePreviewRemovable } from '../components/FilePreview'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 26 | import LoadingPage from '../components/Loading'; |
| 27 | import MessageList from '../components/MessageList'; |
idillon | b0ec86a | 2022-12-06 17:54:55 -0500 | [diff] [blame] | 28 | import { FileDragOverlay } from '../components/Overlay'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 29 | import SendMessageForm from '../components/SendMessageForm'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 30 | import { useConversationContext } from '../contexts/ConversationProvider'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 31 | import { useWebSocketContext } from '../contexts/WebSocketProvider'; |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 32 | import { ConversationMember } from '../models/conversation-member'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 33 | import { useMessagesQuery, useSendMessageMutation } from '../services/conversationQueries'; |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 34 | import { FileHandler } from '../utils/files'; |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 35 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 36 | |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 37 | const ChatInterface = () => { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 38 | const webSocket = useWebSocketContext(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 39 | const { conversationId } = useConversationContext(); |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 40 | const [messages, setMessages] = useState<Message[]>([]); |
| 41 | const [isLoading, setIsLoading] = useState(true); |
| 42 | const [error, setError] = useState(false); |
| 43 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 44 | const messagesQuery = useMessagesQuery(conversationId); |
| 45 | const sendMessageMutation = useSendMessageMutation(conversationId); |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 46 | |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 47 | const [fileHandlers, setFileHandlers] = useState<FileHandler[]>([]); |
| 48 | |
| 49 | const onFilesDrop = useCallback( |
| 50 | (acceptedFiles: File[]) => { |
| 51 | const newFileHandlers = acceptedFiles.map((file) => new FileHandler(file)); |
| 52 | setFileHandlers((oldFileHandlers) => [...oldFileHandlers, ...newFileHandlers]); |
| 53 | }, |
| 54 | [setFileHandlers] |
| 55 | ); |
| 56 | |
| 57 | const removeFile = useCallback( |
| 58 | (fileId: string | number) => { |
| 59 | setFileHandlers((fileHandlers) => fileHandlers.filter((fileHandler) => fileHandler.id !== fileId)); |
| 60 | }, |
| 61 | [setFileHandlers] |
| 62 | ); |
| 63 | |
| 64 | const { |
| 65 | getRootProps, |
| 66 | getInputProps, |
| 67 | open: openFilePicker, |
| 68 | isDragActive, |
| 69 | } = useDropzone({ |
| 70 | onDrop: onFilesDrop, |
| 71 | noClick: true, |
| 72 | noKeyboard: true, |
| 73 | }); |
| 74 | |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 75 | useEffect(() => { |
| 76 | if (messagesQuery.isSuccess) { |
| 77 | const sortedMessages = sortMessages(messagesQuery.data); |
| 78 | setMessages(sortedMessages); |
| 79 | } |
| 80 | }, [messagesQuery.isSuccess, messagesQuery.data]); |
| 81 | |
| 82 | useEffect(() => { |
| 83 | setIsLoading(messagesQuery.isLoading); |
| 84 | }, [messagesQuery.isLoading]); |
| 85 | |
| 86 | useEffect(() => { |
| 87 | setError(messagesQuery.isError); |
| 88 | }, [messagesQuery.isError]); |
| 89 | |
| 90 | const sendMessage = useCallback((message: string) => sendMessageMutation.mutate(message), [sendMessageMutation]); |
| 91 | |
| 92 | useEffect(() => { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 93 | const conversationMessageListener = (data: ConversationMessage) => { |
| 94 | console.log('newMessage'); |
| 95 | setMessages((messages) => addMessage(messages, data.message)); |
| 96 | }; |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame] | 97 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 98 | webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 99 | |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 100 | return () => { |
| 101 | webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
| 102 | }; |
simon | a5c54ef | 2022-11-18 05:26:06 -0500 | [diff] [blame] | 103 | }, [webSocket]); |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 104 | |
| 105 | if (isLoading) { |
| 106 | return <LoadingPage />; |
| 107 | } else if (error) { |
| 108 | return <div>Error loading {conversationId}</div>; |
| 109 | } |
| 110 | |
| 111 | return ( |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 112 | <Stack flex={1} overflow="hidden" {...getRootProps()} paddingBottom="16px"> |
idillon | b0ec86a | 2022-12-06 17:54:55 -0500 | [diff] [blame] | 113 | {isDragActive && <FileDragOverlay />} |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 114 | <input {...getInputProps()} /> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 115 | <MessageList messages={messages} /> |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 116 | <ComposingMembersIndicator /> |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 117 | <Divider |
| 118 | sx={{ |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 119 | marginX: '16px', |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 120 | borderTop: '1px solid #E5E5E5', |
| 121 | }} |
| 122 | /> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 123 | <SendMessageForm onSend={sendMessage} openFilePicker={openFilePicker} /> |
idillon | cab81d7 | 2022-11-08 12:20:00 -0500 | [diff] [blame] | 124 | {fileHandlers.length > 0 && <FilePreviewsList fileHandlers={fileHandlers} removeFile={removeFile} />} |
| 125 | </Stack> |
| 126 | ); |
| 127 | }; |
| 128 | |
| 129 | interface FilePreviewsListProps { |
| 130 | fileHandlers: FileHandler[]; |
| 131 | removeFile: (fileId: string | number) => void; |
| 132 | } |
| 133 | |
| 134 | const FilePreviewsList = ({ fileHandlers, removeFile }: FilePreviewsListProps) => { |
| 135 | return ( |
| 136 | <Stack |
| 137 | direction="row" |
| 138 | flexWrap="wrap" |
| 139 | gap="16px" |
| 140 | overflow="auto" |
| 141 | maxHeight="30%" |
| 142 | paddingX="16px" |
| 143 | marginTop="12px" // spacing with the component on top |
| 144 | paddingTop="4px" // spacing so "RemoveButton" are not cut |
| 145 | > |
| 146 | {fileHandlers.map((fileHandler) => ( |
| 147 | <FilePreviewRemovable |
| 148 | key={fileHandler.id} |
| 149 | remove={() => removeFile(fileHandler.id)} |
| 150 | fileHandler={fileHandler} |
| 151 | borderColor={'#005699' /* Should be same color as message bubble */} |
| 152 | /> |
| 153 | ))} |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 154 | </Stack> |
| 155 | ); |
| 156 | }; |
| 157 | |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 158 | export const ComposingMembersIndicator = () => { |
| 159 | const { t } = useTranslation(); |
| 160 | const { composingMembers } = useConversationContext(); |
| 161 | |
| 162 | const text = useMemo(() => { |
| 163 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 164 | elementPartialKey: 'member', |
| 165 | getElementValue: (member) => member.getDisplayName(), |
| 166 | translaters: [ |
| 167 | () => '', |
| 168 | (interpolations) => t('are_composing_1', interpolations), |
| 169 | (interpolations) => t('are_composing_2', interpolations), |
| 170 | (interpolations) => t('are_composing_3', interpolations), |
| 171 | (interpolations) => t('are_composing_more', interpolations), |
| 172 | ], |
| 173 | }; |
| 174 | |
| 175 | return translateEnumeration<ConversationMember>(composingMembers, options); |
| 176 | }, [composingMembers, t]); |
| 177 | |
| 178 | return ( |
| 179 | <Stack height="30px" padding="0 16px" justifyContent="center"> |
| 180 | <Fade in={composingMembers.length !== 0}> |
| 181 | <Stack |
| 182 | alignItems="center" |
| 183 | direction="row" |
| 184 | spacing="8.5px" |
| 185 | sx={(theme: any) => ({ |
| 186 | height: theme.typography.caption.lineHeight, |
| 187 | })} |
| 188 | > |
| 189 | <WaitingDots /> |
| 190 | <Typography variant="caption">{text}</Typography> |
| 191 | </Stack> |
| 192 | </Fade> |
| 193 | </Stack> |
| 194 | ); |
| 195 | }; |
| 196 | |
| 197 | const SingleDot = ({ delay }: { delay: number }) => ( |
| 198 | <Box |
| 199 | width="8px" |
| 200 | height="8px" |
| 201 | borderRadius="100%" |
| 202 | sx={{ backgroundColor: '#000000' }} |
| 203 | component={motion.div} |
| 204 | animate={{ scale: [0.75, 1, 0.75] }} |
| 205 | transition={{ |
| 206 | delay, |
| 207 | duration: 0.5, |
| 208 | repeatDelay: 1, |
| 209 | repeatType: 'loop', |
| 210 | repeat: Infinity, |
| 211 | ease: 'easeInOut', |
| 212 | }} |
| 213 | /> |
| 214 | ); |
| 215 | |
| 216 | const WaitingDots = () => { |
| 217 | return ( |
| 218 | <Stack direction="row" spacing="5px"> |
| 219 | <SingleDot delay={0} /> |
| 220 | <SingleDot delay={0.5} /> |
| 221 | <SingleDot delay={1} /> |
| 222 | </Stack> |
| 223 | ); |
| 224 | }; |
| 225 | |
idillon | 6847e25 | 2022-11-04 11:50:08 -0400 | [diff] [blame] | 226 | const addMessage = (sortedMessages: Message[], message: Message) => { |
| 227 | if (sortedMessages.length === 0) { |
| 228 | return [message]; |
| 229 | } else if (message.id === sortedMessages[sortedMessages.length - 1].linearizedParent) { |
| 230 | return [...sortedMessages, message]; |
| 231 | } else if (message.linearizedParent === sortedMessages[0].id) { |
| 232 | return [message, ...sortedMessages]; |
| 233 | } else { |
| 234 | console.error("Can't insert message " + message.id); |
| 235 | return sortedMessages; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | const sortMessages = (messages: Message[]) => { |
| 240 | let sortedMessages: Message[] = []; |
| 241 | messages.forEach((message) => (sortedMessages = addMessage(sortedMessages, message))); |
| 242 | return sortedMessages; |
| 243 | }; |
| 244 | |
| 245 | export default ChatInterface; |