forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileContent.tsx
More file actions
137 lines (115 loc) · 4 KB
/
FileContent.tsx
File metadata and controls
137 lines (115 loc) · 4 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { validateProps } from '@msinternal/botframework-webchat-react-valibot';
import { hooks } from 'botframework-webchat-api';
import classNames from 'classnames';
import React, { memo } from 'react';
import { boolean, number, object, optional, pipe, readonly, string, type InferInput } from 'valibot';
import { useStyleToEmotionObject } from '../hooks/internal/styleToEmotionObject';
import useStyleSet from '../hooks/useStyleSet';
import { ComponentIcon } from '../Icon';
const { useByteFormatter, useDirection, useLocalizer } = hooks;
const ROOT_STYLE = {
display: 'flex',
'& .webchat__fileContent__buttonLink': {
display: 'flex',
flex: 1
},
'& .webchat__fileContent__badge': {
display: 'flex',
flex: 1,
flexDirection: 'column'
}
};
// TODO: Consider using `useSanitizeHrefCallback`, which underlying use `sanitize-html` or whatever in HTML content transformer.
const ALLOWED_PROTOCOLS = ['blob:', 'data:', 'http:', 'https:'];
function isAllowedProtocol(url) {
try {
return ALLOWED_PROTOCOLS.includes(new URL(url).protocol);
} catch {
return false;
}
}
const fileContentBadgePropsSchema = pipe(
object({
downloadIcon: optional(boolean()),
fileName: string(),
size: optional(number())
}),
readonly()
);
type FileContentBadgeProps = InferInput<typeof fileContentBadgePropsSchema>;
const FileContentBadge = (props: FileContentBadgeProps) => {
const { downloadIcon = false, fileName, size } = validateProps(fileContentBadgePropsSchema, props);
const [direction] = useDirection();
const formatByte = useByteFormatter();
const localizedSize = typeof size === 'number' && formatByte(size);
return (
<React.Fragment>
<div aria-hidden={true} className="webchat__fileContent__badge">
<div className="webchat__fileContent__fileName">{fileName}</div>
{!!localizedSize && <div className="webchat__fileContent__size">{localizedSize}</div>}
</div>
{downloadIcon && (
<ComponentIcon
appearance="text"
className={classNames(
'webchat__fileContent__downloadIcon',
direction === 'rtl' && 'webchat__fileContent__downloadIcon--rtl'
)}
icon="download"
/>
)}
</React.Fragment>
);
};
type FileContentProps = InferInput<typeof fileContentPropsSchema>;
const fileContentPropsSchema = pipe(
object({
className: optional(string()),
fileName: string(),
href: optional(string()),
size: optional(number())
}),
readonly()
);
function FileContent(props: FileContentProps) {
const { className, href, fileName, size } = validateProps(fileContentPropsSchema, props);
const [{ fileContent: fileContentStyleSet }] = useStyleSet();
const localize = useLocalizer();
const localizeBytes = useByteFormatter();
const rootClassName = useStyleToEmotionObject()(ROOT_STYLE) + '';
const localizedSize = typeof size === 'number' && localizeBytes(size);
const sanitizedHref = href && isAllowedProtocol(href) ? href : undefined;
const alt = localize(
sanitizedHref
? localizedSize
? 'FILE_CONTENT_DOWNLOADABLE_WITH_SIZE_ALT'
: 'FILE_CONTENT_DOWNLOADABLE_ALT'
: localizedSize
? 'FILE_CONTENT_WITH_SIZE_ALT'
: 'FILE_CONTENT_ALT',
fileName,
localizedSize
);
return (
<div className={classNames('webchat__fileContent', rootClassName, fileContentStyleSet + '', className)}>
{sanitizedHref ? (
// URL is sanitized.
// eslint-disable-next-line react/forbid-elements
<a
aria-label={alt}
className="webchat__fileContent__buttonLink"
download={fileName}
href={sanitizedHref}
rel="noopener noreferrer"
target="_blank"
>
<FileContentBadge downloadIcon={true} fileName={fileName} size={size} />
</a>
) : (
<FileContentBadge downloadIcon={false} fileName={fileName} size={size} />
)}
</div>
);
}
export default memo(FileContent);
export { fileContentPropsSchema, type FileContentProps };