Files
Profeto/frontend/src/admin/SettingRow.tsx
T
shangfangjian f71f29b4cb fix(frontend): 按审计报告修复全部17项问题,数据层迁至 src/api/
P0(4): 假数据清除(avg_latency_ms:2400→null,无端点字段改null)、假进度条改诚实的不确定态、
  公共页不再反向依赖 admin(api/public.ts)、PredictProgress 重写
P1(6): 23处 any 归零(对齐后端 Pydantic 契约新增 PredictionMatchRef/HealthProbe 等)、
  a11y(aria-live 0→4,htmlFor 1→17,aria-describedby/invalid 补齐)、App.tsx 抽 SiteLayout、
  路由级 lazy+代码分割(首屏 315KB→238KB)、index.html 补 SEO/favicon/OG、死代码清理(AdminIcon 抽出)
P2(7): Login/index.css 裸色值令牌化、groupByDate useMemo、滚动监听统一、原生控件基元化、
  useLeagues 静默失败补告警、路由级 ErrorBoundary

另修复审计未列问题:
- Monitoring todos 过滤器 t!==false 放行 null 导致整页崩溃 → Boolean(t) 真值过滤
- Collection 渲染期 Date.now()(react-hooks/purity 捕获)→ 计时器 effect
- bg-press-wash/60 透明度修饰符静默失效 → RGB 三元组 + 构建期令牌守卫(下个提交接入)

工程化:数据层 dal/api/types(1047行)git mv 至 src/api/,admin 留 @deprecated 兼容壳,
  21 个引用方直指新路径;tsconfig 开启 noUnusedLocals/noUnusedParameters(清理9处存量)
2026-09-22 23:45:06 +08:00

160 lines
5.5 KiB
TypeScript

/**
* Admin 后台 - 配置项行组件(报刊风)
*
* 展示态:键名 + 来源徽标(数据库覆盖 / .env 默认 / 未配置) + 脱敏值 + 操作按钮
* 编辑态:输入框 + 保存/取消
* 由数据源页与 LLM 配置页共用。
*/
import { useEffect, useState } from 'react'
import type { DataSourceSetting } from '../api/types'
import { Badge, Spinner } from './components'
import { Button, Input } from '../components/ui'
export const ORIGIN_BADGE: Record<DataSourceSetting['origin'], { text: string; status: 'success' | 'info' | 'error' }> = {
db: { text: '数据库覆盖', status: 'success' },
env: { text: '.env 默认', status: 'info' },
none: { text: '未配置', status: 'error' },
}
export default function SettingRow({
setting,
editing,
busy,
onEdit,
onCancel,
onSave,
onClear,
detectModels,
}: {
setting: DataSourceSetting
editing: boolean
busy: boolean
onEdit: () => void
onCancel: () => void
onSave: (value: string) => void
onClear: () => void
/** 可选:编辑态提供「检测可用模型」能力(如 LLM_MODEL 行) */
detectModels?: () => Promise<string[]>
}) {
const [value, setValue] = useState('')
const origin = ORIGIN_BADGE[setting.origin]
// 行内模型检测
const [detecting, setDetecting] = useState(false)
const [detected, setDetected] = useState<string[] | null>(null)
const [detectError, setDetectError] = useState('')
// 进入编辑态时清空上次的检测结果
useEffect(() => {
if (editing) {
setDetected(null)
setDetectError('')
}
}, [editing])
async function handleDetect() {
if (!detectModels || detecting) return
setDetecting(true)
setDetectError('')
try {
setDetected(await detectModels())
} catch (err) {
setDetected(null)
setDetectError(err instanceof Error ? err.message : '检测失败')
} finally {
setDetecting(false)
}
}
if (editing) {
return (
<div className="border-b border-ink-200 py-2.5 last:border-b-0">
<div className="mb-1.5 flex flex-wrap items-center gap-1.5 text-2xs text-ink-500">
<span className="break-all font-mono text-ink-800">{setting.key}</span>
{setting.sensitive && <Badge status="warning">敏感</Badge>}
</div>
<div className="flex flex-col gap-2 sm:flex-row">
<Input
type={setting.sensitive ? 'password' : 'text'}
value={value}
onChange={e => setValue(e.target.value)}
placeholder={`输入新的 ${setting.label}`}
/* 无可见 label(标题即配置项名),用 aria-label 提供可访问名称 */
aria-label={`设置项 ${setting.label} 的值`}
autoFocus
autoComplete="off"
className="flex-1"
/>
<div className="flex gap-2">
<Button variant="solid" size="sm" onClick={() => onSave(value)} disabled={!value.trim() || busy}>
{busy ? (<><Spinner /> 保存中</>) : '保存'}
</Button>
<Button size="sm" onClick={onCancel} disabled={busy}>
取消
</Button>
</div>
</div>
{detectModels && (
<div className="mt-2 space-y-2">
<Button size="sm" onClick={handleDetect} disabled={detecting}>
{detecting ? (<><Spinner /> 检测中</>) : detected ? '重新检测' : '检测可用模型'}
</Button>
{detectError && (
<p className="border-l-2 border-press bg-press-wash/40 px-3 py-1.5 text-2xs leading-relaxed text-press-dark">
{detectError}
</p>
)}
{detected && detected.length > 0 && (
<div className="max-h-48 overflow-y-auto border border-ink-200">
{detected.map(id => (
<button
key={id}
type="button"
onClick={() => setValue(id)}
className={`flex w-full items-center justify-between gap-3 border-b border-ink-200 px-3 py-1.5 text-left last:border-b-0 hover:bg-paper-100 ${
value === id ? 'bg-press-wash/50' : ''
}`}
>
<span className="min-w-0 break-all font-mono text-2xs text-ink-800">{id}</span>
{value === id && <Badge status="success">已选</Badge>}
</button>
))}
</div>
)}
{detected && detected.length === 0 && !detectError && (
<p className="text-2xs text-ink-400">服务未返回可用模型</p>
)}
</div>
)}
</div>
)
}
return (
<div className="space-y-1.5 border-b border-ink-200 py-2.5 last:border-b-0">
<div className="flex flex-wrap items-center gap-x-2 gap-y-1">
<span className="break-all font-mono text-2xs text-ink-800">{setting.key}</span>
<Badge status={origin.status}>{origin.text}</Badge>
</div>
<div className="break-all font-mono text-2xs leading-relaxed text-ink-500">
{setting.configured ? setting.masked : '—'}
</div>
<div className="flex justify-end gap-2">
<Button size="sm" onClick={onEdit} disabled={busy}>
{setting.configured ? '更换' : '配置'}
</Button>
{setting.origin === 'db' && (
<Button size="sm" onClick={onClear} disabled={busy} title="删除数据库覆盖值,回落 .env">
回落 .env
</Button>
)}
</div>
</div>
)
}