/* * @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(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('; } // 2. 外部 svg url if (svgContent) { return ( ]*)>/, ``, ), }} /> ); } // 3. 其他图片链接 if (icon.startsWith('http://') || icon.startsWith('https://')) { return ( ); } // 4. 默认文本 return {icon}; }