71f6311399
与新的「生产知识库绑定」(发布 源库→目标库)配对,避免两处同名分不清。 同步改分区标题、说明、错误文案与测试断言。
87 lines
6.0 KiB
TypeScript
87 lines
6.0 KiB
TypeScript
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>
|
||
} |