Skip to content

Commit 9ce715e

Browse files
committed
revert: the memo change
1 parent e25b355 commit 9ce715e

File tree

3 files changed

+116
-29
lines changed

3 files changed

+116
-29
lines changed

package/src/components/MessageInput/MoreOptionsButton.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type MoreOptionsButtonProps = {
99
handleOnPress?: () => void;
1010
};
1111

12-
export const MoreOptionsButton = (props: MoreOptionsButtonProps) => {
12+
export const UnMemoizedMoreOptionsButton = (props: MoreOptionsButtonProps) => {
1313
const { handleOnPress } = props;
1414

1515
const {
@@ -31,4 +31,6 @@ export const MoreOptionsButton = (props: MoreOptionsButtonProps) => {
3131
);
3232
};
3333

34+
export const MoreOptionsButton = React.memo(UnMemoizedMoreOptionsButton);
35+
3436
MoreOptionsButton.displayName = 'MoreOptionsButton{messageInput}';

package/src/components/MessageInput/SendButton.tsx

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback } from 'react';
1+
import React, { Profiler, useCallback } from 'react';
22

33
import { Pressable } from 'react-native';
44

@@ -15,7 +15,7 @@ import { Search } from '../../icons/Search';
1515
import { SendRight } from '../../icons/SendRight';
1616
import { 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

package/src/components/MessageInput/components/AudioRecorder/AudioRecordingButton.tsx

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { useTranslationContext } from '../../../../contexts/translationContext/T
1010
import { Mic } from '../../../../icons/Mic';
1111
import { AudioRecordingReturnType, NativeHandlers } from '../../../../native';
1212

13-
export type AudioRecordingButtonProps = Partial<
14-
Pick<MessageInputContextValue, 'asyncMessagesMinimumPressDuration'>
13+
type AudioRecordingButtonPropsWithContext = Pick<
14+
MessageInputContextValue,
15+
'asyncMessagesMinimumPressDuration'
1516
> & {
1617
/**
1718
* The current voice recording that is in progress.
@@ -39,21 +40,16 @@ export type AudioRecordingButtonProps = Partial<
3940
startVoiceRecording?: () => Promise<void>;
4041
};
4142

42-
export const AudioRecordingButton = (props: AudioRecordingButtonProps) => {
43+
export const AudioRecordingButtonWithContext = (props: AudioRecordingButtonPropsWithContext) => {
4344
const {
44-
asyncMessagesMinimumPressDuration: propsAsyncMessagesMinimumPressDuration,
45+
asyncMessagesMinimumPressDuration,
4546
buttonSize,
4647
handleLongPress,
4748
handlePress,
4849
permissionsGranted,
4950
recording,
5051
startVoiceRecording,
5152
} = props;
52-
const { asyncMessagesMinimumPressDuration: contextAsyncMessagesMinimumPressDuration } =
53-
useMessageInputContext();
54-
55-
const asyncMessagesMinimumPressDuration =
56-
propsAsyncMessagesMinimumPressDuration || contextAsyncMessagesMinimumPressDuration;
5753

5854
const {
5955
theme: {
@@ -120,6 +116,51 @@ export const AudioRecordingButton = (props: AudioRecordingButtonProps) => {
120116
);
121117
};
122118

119+
const areEqual = (
120+
prevProps: AudioRecordingButtonPropsWithContext,
121+
nextProps: AudioRecordingButtonPropsWithContext,
122+
) => {
123+
const {
124+
asyncMessagesMinimumPressDuration: prevAsyncMessagesMinimumPressDuration,
125+
recording: prevRecording,
126+
} = prevProps;
127+
const {
128+
asyncMessagesMinimumPressDuration: nextAsyncMessagesMinimumPressDuration,
129+
recording: nextRecording,
130+
} = nextProps;
131+
132+
const asyncMessagesMinimumPressDurationEqual =
133+
prevAsyncMessagesMinimumPressDuration === nextAsyncMessagesMinimumPressDuration;
134+
if (!asyncMessagesMinimumPressDurationEqual) {
135+
return false;
136+
}
137+
138+
const recordingEqual = prevRecording === nextRecording;
139+
if (!recordingEqual) {
140+
return false;
141+
}
142+
143+
return true;
144+
};
145+
146+
const MemoizedAudioRecordingButton = React.memo(
147+
AudioRecordingButtonWithContext,
148+
areEqual,
149+
) as typeof AudioRecordingButtonWithContext;
150+
151+
export type AudioRecordingButtonProps = Partial<AudioRecordingButtonPropsWithContext> & {
152+
recording: AudioRecordingReturnType;
153+
};
154+
155+
/**
156+
* Component to display the mic button on the Message Input.
157+
*/
158+
export const AudioRecordingButton = (props: AudioRecordingButtonProps) => {
159+
const { asyncMessagesMinimumPressDuration } = useMessageInputContext();
160+
161+
return <MemoizedAudioRecordingButton {...{ asyncMessagesMinimumPressDuration }} {...props} />;
162+
};
163+
123164
const styles = StyleSheet.create({
124165
container: {
125166
alignItems: 'center',

0 commit comments

Comments
 (0)