simon | f929a36 | 2022-11-18 16:53:45 -0500 | [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 { ComposingStatus, ConversationInfos, ConversationView, WebSocketMessageType } from 'jami-web-common'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 19 | import { useCallback, useEffect, useMemo, useState } from 'react'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 20 | |
| 21 | import LoadingPage from '../components/Loading'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 22 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 23 | import { useConversationDisplayName } from '../hooks/useConversationDisplayName'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 24 | import { useUrlParams } from '../hooks/useUrlParams'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import { ConversationMember } from '../models/conversation-member'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 26 | import { ConversationRouteParams } from '../router'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 27 | import { useConversationInfosQuery, useMembersQuery } from '../services/conversationQueries'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | import { WithChildren } from '../utils/utils'; |
| 29 | import { useAuthContext } from './AuthProvider'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 30 | import { useWebSocketContext } from './WebSocketProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 31 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 32 | interface IConversationContext { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 33 | conversationId: string; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 34 | conversationDisplayName: string; |
| 35 | conversationInfos: ConversationInfos; |
| 36 | members: ConversationMember[]; |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 37 | composingMembers: ConversationMember[]; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 38 | } |
| 39 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 40 | const optionalConversationContext = createOptionalContext<IConversationContext>('ConversationContext'); |
| 41 | const ConversationContext = optionalConversationContext.Context; |
| 42 | export const useConversationContext = optionalConversationContext.useOptionalContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 43 | |
| 44 | export default ({ children }: WithChildren) => { |
| 45 | const { |
| 46 | urlParams: { conversationId }, |
| 47 | } = useUrlParams<ConversationRouteParams>(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 48 | const { accountId, account } = useAuthContext(); |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 49 | const webSocket = useWebSocketContext(); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 50 | const [composingMembers, setComposingMembers] = useState<ConversationMember[]>([]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 51 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 52 | const conversationInfosQuery = useConversationInfosQuery(conversationId!); |
| 53 | const membersQuery = useMembersQuery(conversationId!); |
| 54 | |
| 55 | const isError = useMemo( |
| 56 | () => conversationInfosQuery.isError || membersQuery.isError, |
| 57 | [conversationInfosQuery.isError, membersQuery.isError] |
| 58 | ); |
| 59 | |
| 60 | const isLoading = useMemo( |
| 61 | () => conversationInfosQuery.isLoading || membersQuery.isLoading, |
| 62 | [conversationInfosQuery.isLoading, membersQuery.isLoading] |
| 63 | ); |
| 64 | |
| 65 | const conversationInfos = conversationInfosQuery.data; |
| 66 | const members = membersQuery.data; |
| 67 | |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 68 | const conversationDisplayName = useConversationDisplayName(account, conversationInfos, members); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 69 | |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 70 | const onComposingStatusChanged = useCallback( |
| 71 | (data: ComposingStatus) => { |
| 72 | // FIXME: data.conversationId is an empty string. Don't know why. Should not be. |
| 73 | // Good enough for now, but will be a problem if the user has more than one conversation with the same contact. |
| 74 | // if (data.conversationId === conversationId) |
| 75 | { |
| 76 | setComposingMembers((composingMembers) => { |
| 77 | if (!data.isWriting) { |
| 78 | return composingMembers.filter(({ contact }) => contact.uri !== data.contactId); |
| 79 | } |
| 80 | |
| 81 | const isAlreadyIncluded = composingMembers.find((member) => member.contact.uri === data.contactId); |
| 82 | if (isAlreadyIncluded) { |
| 83 | return composingMembers; |
| 84 | } |
| 85 | |
| 86 | const member = members?.find((member) => member.contact.uri === data.contactId); |
| 87 | if (!member) { |
| 88 | return composingMembers; |
| 89 | } |
| 90 | |
| 91 | return [...composingMembers, member]; |
| 92 | }); |
| 93 | } |
| 94 | }, |
| 95 | [/*conversationId,*/ members] |
| 96 | ); |
| 97 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 98 | useEffect(() => { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 99 | if (!conversationInfos || !conversationId) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 100 | return; |
| 101 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 102 | |
| 103 | const conversationView: ConversationView = { |
| 104 | conversationId, |
| 105 | }; |
| 106 | |
| 107 | webSocket.send(WebSocketMessageType.ConversationView, conversationView); |
idillon | bad5197 | 2023-01-18 17:00:38 -0500 | [diff] [blame] | 108 | webSocket.bind(WebSocketMessageType.ComposingStatus, onComposingStatusChanged); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 109 | |
idillon | bad5197 | 2023-01-18 17:00:38 -0500 | [diff] [blame] | 110 | return () => webSocket.unbind(WebSocketMessageType.ComposingStatus, onComposingStatusChanged); |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 111 | }, [accountId, conversationInfos, conversationId, onComposingStatusChanged, webSocket]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 112 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 113 | const value = useMemo(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 114 | if (!conversationId || !conversationDisplayName || !conversationInfos || !members) { |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | return { |
| 119 | conversationId, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 120 | conversationDisplayName, |
| 121 | conversationInfos, |
| 122 | members, |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 123 | composingMembers, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 124 | }; |
idillon | 3e378fd | 2022-12-23 11:48:12 -0500 | [diff] [blame] | 125 | }, [conversationId, conversationDisplayName, conversationInfos, members, composingMembers]); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 126 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 127 | if (isLoading) { |
| 128 | return <LoadingPage />; |
| 129 | } |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 130 | if (isError || !value) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 131 | return <div>Error loading conversation: {conversationId}</div>; |
| 132 | } |
| 133 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 134 | return <ConversationContext.Provider value={value}>{children}</ConversationContext.Provider>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 135 | }; |