Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modify emoji display condition #56

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/BotMessageWithBodyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ type Props = {
messageCount?: number;
zIndex?: number;
bodyStyle?: object;
isBotWelcomeMessage?: boolean;
};

const ImageContainer = styled.div``;

const EmptyImageContainer = styled.div`
width: 30px;
width: 28px;
`;

export default function BotMessageWithBodyInput(props: Props) {
Expand All @@ -80,11 +81,13 @@ export default function BotMessageWithBodyInput(props: Props) {
bodyStyle,
chainTop,
chainBottom,
isBotWelcomeMessage,
} = props;

const nonChainedMessage = chainTop == null && chainBottom == null;
const displayProfileImage = nonChainedMessage || chainBottom;
const displaySender = nonChainedMessage || chainTop;

return (
<Root style={{ zIndex: messageCount === 1 && zIndex ? zIndex : 0 }}>
{displayProfileImage ? (
Expand All @@ -111,7 +114,9 @@ export default function BotMessageWithBodyInput(props: Props) {
</Sender>
)}
{bodyComponent}
{enableEmojiFeedback && <ReactionContainer message={message} />}
{enableEmojiFeedback && displayProfileImage && !isBotWelcomeMessage && (
<ReactionContainer message={message} />
)}
</BodyContainer>
<SentTime>{formatCreatedAtToAMPM(message.createdAt)}</SentTime>
</Root>
Expand Down
16 changes: 15 additions & 1 deletion src/components/CustomMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { User } from '@sendbird/chat';
import { UserMessage } from '@sendbird/chat/message';
import { useChannelContext } from '@sendbird/uikit-react/Channel/context';
import { useMemo } from 'react';
// eslint-disable-next-line import/no-unresolved
import { EveryMessage } from 'SendbirdUIKitGlobal';

Expand All @@ -20,6 +21,7 @@ import {
replaceUrl,
Token,
} from '../utils';
import { getBotWelcomeMessages } from '../utils/messages';

type Props = {
message: EveryMessage;
Expand All @@ -45,6 +47,16 @@ export default function CustomMessage(props: Props) {
const firstMessage: UserMessage = allMessages[0] as UserMessage;
const firstMessageId = firstMessage?.messageId ?? -1;

const isBotWelcomeMessage = useMemo(() => {
const botWelcomeMessages = getBotWelcomeMessages(
allMessages,
botUser.userId
);
return !!botWelcomeMessages.find(
(welcomeMessage) => welcomeMessage.messageId === message.messageId
);
}, [allMessages.length]);

bang9 marked this conversation as resolved.
Show resolved Hide resolved
// admin message
if (message.messageType === 'admin') {
return <div>{<AdminMessage message={message} />}</div>;
Expand All @@ -71,9 +83,9 @@ export default function CustomMessage(props: Props) {
}
messageCount={allMessages.length}
zIndex={30}
bodyStyle={{ maxWidth: '255px', width: 'calc(100% - 98px)' }}
chainTop={chainTop}
chainBottom={chainBottom}
isBotWelcomeMessage={isBotWelcomeMessage}
/>
</div>
);
Expand All @@ -94,6 +106,7 @@ export default function CustomMessage(props: Props) {
messageCount={allMessages.length}
chainTop={chainTop}
chainBottom={chainBottom}
isBotWelcomeMessage={isBotWelcomeMessage}
/>
);
}
Expand Down Expand Up @@ -125,6 +138,7 @@ export default function CustomMessage(props: Props) {
}
chainTop={chainTop}
chainBottom={chainBottom}
isBotWelcomeMessage={isBotWelcomeMessage}
/>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/ParsedBotMessageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function ParsedBotMessageBody(props: Props) {
: [];

// console.log('## sources: ', sources);
if (tokens.length > 0 && enableSourceMessage) {
if (tokens.length > 0) {
return (
<Root>
{tokens.map((token: Token, i) => {
Expand All @@ -81,7 +81,7 @@ export default function ParsedBotMessageBody(props: Props) {
</BlockContainer>
);
})}
{sources.length > 0 ? (
{sources.length > 0 && enableSourceMessage ? (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found that this condition needs to be here not on the above to make it work correctly.

<>
<SourceContainer sources={sources} />
<BotMessageBottom />
Expand Down
18 changes: 9 additions & 9 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export const DEFAULT_CONSTANT: Constant = {
],
},
firstMessageData: [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled this line cause I don't think it's really helpful 😅

{
data: {
quick_replies: [
'What can I learn from Pre-K 8th grade?',
'Tell me about Math',
],
},
message: "Hi~ I'm Khan Academy Support ChatBot. Ask me anything!",
},
// {
// data: {
// quick_replies: [
// 'What can I learn from Pre-K 8th grade?',
// 'Tell me about Math',
// ],
// },
// message: "Hi~ I'm Khan Academy Support ChatBot. Ask me anything!",
// },
],
createGroupChannelParams: {
name: 'Sendbird AI Chatbot',
Expand Down
4 changes: 1 addition & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { DEFAULT_CONSTANT } from './const';

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App
firstMessageData={DEFAULT_CONSTANT.firstMessageData}
/>
<App firstMessageData={DEFAULT_CONSTANT.firstMessageData} />
</React.StrictMode>
);
21 changes: 21 additions & 0 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,24 @@ export function groupMessagesByShortSpanTime(
}
);
}

export function getBotWelcomeMessages(
messages: EveryMessage[],
botUserId: string
) {
// if the list is empty or the first message is not from bot,
// we just assume there's no welcome messages
if (messages.length === 0 || messages[0]?.sender.userId !== botUserId) {
return [];
}

// if the list has only bot messages, then just return the whole list
if (messages.every((message) => message?.sender.userId === botUserId)) {
return messages;
}

const firstUserMesssage = messages.find(
(message) => message?.sender.userId !== botUserId
);
return messages.slice(0, messages.indexOf(firstUserMesssage));
}
Loading