Skip to content

Commit 9cb5b96

Browse files
authored
feat: add suggested replies (#825)
Addresses https://sendbird.atlassian.net/browse/AC-581 The feature request is from AI chat bot. #### Scenarios If a trigger message(can be set from the dashboard) is sent to the channel from a user, the predefined suggested reply options will be displayed. Once the user clicks an option from the list, the option list will be gone and the message will be sent.
1 parent 59647d3 commit 9cb5b96

File tree

5 files changed

+105
-7
lines changed

5 files changed

+105
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"react-dom": "^16.8.6 || ^17.0.0 || ^18.0.0"
6868
},
6969
"dependencies": {
70-
"@sendbird/chat": "^4.9.14",
70+
"@sendbird/chat": "^4.10.1",
7171
"@sendbird/uikit-tools": "0.0.1-alpha.40",
7272
"css-vars-ponyfill": "^2.3.2",
7373
"date-fns": "^2.16.1",

src/modules/Channel/components/Message/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React, {
55
useEffect,
66
useLayoutEffect,
77
} from 'react';
8-
import type { FileMessage } from '@sendbird/chat/message';
8+
import type { FileMessage, UserMessage } from '@sendbird/chat/message';
99
import format from 'date-fns/format';
1010

1111
import useDidMountEffect from '../../../../utils/useDidMountEffect';
@@ -27,6 +27,7 @@ import { EveryMessage, RenderCustomSeparatorProps, RenderMessageProps } from '..
2727
import { useLocalization } from '../../../../lib/LocalizationContext';
2828
import { useHandleOnScrollCallback } from '../../../../hooks/useHandleOnScrollCallback';
2929
import { useDirtyGetMentions } from '../../../Message/hooks/useDirtyGetMentions';
30+
import SuggestedReplies from '../SuggestedReplies';
3031

3132
type MessageUIProps = {
3233
message: EveryMessage;
@@ -91,6 +92,8 @@ const Message = ({
9192
onMessageHighlighted,
9293
onScrollCallback,
9394
setIsScrolled,
95+
sendMessage,
96+
localMessages,
9497
} = useChannelContext();
9598
const [showEdit, setShowEdit] = useState(false);
9699
const [showRemove, setShowRemove] = useState(false);
@@ -209,6 +212,8 @@ const Message = ({
209212
return null;
210213
}, [message, renderCustomSeparator]);
211214

215+
const suggestedReplies = message.extendedMessagePayload?.suggested_replies as string[] | undefined ?? [];
216+
212217
if (renderedMessage) {
213218
return (
214219
<div
@@ -379,6 +384,13 @@ const Message = ({
379384
/>
380385
)
381386
}
387+
{/** Suggested Replies */}
388+
{message.messageId === currentGroupChannel?.lastMessage.messageId
389+
// the options should appear only when there's no failed or pending messages
390+
&& localMessages.every(message => (message as UserMessage).sendingStatus === 'succeeded')
391+
&& suggestedReplies.length > 0 && (
392+
<SuggestedReplies replyOptions={suggestedReplies} onSendMessage={sendMessage} />
393+
)}
382394
{/* Modal */}
383395
{
384396
showRemove && (
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@import '../../../../styles/variables';
2+
3+
.sendbird-suggested-replies {
4+
@include themed() {
5+
font-family: var(--sendbird-font-family-default);
6+
}
7+
position: relative;
8+
display: flex;
9+
justify-content: flex-end;
10+
align-items: flex-end;
11+
flex-wrap: wrap;
12+
column-gap: 10px;
13+
row-gap: 8px;
14+
margin-top: 16px;
15+
flex-direction: column;
16+
}
17+
18+
.sendbird-suggested-replies__option {
19+
white-space: nowrap;
20+
height: 32px;
21+
font-size: 12px;
22+
padding: 0 14px;
23+
display: flex;
24+
align-items: center;
25+
border-radius: 18px;
26+
cursor: pointer;
27+
@include themed() {
28+
color: t(primary--3-2);
29+
border: 1px solid t(primary--3-2);
30+
background-color: t(bg-0);
31+
}
32+
&:hover {
33+
@include themed() {
34+
background-color: t(bg-1);
35+
}
36+
}
37+
&:active {
38+
@include themed() {
39+
background-color: t(primary--3-2);
40+
color: t(bg-0);
41+
}
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import './index.scss';
2+
import React, { useState } from 'react';
3+
4+
interface Props {
5+
replyOptions: string[];
6+
onSendMessage: ({ message }: { message: string }) => void;
7+
}
8+
9+
const SuggestedReplies = ({ replyOptions, onSendMessage }: Props) => {
10+
const [replied, setReplied] = useState<boolean>(false);
11+
12+
const onClickReply = (
13+
event: React.MouseEvent<HTMLDivElement>,
14+
option: string,
15+
) => {
16+
event.preventDefault();
17+
onSendMessage({ message: option });
18+
setReplied(true);
19+
};
20+
21+
if (replied) {
22+
return null;
23+
}
24+
25+
return (
26+
<div className="sendbird-suggested-replies">
27+
{replyOptions.map((option: string, index: number) => {
28+
return (
29+
<div
30+
className="sendbird-suggested-replies__option"
31+
id={option}
32+
key={index + option}
33+
onClick={(e) => onClickReply(e, option)}
34+
>
35+
{option}
36+
</div>
37+
);
38+
})}
39+
</div>
40+
);
41+
};
42+
43+
export default SuggestedReplies;

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,15 +2430,15 @@ __metadata:
24302430
languageName: node
24312431
linkType: hard
24322432

2433-
"@sendbird/chat@npm:^4.9.14":
2434-
version: 4.10.0
2435-
resolution: "@sendbird/chat@npm:4.10.0"
2433+
"@sendbird/chat@npm:^4.10.1":
2434+
version: 4.10.1
2435+
resolution: "@sendbird/chat@npm:4.10.1"
24362436
peerDependencies:
24372437
"@react-native-async-storage/async-storage": ^1.17.6
24382438
peerDependenciesMeta:
24392439
"@react-native-async-storage/async-storage":
24402440
optional: true
2441-
checksum: 92c0cbb71bcba17800d24cad8c0bcbba8ff6fc89b3210e9902d47fe16e5cf187684c9313a885c1b55960f40f648e703c9b29befb222f03836c3a468906a8b0c0
2441+
checksum: 8e75a50a31557d5ac0d4d24918bdd80c778224cd22b96d6e794bb19ad62a1da454ad85951e461fec429fb0c5b71661755bcb522c2993a7da6f0f3eadfbb89ee6
24422442
languageName: node
24432443
linkType: hard
24442444

@@ -2459,7 +2459,7 @@ __metadata:
24592459
"@rollup/plugin-node-resolve": ^7.1.3
24602460
"@rollup/plugin-replace": ^2.4.2
24612461
"@rollup/plugin-typescript": ^8.2.1
2462-
"@sendbird/chat": ^4.9.14
2462+
"@sendbird/chat": ^4.10.1
24632463
"@sendbird/uikit-tools": 0.0.1-alpha.40
24642464
"@storybook/addon-actions": ^6.5.10
24652465
"@storybook/addon-docs": ^6.5.10

0 commit comments

Comments
 (0)