Skip to content

Commit 27b9b78

Browse files
committed
fix: bug with stop streaming button and types
1 parent 2879ed2 commit 27b9b78

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

package/src/components/AITypingIndicatorView/hooks/useAIState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from 'react';
22

3-
import { Channel } from 'stream-chat';
3+
import { Channel, Event } from 'stream-chat';
44

55
import type { DefaultStreamChatGenerics } from '../../../types/types';
66

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useState } from 'react';
1+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
22
import {
33
Modal,
44
NativeSyntheticEvent,
@@ -28,6 +28,8 @@ import type { UserResponse } from 'stream-chat';
2828
import { useAudioController } from './hooks/useAudioController';
2929
import { useCountdown } from './hooks/useCountdown';
3030

31+
import { StopMessageStreamingButton } from './StopMessageStreamingButton';
32+
3133
import { ChatContextValue, useChatContext, useOwnCapabilitiesContext } from '../../contexts';
3234
import {
3335
AttachmentPickerContextValue,
@@ -58,6 +60,7 @@ import {
5860

5961
import { isImageMediaLibraryAvailable, triggerHaptic } from '../../native';
6062
import type { Asset, DefaultStreamChatGenerics } from '../../types/types';
63+
import { AIStates, useAIState } from '../AITypingIndicatorView';
6164
import { AutoCompleteInput } from '../AutoCompleteInput/AutoCompleteInput';
6265
import { CreatePoll } from '../Poll/CreatePollContent';
6366

@@ -727,6 +730,15 @@ const MessageInputWithContext = <
727730
})),
728731
};
729732

733+
const { channel } = useChannelContext<StreamChatGenerics>();
734+
const { aiState } = useAIState(channel);
735+
736+
const stopGenerating = useCallback(
737+
() => channel.sendEvent({ type: 'stop_generating' }),
738+
[channel],
739+
);
740+
const shouldDisplayStopAIGeneration = [AIStates.Thinking, AIStates.Generating].includes(aiState);
741+
730742
return (
731743
<>
732744
<View
@@ -832,7 +844,10 @@ const MessageInputWithContext = <
832844
</>
833845
)}
834846

835-
{isSendingButtonVisible() &&
847+
{shouldDisplayStopAIGeneration ? (
848+
<StopMessageStreamingButton onPress={stopGenerating} />
849+
) : (
850+
isSendingButtonVisible() &&
836851
(cooldownRemainingSeconds ? (
837852
<CooldownTimer seconds={cooldownRemainingSeconds} />
838853
) : (
@@ -841,7 +856,8 @@ const MessageInputWithContext = <
841856
disabled={sending.current || !isValidMessage() || (giphyActive && !isOnline)}
842857
/>
843858
</View>
844-
))}
859+
))
860+
)}
845861
{audioRecordingEnabled && !micLocked && (
846862
<GestureDetector gesture={panGestureMic}>
847863
<Animated.View

package/src/components/MessageInput/SendButton.tsx

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

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

@@ -12,41 +12,34 @@ 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';
1815

1916
type SendButtonPropsWithContext<
2017
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2118
> = Pick<MessageInputContextValue<StreamChatGenerics>, 'giphyActive' | 'sendMessage'> & {
2219
/** Disables the button */ disabled: boolean;
23-
aiState: AIStatesEnum;
24-
stopGenerating: () => Promise<EventAPIResponse<StreamChatGenerics>>;
2520
};
2621

2722
const SendButtonWithContext = <
2823
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
2924
>(
3025
props: SendButtonPropsWithContext<StreamChatGenerics>,
3126
) => {
32-
const { disabled = false, giphyActive, sendMessage, aiState, stopGenerating } = props;
27+
const { disabled = false, giphyActive, sendMessage } = props;
3328
const {
3429
theme: {
3530
colors: { accent_blue, grey_gainsboro },
3631
messageInput: { searchIcon, sendButton, sendRightIcon, sendUpIcon },
3732
},
3833
} = useTheme();
3934

40-
const shouldDisplayStopAIGeneration = [AIStates.Thinking, AIStates.Generating].includes(aiState);
41-
4235
return (
4336
<Pressable
4437
disabled={disabled}
45-
onPress={disabled ? () => null : shouldDisplayStopAIGeneration ? () => stopGenerating() : () => sendMessage()}
38+
onPress={disabled ? () => null : () => sendMessage()}
4639
style={[sendButton]}
4740
testID='send-button'
4841
>
49-
{giphyActive || shouldDisplayStopAIGeneration ? ( // TODO: Fix the icon please.
42+
{giphyActive ? (
5043
<Search pathFill={disabled ? grey_gainsboro : accent_blue} {...searchIcon} />
5144
) : disabled ? (
5245
<SendRight fill={grey_gainsboro} size={32} {...sendRightIcon} />
@@ -65,13 +58,11 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
6558
disabled: prevDisabled,
6659
giphyActive: prevGiphyActive,
6760
sendMessage: prevSendMessage,
68-
aiState: prevAiState,
6961
} = prevProps;
7062
const {
7163
disabled: nextDisabled,
7264
giphyActive: nextGiphyActive,
7365
sendMessage: nextSendMessage,
74-
aiState: nextAiState,
7566
} = nextProps;
7667

7768
const disabledEqual = prevDisabled === nextDisabled;
@@ -83,9 +74,6 @@ const areEqual = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
8374
const sendMessageEqual = prevSendMessage === nextSendMessage;
8475
if (!sendMessageEqual) return false;
8576

86-
const aiStateEqual = prevAiState === nextAiState;
87-
if (!aiStateEqual) return false;
88-
8977
return true;
9078
};
9179

@@ -107,18 +95,12 @@ export const SendButton = <
10795
props: SendButtonProps<StreamChatGenerics>,
10896
) => {
10997
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])
11498

11599
return (
116100
<MemoizedSendButton
117101
{...{ giphyActive, sendMessage }}
118102
{...props}
119103
{...{ disabled: props.disabled || false }}
120-
aiState={aiState}
121-
stopGenerating={stopGenerating}
122104
/>
123105
);
124106
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import { Pressable } from 'react-native';
3+
4+
import { useTheme } from '../../contexts/themeContext/ThemeContext';
5+
import { CircleStop } from '../../icons';
6+
7+
export type StopMessageStreamingButtonProps = {
8+
/** Function that opens attachment options bottom sheet */
9+
onPress?: () => void;
10+
};
11+
12+
export const StopMessageStreamingButton = (props: StopMessageStreamingButtonProps) => {
13+
const { onPress } = props;
14+
15+
const {
16+
theme: {
17+
colors: { accent_blue },
18+
messageInput: { stopMessageStreamingButton, stopMessageStreamingIcon },
19+
},
20+
} = useTheme();
21+
22+
return (
23+
<Pressable
24+
hitSlop={{ bottom: 15, left: 15, right: 15, top: 15 }}
25+
onPress={onPress}
26+
style={[stopMessageStreamingButton]}
27+
testID='more-options-button'
28+
>
29+
<CircleStop fill={accent_blue} size={32} {...stopMessageStreamingIcon} />
30+
</Pressable>
31+
);
32+
};
33+
34+
StopMessageStreamingButton.displayName = 'StopMessageStreamingButton{messageInput}';

package/src/contexts/themeContext/utils/theme.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ export type Theme = {
353353
innerContainer: ViewStyle;
354354
text: TextStyle;
355355
};
356+
stopMessageStreamingButton: ViewStyle;
357+
stopMessageStreamingIcon: IconProps;
356358
suggestions: {
357359
command: {
358360
args: TextStyle;
@@ -1079,6 +1081,8 @@ export const defaultTheme: Theme = {
10791081
innerContainer: {},
10801082
text: {},
10811083
},
1084+
stopMessageStreamingButton: {},
1085+
stopMessageStreamingIcon: {},
10821086
suggestions: {
10831087
command: {
10841088
args: {},

0 commit comments

Comments
 (0)