Skip to content

Commit a8643a7

Browse files
committed
feat: make send message button react to ai state
1 parent 82464bc commit a8643a7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

package/src/components/AITypingIndicatorView/AITypingIndicatorView.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ export const AITypingIndicatorView = <
1919
const { channel: channelFromContext } = useChannelContext();
2020
const channel = channelFromProps || channelFromContext;
2121
const { aiState } = useAIState(channel);
22+
// TODO: Translations
2223
const allowedStates = {
2324
[AIStates.Thinking]: 'Thinking...',
2425
[AIStates.Generating]: 'Generating...',
2526
};
27+
28+
// TODO: Make it not ugly.
2629
return aiState in allowedStates ? (
2730
<View style={{ paddingHorizontal: 16, paddingVertical: 18 }}>
2831
<Text>{allowedStates[aiState]}</Text>

package/src/components/MessageInput/SendButton.tsx

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

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

@@ -12,34 +12,41 @@ import { SendRight } from '../../icons/SendRight';
1212
import { SendUp } from '../../icons/SendUp';
1313

1414
import type { DefaultStreamChatGenerics } from '../../types/types';
15+
import { useChannelContext } from '../../contexts';
16+
import { AIStates, AIStatesEnum, useAIState } from '../AITypingIndicatorView';
17+
import { EventAPIResponse } from 'stream-chat';
1518

1619
type SendButtonPropsWithContext<
1720
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1821
> = Pick<MessageInputContextValue<StreamChatGenerics>, 'giphyActive' | 'sendMessage'> & {
1922
/** Disables the button */ disabled: boolean;
23+
aiState: AIStatesEnum;
24+
stopGenerating: () => Promise<EventAPIResponse<StreamChatGenerics>>;
2025
};
2126

2227
const SendButtonWithContext = <
2328
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2429
>(
2530
props: SendButtonPropsWithContext<StreamChatGenerics>,
2631
) => {
27-
const { disabled = false, giphyActive, sendMessage } = props;
32+
const { disabled = false, giphyActive, sendMessage, aiState, stopGenerating } = props;
2833
const {
2934
theme: {
3035
colors: { accent_blue, grey_gainsboro },
3136
messageInput: { searchIcon, sendButton, sendRightIcon, sendUpIcon },
3237
},
3338
} = useTheme();
3439

40+
const shouldDisplayStopAIGeneration = [AIStates.Thinking, AIStates.Generating].includes(aiState);
41+
3542
return (
3643
<Pressable
3744
disabled={disabled}
38-
onPress={disabled ? () => null : () => sendMessage()}
45+
onPress={disabled ? () => null : shouldDisplayStopAIGeneration ? () => stopGenerating() : () => sendMessage()}
3946
style={[sendButton]}
4047
testID='send-button'
4148
>
42-
{giphyActive ? (
49+
{giphyActive || shouldDisplayStopAIGeneration ? ( // TODO: Fix the icon please.
4350
<Search pathFill={disabled ? grey_gainsboro : accent_blue} {...searchIcon} />
4451
) : disabled ? (
4552
<SendRight fill={grey_gainsboro} size={32} {...sendRightIcon} />
@@ -58,11 +65,13 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
5865
disabled: prevDisabled,
5966
giphyActive: prevGiphyActive,
6067
sendMessage: prevSendMessage,
68+
aiState: prevAiState,
6169
} = prevProps;
6270
const {
6371
disabled: nextDisabled,
6472
giphyActive: nextGiphyActive,
6573
sendMessage: nextSendMessage,
74+
aiState: nextAiState,
6675
} = nextProps;
6776

6877
const disabledEqual = prevDisabled === nextDisabled;
@@ -74,6 +83,9 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
7483
const sendMessageEqual = prevSendMessage === nextSendMessage;
7584
if (!sendMessageEqual) return false;
7685

86+
const aiStateEqual = prevAiState === nextAiState;
87+
if (!aiStateEqual) return false;
88+
7789
return true;
7890
};
7991

@@ -95,12 +107,18 @@ export const SendButton = <
95107
props: SendButtonProps<StreamChatGenerics>,
96108
) => {
97109
const { giphyActive, sendMessage } = useMessageInputContext<StreamChatGenerics>();
110+
const { channel } = useChannelContext<StreamChatGenerics>();
111+
const { aiState } = useAIState(channel);
112+
113+
const stopGenerating = useCallback(() => channel.sendEvent({ type: 'stop_generating', cid: channel.cid }), [channel])
98114

99115
return (
100116
<MemoizedSendButton
101117
{...{ giphyActive, sendMessage }}
102118
{...props}
103119
{...{ disabled: props.disabled || false }}
120+
aiState={aiState}
121+
stopGenerating={stopGenerating}
104122
/>
105123
);
106124
};

0 commit comments

Comments
 (0)