Introduces a persistent plugins.json config to track enabled/disabled status for plugins, updates the plugin manager to respect this config when loading plugins, and adds API and frontend support for toggling plugin status. The backend now reports plugin status as 'active', 'stopped', or 'disabled', and the frontend displays these states with appropriate labels. Also updates the built-in plugin package.json with author info.
128 lines
4.3 KiB
TypeScript
128 lines
4.3 KiB
TypeScript
import { Button } from '@heroui/button';
|
|
import { Switch } from '@heroui/switch';
|
|
import { Chip } from '@heroui/chip';
|
|
|
|
import { useState } from 'react';
|
|
import { MdDeleteForever, MdPublishedWithChanges } from 'react-icons/md';
|
|
|
|
import DisplayCardContainer from './container';
|
|
import { PluginItem } from '@/controllers/plugin_manager';
|
|
|
|
export interface PluginDisplayCardProps {
|
|
data: PluginItem;
|
|
onReload: () => Promise<void>;
|
|
onToggleStatus: () => Promise<void>;
|
|
onUninstall: () => Promise<void>;
|
|
}
|
|
|
|
const PluginDisplayCard: React.FC<PluginDisplayCardProps> = ({
|
|
data,
|
|
onReload,
|
|
onToggleStatus,
|
|
onUninstall,
|
|
}) => {
|
|
const { name, version, author, description, status } = data;
|
|
const isEnabled = status !== 'disabled';
|
|
const [processing, setProcessing] = useState(false);
|
|
|
|
const handleToggle = () => {
|
|
setProcessing(true);
|
|
onToggleStatus().finally(() => setProcessing(false));
|
|
};
|
|
|
|
const handleReload = () => {
|
|
setProcessing(true);
|
|
onReload().finally(() => setProcessing(false));
|
|
};
|
|
|
|
const handleUninstall = () => {
|
|
setProcessing(true);
|
|
onUninstall().finally(() => setProcessing(false));
|
|
};
|
|
|
|
return (
|
|
<DisplayCardContainer
|
|
className='w-full max-w-[420px]'
|
|
action={
|
|
<div className='flex gap-2 w-full'>
|
|
<Button
|
|
fullWidth
|
|
radius='full'
|
|
size='sm'
|
|
variant='flat'
|
|
className='flex-1 bg-default-100 dark:bg-default-50 text-default-600 font-medium hover:bg-primary/20 hover:text-primary transition-colors'
|
|
startContent={<MdPublishedWithChanges size={16} />}
|
|
onPress={handleReload}
|
|
isDisabled={!isEnabled || processing}
|
|
>
|
|
重载
|
|
</Button>
|
|
|
|
<Button
|
|
fullWidth
|
|
radius='full'
|
|
size='sm'
|
|
variant='flat'
|
|
className='flex-1 bg-default-100 dark:bg-default-50 text-default-600 font-medium hover:bg-danger/20 hover:text-danger transition-colors'
|
|
startContent={<MdDeleteForever size={16} />}
|
|
onPress={handleUninstall}
|
|
isDisabled={processing}
|
|
>
|
|
卸载
|
|
</Button>
|
|
</div>
|
|
}
|
|
enableSwitch={
|
|
<Switch
|
|
isDisabled={processing}
|
|
isSelected={isEnabled}
|
|
onChange={handleToggle}
|
|
classNames={{
|
|
wrapper: 'group-data-[selected=true]:bg-primary-400',
|
|
}}
|
|
/>
|
|
}
|
|
title={name}
|
|
tag={
|
|
<Chip
|
|
className="ml-auto"
|
|
color={status === 'active' ? 'success' : status === 'stopped' ? 'warning' : 'default'}
|
|
size="sm"
|
|
variant="flat"
|
|
>
|
|
{status === 'active' ? '运行中' : status === 'stopped' ? '已停止' : '已禁用'}
|
|
</Chip>
|
|
}
|
|
>
|
|
<div className='grid grid-cols-2 gap-3'>
|
|
<div className='flex flex-col gap-1 p-3 bg-default-100/50 dark:bg-white/10 rounded-xl border border-transparent hover:border-default-200 transition-colors'>
|
|
<span className='text-xs text-default-500 dark:text-white/50 font-medium tracking-wide'>
|
|
版本
|
|
</span>
|
|
<div className='text-sm font-medium text-default-700 dark:text-white/90 truncate'>
|
|
{version}
|
|
</div>
|
|
</div>
|
|
<div className='flex flex-col gap-1 p-3 bg-default-100/50 dark:bg-white/10 rounded-xl border border-transparent hover:border-default-200 transition-colors'>
|
|
<span className='text-xs text-default-500 dark:text-white/50 font-medium tracking-wide'>
|
|
作者
|
|
</span>
|
|
<div className='text-sm font-medium text-default-700 dark:text-white/90 truncate'>
|
|
{author || '未知'}
|
|
</div>
|
|
</div>
|
|
<div className='col-span-2 flex flex-col gap-1 p-3 bg-default-100/50 dark:bg-white/10 rounded-xl border border-transparent hover:border-default-200 transition-colors'>
|
|
<span className='text-xs text-default-500 dark:text-white/50 font-medium tracking-wide'>
|
|
描述
|
|
</span>
|
|
<div className='text-sm font-medium text-default-700 dark:text-white/90 break-words line-clamp-2'>
|
|
{description || '暂无描述'}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</DisplayCardContainer>
|
|
);
|
|
};
|
|
|
|
export default PluginDisplayCard;
|