|
1 | 1 | # Middleware |
2 | 2 |
|
3 | | -## Cooking recipes |
| 3 | +## Recipes |
4 | 4 |
|
5 | | -### Showing channel message as a badge |
| 5 | +### Show channel message as a badge |
6 | 6 |
|
7 | | -When the following activity is received, the middleware will display it in a badge. |
| 7 | +This sample demonstrates the following: |
| 8 | + |
| 9 | +- Add a new activity middleware component for a specific type of activity |
8 | 10 |
|
9 | 11 | #### Activity payload |
10 | 12 |
|
@@ -59,3 +61,62 @@ const polyMiddleware = [ |
59 | 61 | }) |
60 | 62 | ]; |
61 | 63 | ``` |
| 64 | + |
| 65 | +### Decorate activity with a border |
| 66 | + |
| 67 | +This sample demonstrates the following: |
| 68 | + |
| 69 | +- Decorate an activity |
| 70 | +- Use [hooks](./HOOKS.md#usestyleoptions) within the activity middleware component |
| 71 | + |
| 72 | +#### Screenshot |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +#### Code snippet |
| 77 | + |
| 78 | +```tsx |
| 79 | +import ReactWebChat, { createStoreWithOptions, hooks } from 'botframework-webchat'; |
| 80 | +import { |
| 81 | + activityComponent, |
| 82 | + createActivityPolyMiddleware, |
| 83 | + type ActivityPolyMiddlewareProps, |
| 84 | + type ActivityPolyMiddlewareRenderer |
| 85 | +} from 'botframework-webchat/middleware'; |
| 86 | +import React, { Fragment, memo, useMemo } from 'react'; |
| 87 | +import { render } from 'react-dom'; |
| 88 | + |
| 89 | +const { useStyleOptions } = hooks; |
| 90 | + |
| 91 | +type MessageBorderProps = ActivityPolyMiddlewareProps & { |
| 92 | + readonly render: ActivityPolyMiddlewareRenderer | undefined; |
| 93 | +}; |
| 94 | + |
| 95 | +const MessageBorder = memo<MessageBorderProps>(function MessageBorder({ render }) { |
| 96 | + const [{ accent }] = useStyleOptions(); |
| 97 | + |
| 98 | + const style = useMemo(() => ({ outlineColor: accent }), [accent]); |
| 99 | + |
| 100 | + return ( |
| 101 | + <div className="message-border" style={style}> |
| 102 | + <Fragment>{render?.({})}</Fragment> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +}); |
| 106 | + |
| 107 | +const polyMiddleware = [ |
| 108 | + createActivityPolyMiddleware(next => request => { |
| 109 | + const { activity } = request; |
| 110 | + |
| 111 | + if (activity.type === 'message') { |
| 112 | + const render = next(request)?.render; |
| 113 | + |
| 114 | + return activityComponent<MessageBorderProps>(MessageBorder, { |
| 115 | + render |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + return next(request); |
| 120 | + }) |
| 121 | +]; |
| 122 | +``` |
0 commit comments