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

Fix: avatar display in various scenarios #5346

Merged
merged 5 commits into from
Nov 8, 2024
Merged
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
Next Next commit
Fix: avatar display in various scenarios
OEvgeny committed Nov 7, 2024
commit 21172131ebcdd298d93cb60ca305d75702364e53
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ function bin<T>(items: T[], grouping: (last: T, current: T) => boolean): T[][] {
const bins: T[][] = [];
let lastItem: T;

items.forEach(item => {
for (const item of items) {
if (lastItem && grouping(lastItem, item)) {
lastBin.push(item);
} else {
@@ -17,46 +17,51 @@ function bin<T>(items: T[], grouping: (last: T, current: T) => boolean): T[][] {
}

lastItem = item;
});
}

return bins;
}

function sending(activity: WebChatActivity): SendStatus | undefined {
if (activity.from.role === 'user') {
const {
channelData: { state, 'webchat:send-status': sendStatus }
channelData: { 'webchat:send-status': sendStatus }
} = activity;

// `channelData.state` is being deprecated in favor of `channelData['webchat:send-status']`.
// Please refer to #4362 for details. Remove on or after 2024-07-31.
return sendStatus || (state === 'sent' ? 'sent' : 'sending');
return sendStatus;
}
}

function shouldGroupTimestamp(
activityX: WebChatActivity,
activityY: WebChatActivity,
groupTimestamp: boolean | number,
{ Date }: GlobalScopePonyfill
): boolean {
if (groupTimestamp === false) {
// Hide timestamp for all activities.
return true;
} else if (activityX && activityY) {
if (sending(activityX) !== sending(activityY)) {
return false;
}
function createShouldGroupTimestamp(groupTimestamp: boolean | number, { Date }: GlobalScopePonyfill) {
return (activityX: WebChatActivity, activityY: WebChatActivity): boolean => {
if (groupTimestamp === false) {
// Hide timestamp for all activities.
return true;
} else if (activityX && activityY) {
if (sending(activityX) !== sending(activityY)) {
return false;
}

groupTimestamp = typeof groupTimestamp === 'number' ? groupTimestamp : Infinity;
groupTimestamp = typeof groupTimestamp === 'number' ? groupTimestamp : Infinity;

const timeX = new Date(activityX.timestamp).getTime();
const timeY = new Date(activityY.timestamp).getTime();
const timeX = new Date(activityX.timestamp).getTime();
const timeY = new Date(activityY.timestamp).getTime();

return Math.abs(timeX - timeY) <= groupTimestamp;
}
return Math.abs(timeX - timeY) <= groupTimestamp;
}

return false;
};
}

return false;
function shouldGroupSender(x: WebChatActivity, y: WebChatActivity): boolean {
const {
from: { role: roleX, id: idX }
} = x;
const {
from: { role: roleY, id: idY }
} = y;
return roleX === roleY && idX === idY;
}

export default function createDefaultGroupActivitiesMiddleware({
@@ -69,7 +74,7 @@ export default function createDefaultGroupActivitiesMiddleware({
return () =>
() =>
({ activities }) => ({
sender: bin(activities, (x, y) => x.from.role === y.from.role),
status: bin(activities, (x, y) => shouldGroupTimestamp(x, y, groupTimestamp, ponyfill))
sender: bin(activities, shouldGroupSender),
status: bin(activities, createShouldGroupTimestamp(groupTimestamp, ponyfill))
});
}
11 changes: 4 additions & 7 deletions packages/component/src/Avatar/ImageAvatar.tsx
Original file line number Diff line number Diff line change
@@ -2,15 +2,16 @@ import { hooks } from 'botframework-webchat-api';
import classNames from 'classnames';
import React, { memo } from 'react';

import FixedWidthImage from '../Utils/FixedWidthImage';
import useStyleSet from '../hooks/useStyleSet';
import { useStyleToEmotionObject } from '../hooks/internal/styleToEmotionObject';

const { useAvatarForBot, useAvatarForUser } = hooks;

const ROOT_STYLE = {
'& .webchat__imageAvatar__image': {
width: '100%'
width: '100%',
height: '100%',
objectFit: 'cover'
}
};

@@ -25,11 +26,7 @@ const ImageAvatar = memo(({ fromUser }: Readonly<{ fromUser: boolean }>) => {
return (
!!avatarImage && (
<div className={classNames('webchat__imageAvatar', rootClassName, imageAvatarStyleSet + '')}>
<FixedWidthImage
alt=""
className="webchat__imageAvatar__image"
src={fromUser ? avatarImageForUser : avatarImageForBot}
/>
<img alt="" className="webchat__imageAvatar__image" src={fromUser ? avatarImageForUser : avatarImageForBot} />
</div>
)
);
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import useStyleSet from '../../hooks/useStyleSet';
import { useStyleToEmotionObject } from '../../hooks/internal/styleToEmotionObject';

const ROOT_STYLE = {
overflow: 'hidden',
overflow: ['hidden', 'clip'],
position: 'relative',

'> *': {
3 changes: 2 additions & 1 deletion packages/component/src/Styles/StyleSet/ImageAvatar.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,9 @@ import CSSTokens from '../CSSTokens';

export default function createImageAvatarStyle() {
return {
aspectRatio: 1,
OEvgeny marked this conversation as resolved.
Show resolved Hide resolved
height: CSSTokens.SizeAvatar,
overflow: 'hidden',
overflow: ['hidden', 'clip'],
width: CSSTokens.SizeAvatar
};
}