Download components/utils/renderIcon.tsx from zitons/logup-re: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/zitons/logup-re/resolve/main/components/utils/renderIcon.tsx
- Command line
-
hf download hf://spaces/zitons/logup-re/components/utils/renderIcon.tsx
-
curl -L -o renderIcon.tsx https://huggingface.co/spaces/zitons/logup-re/resolve/main/components/utils/renderIcon.tsx
2.1 kB
| /* | |
| * @Date: 2026-01-16 | |
| * @LastEditors: vhko | |
| * @LastEditTime: 2026-01-21 | |
| * @FilePath: /LogUp/components/utils/renderIcon.tsx | |
| * Helllllloo! | |
| */ | |
| import React from 'react'; | |
| import Image from 'next/image'; | |
| type RenderIconProps = { | |
| icon: string; | |
| size?: number; // 可选,避免 undefined 报错 | |
| }; | |
| export function RenderIcon({ icon, size = 55 }: RenderIconProps) { | |
| const [svgContent, setSvgContent] = React.useState<string | null>(null); | |
| React.useEffect(() => { | |
| let active = true; | |
| async function fetchSvg(url: string) { | |
| try { | |
| const res = await fetch(url); | |
| const text = await res.text(); | |
| if (active) { | |
| setSvgContent(text); | |
| } | |
| } catch (err) { | |
| console.error('加载 SVG 失败:', err); | |
| } | |
| } | |
| if (icon && (icon.endsWith('.svg') || icon.includes('.svg?'))) { | |
| fetchSvg(icon); | |
| } else { | |
| setSvgContent(null); | |
| } | |
| return () => { | |
| active = false; | |
| }; | |
| }, [icon]); | |
| if (!icon) return null; | |
| // 1. 内联 svg | |
| if (icon.trim().startsWith('<svg')) { | |
| return <span dangerouslySetInnerHTML={{ __html: icon }} />; | |
| } | |
| // 2. 外部 svg url | |
| if (svgContent) { | |
| return ( | |
| <span | |
| dangerouslySetInnerHTML={{ | |
| __html: svgContent.replace( | |
| /<svg([^>]*)>/, | |
| `<svg width="${size}" height="${size}">`, | |
| ), | |
| }} | |
| /> | |
| ); | |
| } | |
| // 3. 其他图片链接 | |
| if (icon.startsWith('http://') || icon.startsWith('https://')) { | |
| return ( | |
| <Image | |
| src={icon} | |
| alt="icon" | |
| width={size} | |
| height={size} | |
| unoptimized | |
| style={{ width: size, height: size, objectFit: 'contain' }} | |
| /> | |
| ); | |
| } | |
| // 4. 默认文本 | |
| return <span style={{ fontSize: size }}>{icon}</span>; | |
| } | |