Files
knowledge-governance/web/src/ProductionBindingsSection.tsx
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

155 lines
6.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useState } from 'react'
import { useOptionalAuth } from './auth'
import { fetchProductionBindingOptions, fetchProductionBindings, saveProductionBindings } from './productionBindings'
import type { ProductionBindings } from './productionBindings'
import type { KnowledgeBaseOption } from './routingBindings'
// 「生产知识库绑定」:发布链路的 源库 → 目标库 映射。这里选的是发布要写入的
// 生产(目标)知识库,与上面的「测试知识库绑定」(测试端收件库)是两件事。
export function ProductionBindingsSection() {
const auth = useOptionalAuth()
const isAdmin = auth?.identity?.role === 'admin'
const [bindings, setBindings] = useState<ProductionBindings | null>(null)
const [options, setOptions] = useState<KnowledgeBaseOption[]>([])
const [internalDraft, setInternalDraft] = useState('')
const [externalDraft, setExternalDraft] = useState('')
const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false)
const [error, setError] = useState<string | null>(null)
const [message, setMessage] = useState('')
const load = useCallback(async () => {
setLoading(true)
setError(null)
try {
const next = await fetchProductionBindings()
setBindings(next)
setInternalDraft(next.internal_kb_id)
setExternalDraft(next.external_kb_id)
try {
setOptions(await fetchProductionBindingOptions())
} catch {
setOptions([])
}
} catch (cause) {
setError(cause instanceof Error ? cause.message : '读取生产知识库绑定失败')
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
void load()
}, [load])
const dirty =
bindings !== null && (internalDraft !== bindings.internal_kb_id || externalDraft !== bindings.external_kb_id)
const sameSelection = internalDraft !== '' && internalDraft === externalDraft
const save = async () => {
setSaving(true)
setError(null)
setMessage('')
try {
const next = await saveProductionBindings({ internal_kb_id: internalDraft, external_kb_id: externalDraft })
setBindings(next)
const where = `对内 ${next.internal_kb_name || next.internal_kb_id.slice(0, 8) || '未绑定'} · 对外 ${next.external_kb_name || next.external_kb_id.slice(0, 8) || '未绑定'}`
setMessage(
next.production_connected
? `发布目标已更新:${where}。之后新发布的“对内/对外”内容会写入这两个生产库(已发布过的条目仍回到原目标)。`
: `发布目标已保存:${where};但当前核对不了生产端(${next.production_message}),下次发布前请确认。`
)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '保存生产知识库绑定失败')
} finally {
setSaving(false)
}
}
const optionLabel = (base: KnowledgeBaseOption) => `${base.name}(${base.knowledge_count} 文档)`
const renderSide = (label: string, boundID: string, boundName: string) => (
<div className={`kb-binding-side ${boundID ? 'bound' : 'warn'}`}>
<b>{label}</b>
<span>
{boundID ? boundName || `${boundID.slice(0, 8)}…` : '未绑定 · 按同 ID 语义发布(目标端须存在同 ID 库)'}
</span>
<small>{boundID ? 'UI 绑定 · 发布写入此库' : '未绑定:沿用测试库 ID'}</small>
</div>
)
const editorFor = (value: string, onChange: (next: string) => void, placeholder: string) =>
options.length > 0 ? (
<select value={value} onChange={(event) => onChange(event.target.value)} aria-label={placeholder}>
<option value="">{placeholder}</option>
{options.map((base) => (
<option key={base.id} value={base.id}>
{optionLabel(base)}
</option>
))}
</select>
) : (
<input
value={value}
onChange={(event) => onChange(event.target.value.trim())}
placeholder={`${placeholder}(输入生产知识库 ID)`}
aria-label={placeholder}
/>
)
return (
<section className="settings-panel wide" id="settings-production-bindings">
<div className="settings-section-heading">
<div>
<h2>生产知识库绑定</h2>
<p>
发布链路的“源库 → 目标库”:新发布的对内 / 对外内容按这里的绑定写入生产 WeKnora
的对应知识库。留空表示未绑定,按同 ID 语义发布。
</p>
</div>
<button onClick={() => void load()} disabled={loading}>
{loading ? '读取中…' : '重新读取'}
</button>
</div>
{error ? (
<p className="error-message" role="alert">
{error}
</p>
) : null}
{bindings ? (
<div className="kb-binding-grid">
{renderSide('对内生产知识库', bindings.internal_kb_id, bindings.internal_kb_name)}
{renderSide('对外生产知识库', bindings.external_kb_id, bindings.external_kb_name)}
</div>
) : null}
{bindings && !bindings.production_connected ? (
<p className="hint">
生产端核对不可用:{bindings.production_message}。
{bindings.production_base_url ? `(生产地址:${bindings.production_base_url})` : ''}
</p>
) : null}
{bindings && bindings.production_connected && bindings.production_base_url ? (
<p className="hint">
生产端:{bindings.production_base_url}
{bindings.updated_by ? ` · 最近修改:${bindings.updated_by}` : ''}
</p>
) : null}
{isAdmin && bindings ? (
<div className="kb-binding-editor">
<label>对内目标库 {editorFor(internalDraft, setInternalDraft, '选择对内生产知识库')}</label>
<label>对外目标库 {editorFor(externalDraft, setExternalDraft, '选择对外生产知识库')}</label>
<p className="hint">保存时会向生产端核对所选的库是否真实存在;对不上会拒收,生产端不可达则只提示。</p>
{sameSelection ? <p className="error-message">对内与对外必须绑定不同的生产知识库</p> : null}
<button onClick={() => void save()} disabled={saving || !dirty || sameSelection}>
{saving ? '正在核对并保存…' : '保存发布目标'}
</button>
{message ? (
<p className="success-message" role="status">
{message}
</p>
) : null}
</div>
) : null}
{!isAdmin ? <p className="hint">仅管理员可修改发布目标。</p> : null}
</section>
)
}