diff --git a/src/views/account/Chat/Messages.tsx b/src/views/account/Chat/Messages.tsx index 0f9fc0465..cbc299b70 100644 --- a/src/views/account/Chat/Messages.tsx +++ b/src/views/account/Chat/Messages.tsx @@ -1,4 +1,9 @@ -import React, { useEffect, useLayoutEffect, useState } from "react"; +import React, { + useCallback, + useEffect, + useLayoutEffect, + useState, +} from "react"; import { ScrollView, LogBox, @@ -34,6 +39,7 @@ import PapillonHeader from "@/components/Global/PapillonHeader"; import { SquarePen } from "lucide-react-native"; import { TouchableOpacity } from "react-native-gesture-handler"; import InsetsBottomView from "@/components/Global/InsetsBottomView"; +import { TabLocation } from "pawnote"; // Voir la documentation de `react-navigation`. // @@ -46,13 +52,17 @@ LogBox.ignoreLogs([ const Discussions: Screen<"Discussions"> = ({ navigation, route }) => { const theme = useTheme(); + const { colors } = theme; const account = useCurrentAccount((state) => state.account!); + const [chats, setChats] = useState(null); const [refreshing, setRefreshing] = useState(false); - const getChatCreator = (chat: Chat) => chat.creator === account.name ? chat.recipient : chat.creator; + const supported = account.service === AccountService.Pronote; + + const enabled = supported && account.instance?.user.resources[0].tabs.get(TabLocation.Discussions); useLayoutEffect(() => { navigation.setOptions({ @@ -62,148 +72,176 @@ const Discussions: Screen<"Discussions"> = ({ navigation, route }) => { useEffect(() => { void (async () => { - const chats = await getChats(account); - setChats(chats); + await fetchChats(); })(); }, [account?.instance]); const onRefresh = async () => { setRefreshing(true); - const chats = await getChats(account); - setChats(chats); + await fetchChats(); setRefreshing(false); }; - if (account.service !== AccountService.Pronote) - return ( - - - - ); + const fetchChats = useCallback(async () => { + if (!enabled || !supported) { + return; + } + + try { + const chats = await getChats(account); + setChats(chats); + } catch (e) { + console.error("Erreur lors du chargement des discussions :", e); + } + }, [enabled, supported]); + + const getChatCreator = useCallback( + (chat: Chat) => chat.creator === account.name ? chat.recipient : chat.creator, + [account.name] + ); return ( <> - navigation.navigate("ChatCreate")}> - Composer - - - - } - > - {!chats ? ( - navigation.navigate("ChatCreate")} > - - - - Chargement des discussions... - - - Composer + + + )} + + {!supported || !enabled ? ( + + {!supported ? ( + + ) : !enabled && ( + + )} + + ) : ( + } + > + {!chats ? ( + - Vos conversations arrivent... - - - ) : chats.length === 0 ? ( - - ) : ( - - {chats.map((chat) => ( - navigation.navigate("Chat", { handle: chat })} - leading={ - - } + + - - {!chat.read && ( - + + Vos conversations arrivent... + + + ) : chats.length === 0 ? ( + + ) : ( + + {chats.map((chat) => ( + navigation.navigate("Chat", { handle: chat })} + leading={ + - )} - {getChatCreator(chat)} - - {chat.subject || "Aucun sujet"} - Il y a {Math.floor((new Date().getTime() - new Date(chat.date).getTime()) / (1000 * 60 * 60 * 24))} jours - - ))} - - )} - - - + } + > + + {!chat.read && ( + + )} + {getChatCreator(chat)} + + {chat.subject || "Aucun sujet"} + Il y a {Math.floor((new Date().getTime() - new Date(chat.date).getTime()) / (1000 * 60 * 60 * 24))} jours + + ))} + + )} + + + )} ); };