1- import React , { useCallback } from 'react' ;
1+ import React , { Profiler , useCallback } from 'react' ;
22
33import { Pressable } from 'react-native' ;
44
@@ -15,7 +15,7 @@ import { Search } from '../../icons/Search';
1515import { SendRight } from '../../icons/SendRight' ;
1616import { SendUp } from '../../icons/SendUp' ;
1717
18- export type SendButtonProps = Partial < Pick < MessageInputContextValue , 'sendMessage' > > & {
18+ type SendButtonPropsWithContext = Pick < MessageInputContextValue , 'sendMessage' > & {
1919 /** Disables the button */
2020 disabled : boolean ;
2121} ;
@@ -24,10 +24,8 @@ const customComposerDataSelector = (state: CustomDataManagerState) => ({
2424 command : state . custom . command ,
2525} ) ;
2626
27- export const SendButton = ( props : SendButtonProps ) => {
28- const { disabled = false , sendMessage : propsSendMessage } = props ;
29- const { sendMessage : sendMessageFromContext } = useMessageInputContext ( ) ;
30- const sendMessage = propsSendMessage || sendMessageFromContext ;
27+ export const SendButtonWithContext = ( props : SendButtonPropsWithContext ) => {
28+ const { disabled = false , sendMessage } = props ;
3129 const messageComposer = useMessageComposer ( ) ;
3230 const { customDataManager } = messageComposer ;
3331 const { command } = useStateStore ( customDataManager . state , customComposerDataSelector ) ;
@@ -46,20 +44,66 @@ export const SendButton = (props: SendButtonProps) => {
4644 } , [ disabled , sendMessage ] ) ;
4745
4846 return (
49- < Pressable
50- disabled = { disabled }
51- onPress = { onPressHandler }
52- style = { [ sendButton ] }
53- testID = 'send-button'
47+ < Profiler
48+ id = 'Send Button'
49+ onRender = { ( id , phase , actualDuration , baseDuration , start , end ) => {
50+ console . log ( { actualDuration , baseDuration , end , id , phase , start } ) ;
51+ } }
5452 >
55- { command ? (
56- < Search pathFill = { disabled ? grey_gainsboro : accent_blue } { ...searchIcon } />
57- ) : disabled ? (
58- < SendRight fill = { grey_gainsboro } size = { 32 } { ...sendRightIcon } />
59- ) : (
60- < SendUp fill = { accent_blue } size = { 32 } { ...sendUpIcon } />
61- ) }
62- </ Pressable >
53+ < Pressable
54+ disabled = { disabled }
55+ onPress = { onPressHandler }
56+ style = { [ sendButton ] }
57+ testID = 'send-button'
58+ >
59+ { command ? (
60+ < Search pathFill = { disabled ? grey_gainsboro : accent_blue } { ...searchIcon } />
61+ ) : disabled ? (
62+ < SendRight fill = { grey_gainsboro } size = { 32 } { ...sendRightIcon } />
63+ ) : (
64+ < SendUp fill = { accent_blue } size = { 32 } { ...sendUpIcon } />
65+ ) }
66+ </ Pressable >
67+ </ Profiler >
68+ ) ;
69+ } ;
70+
71+ const areEqual = ( prevProps : SendButtonPropsWithContext , nextProps : SendButtonPropsWithContext ) => {
72+ const { disabled : prevDisabled , sendMessage : prevSendMessage } = prevProps ;
73+ const { disabled : nextDisabled , sendMessage : nextSendMessage } = nextProps ;
74+
75+ const disabledEqual = prevDisabled === nextDisabled ;
76+ if ( ! disabledEqual ) {
77+ return false ;
78+ }
79+
80+ const sendMessageEqual = prevSendMessage === nextSendMessage ;
81+ if ( ! sendMessageEqual ) {
82+ return false ;
83+ }
84+
85+ return true ;
86+ } ;
87+
88+ const MemoizedSendButton = React . memo (
89+ SendButtonWithContext ,
90+ areEqual ,
91+ ) as typeof SendButtonWithContext ;
92+
93+ export type SendButtonProps = Partial < SendButtonPropsWithContext > ;
94+
95+ /**
96+ * UI Component for send button in MessageInput component.
97+ */
98+ export const SendButton = ( props : SendButtonProps ) => {
99+ const { sendMessage } = useMessageInputContext ( ) ;
100+
101+ return (
102+ < MemoizedSendButton
103+ { ...{ sendMessage } }
104+ { ...props }
105+ { ...{ disabled : props . disabled || false } }
106+ />
63107 ) ;
64108} ;
65109
0 commit comments