blob: 538d0e0a4c20b18b3575007787853d0dbae9ec6a [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simond47ef9e2022-09-28 22:24:28 -040018import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
idillon07d31cc2022-12-06 22:40:14 -050019import { ConversationInfos, IConversationMember, IConversationSummary, Message } from 'jami-web-common';
20import { useCallback } from 'react';
idillon08f77172022-09-13 19:14:17 -040021
simon5da8ca62022-11-09 15:21:25 -050022import { useAuthContext } from '../contexts/AuthProvider';
idillon07d31cc2022-12-06 22:40:14 -050023import { ConversationMember } from '../models/conversation-member';
simon5da8ca62022-11-09 15:21:25 -050024
idillon07d31cc2022-12-06 22:40:14 -050025export const useConversationInfosQuery = (conversationId?: string) => {
simon94fe53e2022-11-10 12:51:58 -050026 const { axiosInstance } = useAuthContext();
idillon07d31cc2022-12-06 22:40:14 -050027 return useQuery(
28 ['conversations', conversationId],
simon94fe53e2022-11-10 12:51:58 -050029 async () => {
idillon07d31cc2022-12-06 22:40:14 -050030 const { data } = await axiosInstance.get<ConversationInfos>(`/conversations/${conversationId}/infos`);
simon94fe53e2022-11-10 12:51:58 -050031 return data;
32 },
33 {
34 enabled: !!conversationId,
35 }
36 );
idillon07d31cc2022-12-06 22:40:14 -050037};
simone35acc22022-12-02 16:51:12 -050038
idillon07d31cc2022-12-06 22:40:14 -050039export const useConversationsSummariesQuery = () => {
40 const { axiosInstance } = useAuthContext();
41 return useQuery(['conversations', 'summaries'], async () => {
42 const { data } = await axiosInstance.get<IConversationSummary[]>(`/conversations`);
43 return data;
44 });
45};
46
47export const useRefreshConversationsSummaries = () => {
48 const queryClient = useQueryClient();
49 return useCallback(() => {
50 queryClient.invalidateQueries(['conversations', 'summaries']);
51 }, [queryClient]);
52};
53
54export const useMembersQuery = (conversationId?: string) => {
55 const { axiosInstance } = useAuthContext();
56 return useQuery(
57 ['conversations', conversationId, 'members'],
58 async () => {
59 const { data } = await axiosInstance.get<IConversationMember[]>(`/conversations/${conversationId}/members`);
60 return data.map((item) => ConversationMember.fromInterface(item));
61 },
62 {
63 enabled: !!conversationId,
simone35acc22022-12-02 16:51:12 -050064 }
idillon07d31cc2022-12-06 22:40:14 -050065 );
simond47ef9e2022-09-28 22:24:28 -040066};
idillon08f77172022-09-13 19:14:17 -040067
simon5da8ca62022-11-09 15:21:25 -050068export const useMessagesQuery = (conversationId: string) => {
simon94fe53e2022-11-10 12:51:58 -050069 const { axiosInstance } = useAuthContext();
70 return useQuery(
idillon07d31cc2022-12-06 22:40:14 -050071 ['conversations', conversationId, 'messages'],
simon94fe53e2022-11-10 12:51:58 -050072 async () => {
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050073 const { data } = await axiosInstance.get<Message[]>(`/conversations/${conversationId}/messages`);
simon94fe53e2022-11-10 12:51:58 -050074 return data;
75 },
76 {
77 enabled: !!conversationId,
78 }
79 );
simond47ef9e2022-09-28 22:24:28 -040080};
idillon08f77172022-09-13 19:14:17 -040081
simon5da8ca62022-11-09 15:21:25 -050082export const useSendMessageMutation = (conversationId: string) => {
simon94fe53e2022-11-10 12:51:58 -050083 const { axiosInstance } = useAuthContext();
simond47ef9e2022-09-28 22:24:28 -040084 const queryClient = useQueryClient();
85 return useMutation(
simon94fe53e2022-11-10 12:51:58 -050086 (message: string) => axiosInstance.post(`/conversations/${conversationId}/messages`, { message }),
simond47ef9e2022-09-28 22:24:28 -040087 {
simon5da8ca62022-11-09 15:21:25 -050088 onSuccess: () => queryClient.invalidateQueries(['messages', conversationId]),
simond47ef9e2022-09-28 22:24:28 -040089 }
90 );
91};