| 1 | // @ts-ignore |
| 2 | import DOMPurify from "dompurify"; |
| 3 | import { useMemo } from "react"; |
| 4 | import { themeIcons } from "seti-file-icons"; |
| 5 | |
| 6 | export interface FileIconProps { |
| 7 | height: `${number}px`; |
| 8 | width: `${number}px`; |
| 9 | filename: string; |
| 10 | } |
| 11 | |
| 12 | export default function FileIcon({ filename, height, width }: FileIconProps) { |
| 13 | const file = useMemo(() => { |
| 14 | if (filename.includes(" (")) { |
| 15 | const path = filename.split(" "); |
| 16 | path.pop(); |
| 17 | return path.join(" "); |
| 18 | } else { |
| 19 | return filename; |
| 20 | } |
| 21 | }, [filename]); |
| 22 | |
| 23 | const getIcon = themeIcons({ |
| 24 | blue: "#268bd2", |
| 25 | grey: "#657b83", |
| 26 | "grey-light": "#839496", |
| 27 | green: "#859900", |
| 28 | orange: "#cb4b16", |
| 29 | pink: "#d33682", |
| 30 | purple: "#6c71c4", |
| 31 | red: "#dc322f", |
| 32 | white: "#fdf6e3", |
| 33 | yellow: "#b58900", |
| 34 | ignore: "#586e75", |
| 35 | }); |
| 36 | |
| 37 | // Sanitize the SVG string before rendering it |
| 38 | const { svg, color } = getIcon(file); |
| 39 | const sanitizedSVG = DOMPurify.sanitize(svg); |
| 40 | |
| 41 | return ( |
| 42 | <span |
| 43 | dangerouslySetInnerHTML={{ __html: sanitizedSVG }} |
| 44 | style={{ |
| 45 | width: width, |
| 46 | height: height, |
| 47 | fill: color, |
| 48 | flexShrink: 0, |
| 49 | display: "flex", |
| 50 | }} |
| 51 | /> |
| 52 | ); |
| 53 | } |