Skip to content

Commit 1cf9027

Browse files
committed
fix: reduce prop surface area from auto complete input
1 parent f6a20b6 commit 1cf9027

File tree

8 files changed

+31
-133
lines changed

8 files changed

+31
-133
lines changed

package/src/components/AutoCompleteInput/AutoCompleteInput.tsx

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
import { CustomDataManagerState, TextComposerState } from 'stream-chat';
1313

14+
import { useChannelContext } from '../../contexts/channelContext/ChannelContext';
1415
import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer';
1516
import {
1617
MessageInputContextValue,
@@ -24,21 +25,16 @@ import {
2425

2526
import { useStateStore } from '../../hooks/useStateStore';
2627

27-
type AutoCompleteInputPropsWithContext = TextInputProps &
28-
Pick<
29-
MessageInputContextValue,
30-
'maxMessageLength' | 'numberOfLines' | 'onChangeText' | 'setInputBoxRef'
31-
> &
32-
Pick<TranslationContextValue, 't'> & {
28+
type AutoCompleteInputProps = TextInputProps &
29+
Partial<Pick<MessageInputContextValue, 'setInputBoxRef'>> &
30+
Partial<Pick<TranslationContextValue, 't'>> & {
3331
/**
3432
* This is currently passed in from MessageInput to avoid rerenders
3533
* that would happen if we put this in the MessageInputContext
3634
*/
3735
cooldownActive?: boolean;
3836
};
3937

40-
export type AutoCompleteInputProps = Partial<AutoCompleteInputPropsWithContext>;
41-
4238
const textComposerStateSelector = (state: TextComposerState) => ({
4339
text: state.text,
4440
});
@@ -47,22 +43,34 @@ const customComposerDataSelector = (state: CustomDataManagerState) => ({
4743
command: state.custom.command,
4844
});
4945

50-
const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext) => {
51-
const {
52-
cooldownActive = false,
53-
maxMessageLength,
54-
numberOfLines,
55-
onChangeText,
56-
setInputBoxRef,
57-
t,
58-
...rest
59-
} = props;
46+
const MAX_NUMBER_OF_LINES = 5;
47+
48+
export const AutoCompleteInput = (props: AutoCompleteInputProps) => {
49+
const { cooldownActive = false, setInputBoxRef: propSetInputBoxRef, t: propT, ...rest } = props;
6050
const [localText, setLocalText] = useState('');
6151
const [textHeight, setTextHeight] = useState(0);
6252
const messageComposer = useMessageComposer();
6353
const { customDataManager, textComposer } = messageComposer;
6454
const { text } = useStateStore(textComposer.state, textComposerStateSelector);
6555
const { command } = useStateStore(customDataManager.state, customComposerDataSelector);
56+
const { channel } = useChannelContext();
57+
const { setInputBoxRef: contextSetInputBoxRef } = useMessageInputContext();
58+
const { t: contextT } = useTranslationContext();
59+
60+
const setInputBoxRef = propSetInputBoxRef || contextSetInputBoxRef;
61+
const t = propT || contextT;
62+
63+
const maxMessageLength = useMemo(() => {
64+
return channel.getConfig()?.max_message_length;
65+
}, [channel]);
66+
67+
const numberOfLines = useMemo(() => {
68+
if (props.numberOfLines) {
69+
return props.numberOfLines;
70+
}
71+
72+
return MAX_NUMBER_OF_LINES;
73+
}, [props.numberOfLines]);
6674

6775
useEffect(() => {
6876
setLocalText(text);
@@ -76,13 +84,8 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
7684
[textComposer],
7785
);
7886

79-
const onTextChangeHandler = useCallback(
87+
const onChangeTextHandler = useCallback(
8088
(newText: string) => {
81-
if (onChangeText) {
82-
onChangeText(newText);
83-
return;
84-
}
85-
8689
setLocalText(newText);
8790

8891
/**
@@ -96,7 +99,7 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
9699
});
97100
}, 0);
98101
},
99-
[textComposer, onChangeText],
102+
[textComposer],
100103
);
101104

102105
const {
@@ -124,7 +127,7 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
124127
autoFocus={!!command}
125128
maxLength={maxMessageLength}
126129
multiline
127-
onChangeText={onTextChangeHandler}
130+
onChangeText={onChangeTextHandler}
128131
onContentSizeChange={handleContentSizeChange}
129132
onSelectionChange={handleSelectionChange}
130133
placeholder={placeholderText}
@@ -146,50 +149,6 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
146149
);
147150
};
148151

149-
const areEqual = (
150-
prevProps: AutoCompleteInputPropsWithContext,
151-
nextProps: AutoCompleteInputPropsWithContext,
152-
) => {
153-
const { cooldownActive: prevCooldownActive, t: prevT } = prevProps;
154-
const { cooldownActive: nextCooldownActive, t: nextT } = nextProps;
155-
156-
const tEqual = prevT === nextT;
157-
if (!tEqual) {
158-
return false;
159-
}
160-
161-
const cooldownActiveEqual = prevCooldownActive === nextCooldownActive;
162-
if (!cooldownActiveEqual) {
163-
return false;
164-
}
165-
166-
return true;
167-
};
168-
169-
const MemoizedAutoCompleteInput = React.memo(
170-
AutoCompleteInputWithContext,
171-
areEqual,
172-
) as typeof AutoCompleteInputWithContext;
173-
174-
export const AutoCompleteInput = (props: AutoCompleteInputProps) => {
175-
const { maxMessageLength, numberOfLines, onChangeText, setInputBoxRef } =
176-
useMessageInputContext();
177-
const { t } = useTranslationContext();
178-
179-
return (
180-
<MemoizedAutoCompleteInput
181-
{...{
182-
maxMessageLength,
183-
numberOfLines,
184-
onChangeText,
185-
setInputBoxRef,
186-
t,
187-
}}
188-
{...props}
189-
/>
190-
);
191-
};
192-
193152
const styles = StyleSheet.create({
194153
inputBox: {
195154
flex: 1,

package/src/components/Channel/Channel.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ export type ChannelPropsWithContext = Pick<ChannelContextValue, 'channel'> &
436436
* Boolean flag to enable/disable marking the channel as read on mount
437437
*/
438438
markReadOnMount?: boolean;
439-
maxMessageLength?: number;
440439
/**
441440
* Load the channel at a specified message instead of the most recent message.
442441
*/
@@ -567,7 +566,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
567566
loadingMoreRecent: loadingMoreRecentProp,
568567
markdownRules,
569568
markReadOnMount = true,
570-
maxMessageLength: maxMessageLengthProp,
571569
maxNumberOfFiles = 10,
572570
maxTimeBetweenGroupedMessages,
573571
mentionAllAppUsersEnabled = false,
@@ -616,8 +614,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
616614
NetworkDownIndicator = NetworkDownIndicatorDefault,
617615
// TODO: Think about this one
618616
newMessageStateUpdateThrottleInterval = defaultThrottleInterval,
619-
numberOfLines = 5,
620-
onChangeText,
621617
onLongPressMessage,
622618
onPressInMessage,
623619
onPressMessage,
@@ -1789,13 +1785,10 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
17891785
InputButtons,
17901786
InputEditingStateHeader,
17911787
InputReplyStateHeader,
1792-
maxMessageLength: maxMessageLengthProp ?? clientChannelConfig?.max_message_length ?? undefined,
17931788
maxNumberOfFiles,
17941789
mentionAllAppUsersEnabled,
17951790
mentionAllAppUsersQuery,
17961791
MoreOptionsButton,
1797-
numberOfLines,
1798-
onChangeText,
17991792
openPollCreationDialog,
18001793
SendButton,
18011794
sendImageAsync,

package/src/components/Channel/hooks/useCreateInputMessageInputContext.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ export const useCreateInputMessageInputContext = ({
4343
InputButtons,
4444
InputEditingStateHeader,
4545
InputReplyStateHeader,
46-
maxMessageLength,
4746
maxNumberOfFiles,
4847
mentionAllAppUsersEnabled,
4948
mentionAllAppUsersQuery,
5049
MoreOptionsButton,
51-
numberOfLines,
52-
onChangeText,
5350
openPollCreationDialog,
5451
SendButton,
5552
sendImageAsync,
@@ -110,13 +107,10 @@ export const useCreateInputMessageInputContext = ({
110107
InputButtons,
111108
InputEditingStateHeader,
112109
InputReplyStateHeader,
113-
maxMessageLength,
114110
maxNumberOfFiles,
115111
mentionAllAppUsersEnabled,
116112
mentionAllAppUsersQuery,
117113
MoreOptionsButton,
118-
numberOfLines,
119-
onChangeText,
120114
openPollCreationDialog,
121115
SendButton,
122116
sendImageAsync,
@@ -135,7 +129,6 @@ export const useCreateInputMessageInputContext = ({
135129
channelId,
136130
editingDep,
137131
initialValue,
138-
maxMessageLength,
139132
CreatePollContent,
140133
showPollCreationDialog,
141134
],

package/src/components/Message/hooks/useMessageActionHandlers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export const useMessageActionHandlers = ({
106106

107107
const handleEditMessage = () => {
108108
setEditingState(message);
109-
messageComposer.setQuotedMessage(null);
110109
};
111110

112111
const handleFlagMessage = () => {

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ type MessageInputPropsWithContext = Pick<
145145
| 'SendButton'
146146
| 'sending'
147147
| 'sendMessageAsync'
148-
| 'setShowMoreOptions'
149-
| 'showMoreOptions'
150148
| 'ShowThreadMessageInChannelButton'
151149
| 'StartAudioRecordingButton'
152150
| 'removeFile'
@@ -906,7 +904,6 @@ const areEqual = (
906904
isValidMessage: prevIsValidMessage,
907905
openPollCreationDialog: prevOpenPollCreationDialog,
908906
sending: prevSending,
909-
showMoreOptions: prevShowMoreOptions,
910907
showPollCreationDialog: prevShowPollCreationDialog,
911908
t: prevT,
912909
thread: prevThread,
@@ -928,7 +925,6 @@ const areEqual = (
928925
isValidMessage: nextIsValidMessage,
929926
openPollCreationDialog: nextOpenPollCreationDialog,
930927
sending: nextSending,
931-
showMoreOptions: nextShowMoreOptions,
932928
showPollCreationDialog: nextShowPollCreationDialog,
933929
t: nextT,
934930
thread: nextThread,
@@ -997,11 +993,6 @@ const areEqual = (
997993
return false;
998994
}
999995

1000-
const showMoreOptionsEqual = prevShowMoreOptions === nextShowMoreOptions;
1001-
if (!showMoreOptionsEqual) {
1002-
return false;
1003-
}
1004-
1005996
const isOnlineEqual = prevIsOnline === nextIsOnline;
1006997
if (!isOnlineEqual) {
1007998
return false;
@@ -1110,8 +1101,6 @@ export const MessageInput = (props: MessageInputProps) => {
11101101
sendMessage,
11111102
sendMessageAsync,
11121103
SendMessageDisallowedIndicator,
1113-
setShowMoreOptions,
1114-
showMoreOptions,
11151104
showPollCreationDialog,
11161105
ShowThreadMessageInChannelButton,
11171106
StartAudioRecordingButton,
@@ -1186,8 +1175,6 @@ export const MessageInput = (props: MessageInputProps) => {
11861175
sendMessage,
11871176
sendMessageAsync,
11881177
SendMessageDisallowedIndicator,
1189-
setShowMoreOptions,
1190-
showMoreOptions,
11911178
showPollCreationDialog,
11921179
ShowThreadMessageInChannelButton,
11931180
StartAudioRecordingButton,

package/src/components/MessageInput/components/CommandInput.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import { AutoCompleteInput } from '../../AutoCompleteInput/AutoCompleteInput';
1717
import { useCountdown } from '../hooks/useCountdown';
1818

1919
export type CommandInputProps = Partial<
20-
Pick<
21-
MessageInputContextValue,
22-
'additionalTextInputProps' | 'cooldownEndsAt' | 'setShowMoreOptions'
23-
>
20+
Pick<MessageInputContextValue, 'additionalTextInputProps' | 'cooldownEndsAt'>
2421
> & {
2522
disabled: boolean;
2623
};
@@ -32,16 +29,13 @@ const customComposerDataSelector = (state: CustomDataManagerState) => ({
3229
export const CommandInput = ({
3330
cooldownEndsAt: propCooldownEndsAt,
3431
disabled,
35-
setShowMoreOptions: propSetShowMoreOptions,
3632
}: CommandInputProps) => {
37-
const { cooldownEndsAt: contextCooldownEndsAt, setShowMoreOptions: contextSetShowMoreOptions } =
38-
useMessageInputContext();
33+
const { cooldownEndsAt: contextCooldownEndsAt } = useMessageInputContext();
3934
const messageComposer = useMessageComposer();
4035
const { customDataManager } = messageComposer;
4136
const { command } = useStateStore(customDataManager.state, customComposerDataSelector);
4237

4338
const cooldownEndsAt = propCooldownEndsAt || contextCooldownEndsAt;
44-
const setShowMoreOptions = propSetShowMoreOptions || contextSetShowMoreOptions;
4539

4640
const { seconds: cooldownRemainingSeconds } = useCountdown(cooldownEndsAt);
4741

@@ -57,8 +51,6 @@ export const CommandInput = ({
5751

5852
const onCloseHandler = () => {
5953
customDataManager.setCustomData({ command: null });
60-
// TODO: Rethink setShowMoreOptions
61-
setShowMoreOptions(true);
6254
};
6355

6456
if (!command) {

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ export type LocalMessageInputContext = {
208208
setInputBoxRef: LegacyRef<TextInput> | undefined;
209209
setNumberOfUploads: React.Dispatch<React.SetStateAction<number>>;
210210
setSendThreadMessageInChannel: React.Dispatch<React.SetStateAction<boolean>>;
211-
setShowMoreOptions: React.Dispatch<React.SetStateAction<boolean>>;
212-
showMoreOptions: boolean;
213211
/**
214212
* Function for taking a photo and uploading it
215213
*/
@@ -340,8 +338,6 @@ export type InputMessageInputContextValue = {
340338
*/
341339
MoreOptionsButton: React.ComponentType<MoreOptionsButtonProps>;
342340

343-
/** Limit on the number of lines in the text input before scrolling */
344-
numberOfLines: number;
345341
/**
346342
* Custom UI component for send button.
347343
*
@@ -457,14 +453,9 @@ export type InputMessageInputContextValue = {
457453
* - toggleAttachmentPicker
458454
*/
459455
InputButtons?: React.ComponentType<InputButtonsProps>;
460-
maxMessageLength?: number;
461456
/** Object containing filters/sort/options overrides for an @mention user query */
462457
mentionAllAppUsersEnabled?: boolean;
463458
mentionAllAppUsersQuery?: MentionAllAppUsersQuery;
464-
/**
465-
* Callback that is called when the text input's text changes. Changed text is passed as a single string argument to the callback handler.
466-
*/
467-
onChangeText?: (newText: string) => void;
468459
openPollCreationDialog?: ({ sendMessage }: Pick<LocalMessageInputContext, 'sendMessage'>) => void;
469460
SendMessageDisallowedIndicator?: React.ComponentType;
470461
/**
@@ -562,8 +553,6 @@ export const MessageInputProvider = ({
562553
setFileUploads,
563554
setImageUploads,
564555
setNumberOfUploads,
565-
setShowMoreOptions,
566-
showMoreOptions,
567556
} = useMessageDetailsForState(editing, initialValue);
568557
const { endsAt: cooldownEndsAt, start: startCooldown } = useCooldown();
569558

@@ -1409,8 +1398,6 @@ export const MessageInputProvider = ({
14091398
setInputBoxRef,
14101399
setNumberOfUploads,
14111400
setSendThreadMessageInChannel,
1412-
setShowMoreOptions,
1413-
showMoreOptions,
14141401
takeAndUploadImage,
14151402
thread,
14161403
toggleAttachmentPicker,

0 commit comments

Comments
 (0)