Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
/**
* PluginEntry - 插件入口组件
* 根据插件状态显示不同颜色
*/
'use client'
import type { SidebarCommonProps } from '../../types'
import { Puzzle } from 'lucide-react'
import { useShallow } from 'zustand/react/shallow'
import { useTransClient } from '@/app/i18n/client'
import { PluginModal, PluginUpdatePopover } from '@/components/Plugin'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'
import { usePluginStore } from '@/store/plugin'
import { PluginStatus } from '@/store/plugin/types/baseTypes'
import { cn } from '@/utils/className'
export function PluginEntry({ collapsed }: SidebarCommonProps) {
const { t } = useTransClient('common')
const { hostAccessGranted, pluginNeedsUpdate, pluginStatus, pluginVersion, pluginModalVisible, openPluginModal, closePluginModal } = usePluginStore(
useShallow(state => ({
hostAccessGranted: state.hostAccessGranted,
pluginNeedsUpdate: state.pluginNeedsUpdate,
pluginStatus: state.status,
pluginVersion: state.pluginVersion,
pluginModalVisible: state.pluginModalVisible,
openPluginModal: state.openPluginModal,
closePluginModal: state.closePluginModal,
})),
)
// 根据插件状态返回对应的颜色和状态文本
const getStatusInfo = () => {
if (pluginStatus === PluginStatus.READY && pluginNeedsUpdate) {
return {
iconColor: 'text-warning',
dotColor: 'bg-warning',
statusText: t('pluginStatus.updateAvailable'),
statusColor: 'text-warning',
}
}
switch (pluginStatus) {
case PluginStatus.READY:
return {
iconColor: 'text-success',
dotColor: 'bg-success',
statusText: t('pluginStatus.ready'),
statusColor: 'text-success',
}
case PluginStatus.INSTALLED_NO_PERMISSION:
return {
iconColor: 'text-warning',
dotColor: 'bg-warning',
statusText: hostAccessGranted === false
? t('pluginStatus.siteAccessRequired')
: t('pluginStatus.noPermission'),
statusColor: 'text-warning',
}
case PluginStatus.CHECKING:
return {
iconColor: 'text-info',
dotColor: 'bg-info animate-pulse',
statusText: t('pluginStatus.checking'),
statusColor: 'text-info',
}
case PluginStatus.NOT_INSTALLED:
case PluginStatus.UNKNOWN:
default:
return {
iconColor: 'text-muted-foreground/70',
dotColor: 'bg-muted-foreground/70',
statusText: t('pluginStatus.notInstalled'),
statusColor: 'text-muted-foreground/70',
}
}
}
const { iconColor, dotColor, statusText, statusColor } = getStatusInfo()
const statusNode = (() => {
if (collapsed)
return null
if (pluginNeedsUpdate && pluginVersion) {
return (
<PluginUpdatePopover currentVersion={pluginVersion} side="right" align="end">
<span
className={cn('text-xs', statusColor)}
data-testid="sidebar-plugin-status"
>
{statusText}
</span>
</PluginUpdatePopover>
)
}
return (
<span className={cn('text-xs', statusColor)} data-testid="sidebar-plugin-status">
{statusText}
</span>
)
})()
const content = (
<button
onClick={openPluginModal}
data-testid="sidebar-plugin"
className={cn(
'flex w-full cursor-pointer items-center rounded-lg border-none bg-transparent text-muted-foreground transition-colors hover:bg-brand-cyan/10 hover:text-brand-cyan',
collapsed ? 'h-9 w-9 justify-center' : 'justify-between px-3 py-2',
)}
>
<div className="flex items-center gap-2">
<div className="relative">
<Puzzle size={18} className={iconColor} />
{/* 状态指示点 */}
<span
className={cn(
'absolute -right-0.5 -top-0.5 h-2 w-2 rounded-full border border-background',
dotColor,
)}
/>
</div>
{!collapsed && <span className="text-sm">{t('plugin')}</span>}
</div>
{statusNode}
</button>
)
return (
<>
{collapsed ? (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>{content}</TooltipTrigger>
<TooltipContent side="right">
<p>
{t('plugin')}
{' '}
-
{statusText}
</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
) : (
content
)}
{/* 插件状态弹框 */}
<PluginModal visible={pluginModalVisible} onClose={closePluginModal} />
</>
)
}