Skip to content

Commit 3bb3b71

Browse files
committed
Add useBuildRenderAvatarCallback test
1 parent 5d3bb3e commit 3bb3b71

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
5+
</head>
6+
<body>
7+
<main id="webchat"></main>
8+
<script type="importmap">
9+
{
10+
"imports": {
11+
"botframework-webchat": "/__dist__/packages/bundle/static/botframework-webchat.js",
12+
"botframework-webchat/component": "/__dist__/packages/bundle/static/botframework-webchat/component.js",
13+
"botframework-webchat/middleware": "/__dist__/packages/bundle/static/botframework-webchat/middleware.js",
14+
"react": "/__dist__/packages/bundle/static/react.js",
15+
"react-dom": "/__dist__/packages/bundle/static/react-dom.js",
16+
"react-dom/client": "/__dist__/packages/bundle/static/react-dom/client.js"
17+
}
18+
}
19+
</script>
20+
<script type="module">
21+
import '/test-harness.mjs';
22+
import '/test-page-object.mjs';
23+
24+
import { createStoreWithOptions, renderWebChat, testIds } from 'botframework-webchat';
25+
import { Composer } from 'botframework-webchat/component';
26+
import {
27+
avatarComponent,
28+
createAvatarPolymiddleware,
29+
useBuildRenderAvatarCallback
30+
} from 'botframework-webchat/middleware';
31+
import { createElement, memo } from 'react';
32+
import { createRoot } from 'react-dom/client';
33+
34+
const {
35+
testHelpers: { createDirectLineEmulator }
36+
} = window;
37+
38+
testHelpers.hideKnownError();
39+
40+
// TODO: Should find ways to eliminate this line.
41+
window.WebChat = { createStoreWithOptions, testIds };
42+
43+
run(async function () {
44+
const { directLine, store } = createDirectLineEmulator();
45+
46+
const MiddlewareAvatar = ({ activity, children, fromUser }) =>
47+
createElement(
48+
'div',
49+
{
50+
style: {
51+
borderRadius: 40,
52+
height: 40,
53+
outlineColor: 'green',
54+
outlineOffset: 2,
55+
outlineStyle: fromUser ? 'dotted' : 'dashed',
56+
outlineWidth: 2,
57+
overflow: 'hidden',
58+
width: 40
59+
}
60+
},
61+
children
62+
);
63+
64+
const PolymiddlewareAvatar = ({ activity, children }) =>
65+
createElement(
66+
'div',
67+
{
68+
style: {
69+
borderRadius: 32,
70+
height: 32,
71+
margin: 4,
72+
outlineColor: 'red',
73+
outlineOffset: 2,
74+
outlineStyle: activity.from?.role === 'user' ? 'dotted' : 'dashed',
75+
outlineWidth: 2,
76+
overflow: 'hidden',
77+
width: 32
78+
}
79+
},
80+
children
81+
);
82+
83+
const RenderAvatar = memo(({ activity }) => {
84+
const renderAvatar = useBuildRenderAvatarCallback()
85+
86+
return createElement(renderAvatar({ activity }));
87+
});
88+
89+
RenderAvatar.displayName = 'RenderAvatar';
90+
91+
createRoot(document.getElementById('webchat')).render(
92+
createElement(
93+
Composer,
94+
{
95+
avatarMiddleware: [
96+
() => next => request => {
97+
const children = next(request);
98+
99+
return (...args) =>
100+
createElement(
101+
MiddlewareAvatar,
102+
{ activity: request.activity, fromUser: request.fromUser },
103+
children(...args)
104+
);
105+
}
106+
],
107+
directLine,
108+
polymiddleware: Object.freeze([
109+
createAvatarPolymiddleware(next => request => {
110+
const children = next(request)?.render();
111+
112+
return avatarComponent(PolymiddlewareAvatar, { activity: request.activity, children });
113+
})
114+
]),
115+
store,
116+
styleOptions: {
117+
botAvatarImage: '/assets/bot-avatar.jpg',
118+
botAvatarInitials: 'WC',
119+
userAvatarImage: '/assets/user-avatar.jpg',
120+
userAvatarInitials: 'WW'
121+
}
122+
},
123+
createElement(
124+
'div',
125+
{
126+
style: {
127+
margin: 4
128+
}
129+
},
130+
createElement(RenderAvatar, {
131+
activity: {
132+
from: { role: 'bot' },
133+
id: 'a-00001',
134+
text: 'Hello, World!',
135+
type: 'messagenp'
136+
}
137+
})
138+
)
139+
)
140+
);
141+
142+
await host.snapshot('local');
143+
});
144+
</script>
145+
</body>
146+
</html>
6.94 KB
Loading

packages/api-middleware/src/avatarPolymiddleware.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type AvatarPolymiddlewareProxyProps = Readonly<InferInput<typeof avatarPolymiddl
5353

5454
// A friendlier version than the organic <Proxy>.
5555
const AvatarPolymiddlewareProxy = memo(function AvatarPolymiddlewareProxy(props: AvatarPolymiddlewareProxyProps) {
56+
// TODO: [P1] Proxy should not require `styleOptions`, it should read from `useStyleOptions`.
57+
// However the `useStyleOptions` hook is in `api` which is not available in `api-middleware`.
58+
// We should refactor `useStyleOptions` into `api-style-options` so we can make the hook available here.
5659
const { [__INTERNAL_DO_NOT_USE__avatarPolymiddlewareRequestStyleOptionsSymbol]: styleOptions, activity } =
5760
validateProps(avatarPolymiddlewareProxyPropsSchema, props);
5861

0 commit comments

Comments
 (0)