From 853c109b942260341023ad377f335cd55adb1cb4 Mon Sep 17 00:00:00 2001 From: Piotr Suwala Date: Fri, 15 Nov 2024 09:18:06 +0100 Subject: [PATCH] feat(samples): stopping typing callback example --- .../screens/ordinary/chat/Chat.tsx | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/samples/react-native-group-chat/screens/ordinary/chat/Chat.tsx b/samples/react-native-group-chat/screens/ordinary/chat/Chat.tsx index 0ee1c11..b09fa7b 100644 --- a/samples/react-native-group-chat/screens/ordinary/chat/Chat.tsx +++ b/samples/react-native-group-chat/screens/ordinary/chat/Chat.tsx @@ -17,11 +17,13 @@ import { EnhancedIMessage, mapPNMessageToGChatMessage } from "../../../utils" import { ChatContext } from "../../../context" import { HomeStackParamList } from "../../../types" import { Avatar, useActionsMenu } from "../../../components" -import { colorPalette as colors, Text } from "../../../ui-components" +import { Button, colorPalette as colors, Text } from "../../../ui-components" import { useNavigation } from "@react-navigation/native" import { useCommonChatRenderers } from "../../../hooks" import { MaterialCommunityIcons, MaterialIcons } from "@expo/vector-icons" +let stopTypingCallback = () => {} + export function ChatScreen({}: StackScreenProps) { const { chat, setCurrentChannel, currentChannel, getUser, currentChannelMembers } = useContext(ChatContext) @@ -186,12 +188,6 @@ export function ChatScreen({}: StackScreenProps) { }) ) - if (currentChannel.type !== "public") { - currentChannel.getTyping((value) => { - setTypingData(value) - }) - } - setGiftedChatMappedMessages((msgs) => GiftedChat.prepend( [], @@ -224,6 +220,19 @@ export function ChatScreen({}: StackScreenProps) { switchChannelImplementation() }, [currentChannel, currentChannelMembership]) + useEffect(() => { + if (!currentChannel) { + return + } + + if (currentChannel.type !== "public") { + stopTypingCallback = currentChannel.getTyping((value) => { + setTypingData(value) + }) + return stopTypingCallback + } + }, [currentChannel]) + useEffect(() => { if (!currentChannel) { return @@ -408,6 +417,13 @@ export function ChatScreen({}: StackScreenProps) { messageContainerRef={giftedChatRef} renderActions={renderActions} /> + )