-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathFileIcon.tsx
More file actions
79 lines (68 loc) · 1.92 KB
/
Copy pathFileIcon.tsx
File metadata and controls
79 lines (68 loc) · 1.92 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
import React, { useMemo } from 'react';
import { iconMap } from './iconMap';
import { mergeFileIconSizeConfig } from './FileIconSet';
import { mimeTypeToExtensionMap } from './mimeTypes';
import type { FileIconSize } from './FileIconSet';
export type FileIconSizeConfigOverride = Partial<
Record<
FileIconSize,
Partial<{ width: number; height: number; labelX: number; labelY: number }>
>
>;
export type FileIconProps = {
className?: string;
fileName?: string;
mimeType?: string;
/** Override dimensions/label position per size (sm, md, lg, xl). */
sizeConfig?: FileIconSizeConfigOverride;
size?: FileIconSize;
};
export function mimeTypeToIcon(mimeType?: string) {
const theMap = iconMap['standard'];
if (!mimeType) return theMap.fallback;
const icon = theMap[mimeType];
if (icon) return icon;
if (mimeType.startsWith('audio/')) return theMap['audio/'];
if (mimeType.startsWith('video/')) return theMap['video/'];
if (mimeType.startsWith('image/')) return theMap['image/'];
if (mimeType.startsWith('text/')) return theMap['text/'];
return theMap.fallback;
}
const labelFromMimeType = ({
fileName,
mimeType,
}: Pick<FileIconProps, 'fileName' | 'mimeType'>) => {
let label;
if (mimeType) {
label = mimeTypeToExtensionMap[mimeType];
}
if (!label && fileName) {
label = fileName.split('.').slice(-1)[0];
}
return label;
};
export const FileIcon = (props: FileIconProps) => {
const {
className,
fileName,
mimeType,
size = 'md',
sizeConfig: sizeConfigOverride,
...rest
} = props;
const sizeConfig = useMemo(
() => mergeFileIconSizeConfig(sizeConfigOverride),
[sizeConfigOverride],
);
const Icon = mimeTypeToIcon(mimeType);
const label = fileName ? labelFromMimeType({ fileName, mimeType }) : undefined;
return (
<Icon
{...rest}
className={className}
label={label}
size={size}
sizeConfig={sizeConfig}
/>
);
};