-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathLgWidget.tsx
96 lines (83 loc) · 2.72 KB
/
LgWidget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { WidgetContainerProps } from '@bfc/extension-client';
import styled from '@emotion/styled';
import get from 'lodash/get';
import { Stack } from '@fluentui/react/lib/Stack';
import { Text } from '@fluentui/react/lib/Text';
import React from 'react';
import { useLgTemplate } from './useLgTemplate';
const getResponseTypeDisplayText = (responseType: string) => {
switch (responseType) {
default:
case 'Attachments':
case 'Text':
return responseType;
case 'Speak':
return 'Speech';
case 'SuggestedActions':
return 'Sug. Actions';
}
};
const Root = styled(Stack)({
lineHeight: '16px',
});
const rootTokens = { childrenGap: 4 };
const rowTokens = { childrenGap: 8 };
const GrayText = styled(Text)({
color: '#838383',
});
const OneLinerText = styled(Stack)({
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
flex: 1,
'& span': {
overflowX: 'hidden',
},
});
const MoreCountText = styled(GrayText)({
width: 20,
});
const StructuredResponseRow = React.memo(
({ keyText, valueText, moreCount }: { keyText: string; valueText: string; moreCount?: number }) => {
return (
<Stack horizontal tokens={rowTokens} verticalAlign="center">
<GrayText variant="small">{keyText}</GrayText>
<OneLinerText horizontal title={valueText} tokens={rootTokens}>
<Stack styles={{ root: { overflow: 'hidden', maxWidth: `calc(100% - ${moreCount ? '24px' : '0px'})` } }}>
<Text variant="small">{valueText}</Text>
</Stack>
{moreCount && <MoreCountText variant="small">{`+${moreCount}`}</MoreCountText>}
</OneLinerText>
</Stack>
);
}
);
export interface LgWidgetProps extends WidgetContainerProps {
/** indicates which field contains lg activity. ('activity', 'prompt', 'invalidPrompt'...) */
field: string;
defaultContent?: string;
}
export const LgWidget: React.FC<LgWidgetProps> = ({ data, field, defaultContent = '' }) => {
const activityTemplate = get(data, field, '') as string;
const templateTextData = useLgTemplate(activityTemplate) ?? defaultContent;
// If the template has structured response, show rich text
if (typeof templateTextData === 'object') {
const data = templateTextData as Record<string, { value: string; moreCount?: number }>;
return (
<Root tokens={rootTokens}>
{Object.keys(data).map((k) => (
<StructuredResponseRow
key={k}
keyText={getResponseTypeDisplayText(k)}
moreCount={data[k].moreCount}
valueText={data[k].value}
/>
))}
</Root>
);
}
// otherwise show full text
return <>{templateTextData}</>;
};