'use client'; import * as React from 'react'; import { Input } from '@/components/ui/input'; import { Switch } from '@/components/ui/switch'; import { ManageDetailPanel, ManagePage, } from '@/components/common/general/manage-pannel'; import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { ShieldCheck } from 'lucide-react'; import { formatDateTime } from '@/lib/utils'; import type { SystemConfig } from '@/lib/services/admin'; import { AdminProvider, useAdmin } from '@/contexts/admin-context'; /** * 系统配置 * 显示系统配置的详细信息和编辑面板 * * @example * ```tsx * * ``` * @param {SystemConfig} config - 系统配置 * @param {Partial} editData - 编辑数据 * @param {function} onEditDataChange - 编辑数据改变回调 * @param {function} onSave - 保存回调 * @param {boolean} saving - 是否正在保存 * @returns {React.ReactNode} 系统配置详情面板组件 */ function SystemConfigDetailPanel({ config, editData, onEditDataChange, onSave, saving, }: { config: SystemConfig | null; editData: Partial; onEditDataChange: ( field: keyof SystemConfig, value: SystemConfig[keyof SystemConfig], ) => void; onSave: () => void; saving: boolean; }) { return ( 配置键 {config?.key} 配置值 {config?.key.endsWith('_enabled') ? ( { onEditDataChange('value', checked ? 'true' : 'false'); }} /> ) : ( { const value = e.target.value; if (value === '') { onEditDataChange('value', ''); return; } onEditDataChange('value', value); }} className='!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-inner-spin-button]' style={{ MozAppearance: 'textfield', }} /> )} 配置描述 { const value = e.target.value; onEditDataChange('description', value); }} className='!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px]' /> 公共可见 { onEditDataChange('visibility', checked ? 1 : 0); }} /> 配置类型 {config?.type === 'system' ? '系统配置' : '业务配置'} 创建时间 {config ? formatDateTime(config.created_at) : ''} ); } /** * 系统配置管理组件 * * @example * ```tsx * * ``` * @returns {React.ReactNode} 系统配置管理组件 */ export function SystemConfigs() { const { systemConfigs: configs, systemConfigsLoading: loading, systemConfigsError: error, refetchSystemConfigs, updateSystemConfig, } = useAdmin(); const [activeTab, setActiveTab] = React.useState<'system' | 'business'>( 'business', ); React.useEffect(() => { refetchSystemConfigs(activeTab); }, [activeTab, refetchSystemConfigs]); const getInitialEditData = (config: SystemConfig) => ({ value: config.value, visibility: config.visibility, description: config.description, }); const handleSave = async ( config: SystemConfig, editData: Partial, ) => { if (!config) return; await updateSystemConfig(config.key, { value: editData.value ?? config.value, visibility: editData.visibility ?? config.visibility, description: editData.description ?? config.description, }); }; return ( title='系统配置' icon={ShieldCheck} data={configs} loading={loading} error={error} onReload={() => refetchSystemConfigs(activeTab)} getInitialEditData={getInitialEditData} onSave={handleSave} getId={(config) => config.key} emptyDescription='未发现系统配置' loadingDescription='配置加载中' headerExtra={ setActiveTab(val as 'system' | 'business')} className='w-[180px]' > 业务配置 系统配置 } columns={[ { header: '配置键', cell: (item) => ( {item.key} ), width: '200px', }, { header: '配置值', cell: (item) => ( {item.value} ), width: '120px', }, { header: '公共可见', cell: (item) => ( {item.visibility === 1 ? '可见' : '不可见'} ), width: '80px', }, { header: '描述', cell: (item) => ( {item.description} ), width: '200px', }, ]} renderDetail={({ selected, hovered, editData, onEditDataChange, onSave, saving, }) => ( )} /> ); } export default function SystemConfigPage() { return ( ); }
{config?.key}
{config ? formatDateTime(config.created_at) : ''}