1- import { useMemo } from 'react' ;
2- import { Alert } from 'react-native' ;
1+ import { useEffect , useMemo , useRef } from 'react' ;
2+ import { Alert , EventSubscription , Keyboard , Platform } from 'react-native' ;
33
44import { UserResponse } from 'stream-chat' ;
55
@@ -9,12 +9,15 @@ import type { ChannelContextValue } from '../../../contexts/channelContext/Chann
99import type { ChatContextValue } from '../../../contexts/chatContext/ChatContext' ;
1010import { MessageComposerAPIContextValue } from '../../../contexts/messageComposerContext/MessageComposerAPIContext' ;
1111import type { MessageContextValue } from '../../../contexts/messageContext/MessageContext' ;
12+ import { useMessageInputContext } from '../../../contexts/messageInputContext/MessageInputContext' ;
1213import type { MessagesContextValue } from '../../../contexts/messagesContext/MessagesContext' ;
1314
1415import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext' ;
1516import { useStableCallback } from '../../../hooks' ;
17+ import { useKeyboardVisibility } from '../../../hooks/useKeyboardVisibility' ;
1618import { useTranslatedMessage } from '../../../hooks/useTranslatedMessage' ;
1719import { NativeHandlers } from '../../../native' ;
20+ import { KeyboardControllerPackage } from '../../KeyboardCompatibleView/KeyboardControllerAvoidingView' ;
1821
1922export const useMessageActionHandlers = ( {
2023 channel,
@@ -37,9 +40,19 @@ export const useMessageActionHandlers = ({
3740 const { t } = useTranslationContext ( ) ;
3841 const handleResendMessage = useStableCallback ( ( ) => retrySendMessage ( message ) ) ;
3942 const translatedMessage = useTranslatedMessage ( message ) ;
43+ const { inputBoxRef } = useMessageInputContext ( ) ;
44+ const isKeyboardVisible = useKeyboardVisibility ( ) ;
45+ const keyboardDidShowSubscriptionRef = useRef < EventSubscription | undefined > ( undefined ) ;
4046
4147 const isMuted = useUserMuteActive ( message . user ) ;
4248
49+ const clearKeyboardDidShowSubscription = useStableCallback ( ( ) => {
50+ keyboardDidShowSubscriptionRef . current ?. remove ( ) ;
51+ keyboardDidShowSubscriptionRef . current = undefined ;
52+ } ) ;
53+
54+ useEffect ( ( ) => clearKeyboardDidShowSubscription , [ clearKeyboardDidShowSubscription ] ) ;
55+
4356 const handleQuotedReplyMessage = useStableCallback ( ( ) => {
4457 setQuotedMessage ( message ) ;
4558 } ) ;
@@ -115,7 +128,38 @@ export const useMessageActionHandlers = ({
115128 } ) ;
116129
117130 const handleEditMessage = useStableCallback ( ( ) => {
118- setEditingState ( message ) ;
131+ requestAnimationFrame ( ( ) =>
132+ requestAnimationFrame ( ( ) => {
133+ clearKeyboardDidShowSubscription ( ) ;
134+
135+ const openEditingState = ( ) => {
136+ clearKeyboardDidShowSubscription ( ) ;
137+ setEditingState ( message ) ;
138+ } ;
139+
140+ if ( ! inputBoxRef . current ) {
141+ openEditingState ( ) ;
142+ return ;
143+ }
144+
145+ if ( isKeyboardVisible ) {
146+ inputBoxRef . current ?. focus ( ) ;
147+ openEditingState ( ) ;
148+ return ;
149+ }
150+
151+ const event = Platform . OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow' ;
152+
153+ keyboardDidShowSubscriptionRef . current = KeyboardControllerPackage ?. KeyboardEvents
154+ ? KeyboardControllerPackage . KeyboardEvents . addListener (
155+ 'keyboardDidShow' ,
156+ openEditingState ,
157+ )
158+ : Keyboard . addListener ( event , openEditingState ) ;
159+
160+ inputBoxRef . current ?. focus ( ) ;
161+ } ) ,
162+ ) ;
119163 } ) ;
120164
121165 const handleFlagMessage = useStableCallback ( ( ) => {
0 commit comments