Skip to content

Commit 90bd97c

Browse files
committed
fix: optimize send button performance
1 parent 5660941 commit 90bd97c

File tree

3 files changed

+17
-93
lines changed

3 files changed

+17
-93
lines changed

package/src/components/MessageInput/MoreOptionsButton.tsx

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

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

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

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

package/src/components/MessageInput/SendButton.tsx

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Search } from '../../icons/Search';
1515
import { SendRight } from '../../icons/SendRight';
1616
import { SendUp } from '../../icons/SendUp';
1717

18-
type SendButtonPropsWithContext = Pick<MessageInputContextValue, 'sendMessage'> & {
18+
export type SendButtonProps = Partial<Pick<MessageInputContextValue, 'sendMessage'>> & {
1919
/** Disables the button */
2020
disabled: boolean;
2121
};
@@ -24,8 +24,10 @@ const customComposerDataSelector = (state: CustomDataManagerState) => ({
2424
command: state.custom.command,
2525
});
2626

27-
export const SendButtonWithContext = (props: SendButtonPropsWithContext) => {
28-
const { disabled = false, sendMessage } = props;
27+
export const SendButton = (props: SendButtonProps) => {
28+
const { disabled = false, sendMessage: propsSendMessage } = props;
29+
const { sendMessage: sendMessageFromContext } = useMessageInputContext();
30+
const sendMessage = propsSendMessage || sendMessageFromContext;
2931
const messageComposer = useMessageComposer();
3032
const { customDataManager } = messageComposer;
3133
const { command } = useStateStore(customDataManager.state, customComposerDataSelector);
@@ -61,43 +63,4 @@ export const SendButtonWithContext = (props: SendButtonPropsWithContext) => {
6163
);
6264
};
6365

64-
const areEqual = (prevProps: SendButtonPropsWithContext, nextProps: SendButtonPropsWithContext) => {
65-
const { disabled: prevDisabled, sendMessage: prevSendMessage } = prevProps;
66-
const { disabled: nextDisabled, sendMessage: nextSendMessage } = nextProps;
67-
68-
const disabledEqual = prevDisabled === nextDisabled;
69-
if (!disabledEqual) {
70-
return false;
71-
}
72-
73-
const sendMessageEqual = prevSendMessage === nextSendMessage;
74-
if (!sendMessageEqual) {
75-
return false;
76-
}
77-
78-
return true;
79-
};
80-
81-
const MemoizedSendButton = React.memo(
82-
SendButtonWithContext,
83-
areEqual,
84-
) as typeof SendButtonWithContext;
85-
86-
export type SendButtonProps = Partial<SendButtonPropsWithContext>;
87-
88-
/**
89-
* UI Component for send button in MessageInput component.
90-
*/
91-
export const SendButton = (props: SendButtonProps) => {
92-
const { sendMessage } = useMessageInputContext();
93-
94-
return (
95-
<MemoizedSendButton
96-
{...{ sendMessage }}
97-
{...props}
98-
{...{ disabled: props.disabled || false }}
99-
/>
100-
);
101-
};
102-
10366
SendButton.displayName = 'SendButton{messageInput}';

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

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

13-
type AudioRecordingButtonPropsWithContext = Pick<
13+
type AudioRecordingButtonProps = Pick<
1414
MessageInputContextValue,
1515
'asyncMessagesMinimumPressDuration'
1616
> & {
@@ -40,16 +40,24 @@ type AudioRecordingButtonPropsWithContext = Pick<
4040
startVoiceRecording?: () => Promise<void>;
4141
};
4242

43-
export const AudioRecordingButtonWithContext = (props: AudioRecordingButtonPropsWithContext) => {
43+
/**
44+
* Component to display the mic button on the Message Input.
45+
*/
46+
export const AudioRecordingButton = (props: AudioRecordingButtonProps) => {
4447
const {
45-
asyncMessagesMinimumPressDuration,
48+
asyncMessagesMinimumPressDuration: propAsyncMessagesMinimumPressDuration,
4649
buttonSize,
4750
handleLongPress,
4851
handlePress,
4952
permissionsGranted,
5053
recording,
5154
startVoiceRecording,
5255
} = props;
56+
const { asyncMessagesMinimumPressDuration: contextAsyncMessagesMinimumPressDuration } =
57+
useMessageInputContext();
58+
59+
const asyncMessagesMinimumPressDuration =
60+
propAsyncMessagesMinimumPressDuration || contextAsyncMessagesMinimumPressDuration;
5361

5462
const {
5563
theme: {
@@ -116,51 +124,6 @@ export const AudioRecordingButtonWithContext = (props: AudioRecordingButtonProps
116124
);
117125
};
118126

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-
164127
const styles = StyleSheet.create({
165128
container: {
166129
alignItems: 'center',

0 commit comments

Comments
 (0)