import { Avatar } from '@heroui/avatar'; import { Button } from '@heroui/button'; import { Card, CardBody, CardFooter } from '@heroui/card'; import { Chip } from '@heroui/chip'; import { Switch } from '@heroui/switch'; import { Tooltip } from '@heroui/tooltip'; import { MdDeleteForever, MdSettings } from 'react-icons/md'; import clsx from 'clsx'; import { useLocalStorage } from '@uidotdev/usehooks'; import { useState } from 'react'; import key from '@/const/key'; import { PluginItem } from '@/controllers/plugin_manager'; import { getPluginIconUrl } from '@/utils/plugin_icon'; /** 提取作者头像 URL */ function getAuthorAvatar (homepage?: string, repository?: string): string | undefined { // 1. 尝试从 repository 提取 GitHub 用户名 if (repository) { try { // 处理 git+https://github.com/... 或 https://github.com/... const repoUrl = repository.replace(/^git\+/, '').replace(/\.git$/, ''); const url = new URL(repoUrl); if (url.hostname === 'github.com' || url.hostname === 'www.github.com') { const parts = url.pathname.split('/').filter(Boolean); if (parts.length >= 1) { return `https://github.com/${parts[0]}.png`; } } } catch { // 忽略解析错误 } } // 2. 尝试从 homepage 提取 if (homepage) { try { const url = new URL(homepage); if (url.hostname === 'github.com' || url.hostname === 'www.github.com') { const parts = url.pathname.split('/').filter(Boolean); if (parts.length >= 1) { return `https://github.com/${parts[0]}.png`; } } else { // 如果是自定义域名,尝试获取 favicon return `https://api.iowen.cn/favicon/${url.hostname}.png`; } } catch { // 忽略解析错误 } } return undefined; } export interface PluginDisplayCardProps { data: PluginItem; onToggleStatus: () => Promise; onUninstall: () => Promise; onConfig?: () => void; hasConfig?: boolean; } const PluginDisplayCard: React.FC = ({ data, onToggleStatus, onUninstall, onConfig, hasConfig = false, }) => { const { name, version, author, description, status, homepage, repository, icon } = data; const isEnabled = status === 'active'; const [processing, setProcessing] = useState(false); const [isExpanded, setIsExpanded] = useState(false); const [backgroundImage] = useLocalStorage(key.backgroundImage, ''); const hasBackground = !!backgroundImage; // 优先使用后端返回的 icon URL(需要携带 token),否则尝试提取作者头像,最后兜底 Vercel 风格头像 const avatarUrl = getPluginIconUrl(icon) || getAuthorAvatar(homepage, repository) || `https://avatar.vercel.sh/${encodeURIComponent(name)}`; const handleToggle = () => { setProcessing(true); onToggleStatus().finally(() => setProcessing(false)); }; const handleUninstall = () => { setProcessing(true); onUninstall().finally(() => setProcessing(false)); }; return ( {/* Header */}

{name}

by {author || '未知'}

{status === 'active' ? '运行中' : status === 'stopped' ? '已停止' : '已禁用'}
{/* Description */}
setIsExpanded(!isExpanded)} >

{description || '暂无描述'}

{/* Version Badge */}
v{version}
{isEnabled ? '已启用' : '已禁用'}
{hasConfig && ( )} ); }; export default PluginDisplayCard;