feat(qa): 数据源配置改为统一添加向导
build / build (push) Successful in 1m6s

一个「+ 添加数据源」入口、五步固定(类型→授权→范围→校验→保存),类型只换候选来源与授权说明,
版式与动作词一致;对内/对外不在此配置,仍只在来源与目录标注。钉钉两类复用既有登记与扫码会话路径。
This commit is contained in:
hermes
2026-10-26 17:36:59 +08:00
parent 020cae1228
commit 740f0e69ba
4 changed files with 468 additions and 7 deletions
+313
View File
@@ -0,0 +1,313 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { DingtalkRequestError, registerDingtalkSpace } from './dingtalkSession'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import {
discoveryCandidates,
fetchAvailableDingtalkSources,
type AvailableDingtalkSources,
type DiscoveryCandidate,
} from './dingtalkSources'
import './sourceWizard.css'
// 「系统设置 → 数据源配置」的统一添加向导。
// 一个入口、五步固定:类型 → 授权 → 范围 → 校验 → 保存。
// 类型只决定「候选从哪来」与「授权怎么说明」,版式和动作词完全一致。
// 对内/对外不在这里配置:策略只在「来源与目录」标注。
// 单篇钉钉文档/表格不进这里——把链接发给我,我用 dws 导入。
type SourceKind = 'wiki' | 'drive' | 'pp'
const kinds: { key: SourceKind; title: string; detail: string; icon: string }[] = [
{ key: 'wiki', title: '钉钉知识库', detail: '组织 / 我的知识库', icon: '库' },
{ key: 'drive', title: '钉盘', detail: '我的文件 / 组织空间', icon: '盘' },
// PP 的登记端点还没建(凭据在服务端,向导不录密码),暂留在下方清单里管理,
// 端点就绪后再作为第三张卡放进来——避免点到最后保存不出去。
]
const steps = ['类型', '授权', '范围', '校验', '保存'] as const
export function SourceWizard({
registeredNames,
onRegistered,
}: {
registeredNames: string[]
onRegistered: () => void
}) {
const [open, setOpen] = useState(false)
const [step, setStep] = useState(0)
const [kind, setKind] = useState<SourceKind | null>(null)
const [list, setList] = useState<AvailableDingtalkSources | null>(null)
const [loading, setLoading] = useState(false)
const [loadError, setLoadError] = useState<string | null>(null)
const [checked, setChecked] = useState<string[]>([])
const [busy, setBusy] = useState(false)
const [failure, setFailure] = useState<string | null>(null)
const [message, setMessage] = useState<string | null>(null)
const [loginRequired, setLoginRequired] = useState(false)
const load = useCallback(async (signal?: AbortSignal) => {
setLoading(true)
setLoadError(null)
try {
setList(await fetchAvailableDingtalkSources(signal))
} catch (cause) {
if (cause instanceof DOMException && cause.name === 'AbortError') return
setLoadError(cause instanceof Error ? cause.message : '列举数据源失败')
setList(null)
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
if (!open || (kind !== 'wiki' && kind !== 'drive')) return
const controller = new AbortController()
void load(controller.signal)
return () => controller.abort()
}, [open, kind, load])
const candidates = useMemo(() => discoveryCandidates(list, registeredNames), [list, registeredNames])
const visible = useMemo(
() =>
candidates.filter((item) =>
kind === 'wiki' ? item.kind === 'knowledge_base' : kind === 'drive' ? item.kind === 'drive_space' : false
),
[candidates, kind]
)
const selected = visible.filter((item) => checked.includes(item.key))
const close = () => {
setOpen(false)
setStep(0)
setKind(null)
setChecked([])
setFailure(null)
setMessage(null)
setLoginRequired(false)
}
const toggle = (key: string) =>
setChecked((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]))
const submit = async (retryAfterLogin = false) => {
setBusy(true)
setFailure(null)
const added: string[] = []
const failed: string[] = []
let needLogin = false
for (const candidate of selected) {
try {
const result = await registerDingtalkSpace(candidate.url)
added.push(result.name || candidate.name)
} catch (cause) {
if (cause instanceof DingtalkRequestError && cause.code === 'login_required' && !retryAfterLogin) {
needLogin = true
break
}
failed.push(`${candidate.name}:${cause instanceof Error ? cause.message : '登记失败'}`)
}
}
setBusy(false)
if (needLogin) {
setLoginRequired(true)
setFailure('钉钉扫码会话已失效:扫码后会自动继续这次添加。')
return
}
if (added.length) {
setMessage(`已加入 ${added.length} 个来源:${added.join('、')}。首次目录扫描已在后台排队。`)
onRegistered()
void load()
}
if (failed.length) setFailure(failed.join(';'))
}
const canAdvance =
(step === 0 && kind !== null) ||
(step === 1 && kind !== null) ||
(step === 2 && (kind === 'pp' || selected.length > 0)) ||
(step === 3 && (kind === 'pp' || selected.length > 0))
return (
<section className="source-wizard" aria-label="添加数据源">
<div className="source-wizard-bar">
<div>
<h3>数据源</h3>
<p className="hint">
一个入口添加:{kinds.map((item) => item.title).join(' / ')}。对内/对外不在这里配置,去「来源与目录」标注。
单篇钉钉文档或表格把链接发给我,我用 dws 直接导入。
</p>
</div>
<button type="button" className="source-wizard-open" onClick={() => setOpen(true)}>
+ 添加数据源
</button>
</div>
{open ? (
<div className="source-wizard-panel" role="dialog" aria-label="添加数据源向导">
<ol className="source-wizard-steps">
{steps.map((label, index) => (
<li key={label} className={index === step ? 'now' : index < step ? 'done' : ''}>
{index + 1} {label}
</li>
))}
</ol>
{step === 0 ? (
<div className="source-wizard-types">
{kinds.map((item) => (
<button
key={item.key}
type="button"
className={kind === item.key ? 'type sel' : 'type'}
onClick={() => {
setKind(item.key)
setChecked([])
}}
>
<span aria-hidden="true">{item.icon}</span>
<b>{item.title}</b>
<small>{item.detail}</small>
</button>
))}
</div>
) : null}
{step === 1 ? (
<div className="source-wizard-auth">
{kind === 'pp' ? (
<>
<div className="row">
<span>授权方式</span>
<b>公开只读</b>
</div>
<div className="row">
<span>凭据</span>
<span>由服务端持有,无需填写</span>
</div>
<p className="hint">资料下载中心的登录凭据由运维放在服务端,向导里不录入任何密码。</p>
</>
) : (
<>
<div className="row">
<span>授权方式</span>
<b>当前钉钉账号扫码会话</b>
</div>
<div className="row">
<span>读取权限</span>
<span>只读;不写回、不删除源文件</span>
</div>
<p className="hint">
用的是平台已登录的 dws 凭据,不需要每个来源单独扫码;会话失效时会在保存时弹二维码。
</p>
</>
)}
</div>
) : null}
{step === 2 ? (
<div className="source-wizard-scope">
{kind === 'pp' ? (
<p className="hint">PP 只有一个公开只读门户,添加即可接入;文件清单在首次扫描时生成。</p>
) : loading ? (
<p className="hint">正在列举当前账号可见的来源…</p>
) : loadError ? (
<p role="alert">{loadError}</p>
) : visible.length === 0 ? (
<p className="hint">
当前账号没有可添加的{kind === 'wiki' ? '知识库' : '钉盘空间'};已加入的不会重复列出。
</p>
) : (
<>
<div className="source-wizard-check">
{visible.map((item) => (
<label key={item.key}>
<input type="checkbox" checked={checked.includes(item.key)} onChange={() => toggle(item.key)} />
<span>{item.name}</span>
</label>
))}
</div>
<p className="hint">
已选 {selected.length} 项。勾选空间即可,目录细节加入后再在「来源与目录」里标注。
</p>
</>
)}
</div>
) : null}
{step === 3 ? (
<div className="source-wizard-verify">
<ul>
{(kind === 'pp' ? [] : selected).map((item: DiscoveryCandidate) => (
<li key={item.key}>
<span>{item.name}</span>
<b className="ok">仍可见</b>
</li>
))}
{kind === 'pp' ? (
<li>
<span>Partner Portal 公开目录</span>
<b className="ok">公开只读可达</b>
</li>
) : null}
</ul>
<p className="hint">添加时会按当前账号再读一次;如果已看不到,会拒绝并提示刷新。</p>
</div>
) : null}
{step === 4 ? (
<div className="source-wizard-save">
{message ? <p role="status">{message}</p> : null}
{failure ? <p role="alert">{failure}</p> : null}
{!message && !failure ? (
<p className="hint">
将加入{' '}
{(kind === 'pp' ? ['Partner Portal'] : selected.map((item) => item.name)).join('、') || '(未选择)'}
。
</p>
) : null}
{loginRequired ? (
<DingtalkSessionControl
autoStart
hideTrigger
space={selected[0]?.url}
onSuccess={() => {
setLoginRequired(false)
void submit(true)
}}
/>
) : null}
</div>
) : null}
<div className="source-wizard-foot">
<button type="button" className="ghost" onClick={() => (step === 0 ? close() : setStep(step - 1))}>
{step === 0 ? '取消' : '上一步'}
</button>
{step < 3 ? (
<button type="button" className="primary" disabled={!canAdvance} onClick={() => setStep(step + 1)}>
下一步
</button>
) : step === 3 ? (
<button
type="button"
className="primary"
disabled={busy || !canAdvance}
onClick={() => {
setStep(4)
void submit()
}}
>
{busy ? '正在加入…' : '保存并首次扫描'}
</button>
) : (
<button type="button" className="primary" onClick={close}>
完成
</button>
)}
</div>
</div>
) : null}
</section>
)
}
+4 -3
View File
@@ -560,16 +560,17 @@ describe('SystemSettingsView 数据源配置的默认清单', () => {
// 三个非 PP 来源一个都不该渲染,也没有任何折叠开关。
for (const hidden of ['国内工业', 'ATOM知识库', '本地手工验收', '显示 AList', '折叠'])
expect(html).not.toContain(hidden)
expect(html).toContain('dws 导入(钉钉)')
// 统一入口:一个「+ 添加数据源」向导,不再有按类型分家的 dws 导入面板标题。
expect(html).toContain('+ 添加数据源')
expect(html).toContain('PP 导入')
expect(html).toContain('去掉来源</button>')
expect(html).toContain('端点与凭据')
// PP 来源的卡片操作照旧:改刷新周期、启停、看修订历史。
for (const kept of ['查看修订历史', '保存并启用调度', '停用来源', '自动刷新周期']) expect(html).toContain(kept)
for (const removed of ['+ 新增钉钉知识库', '文件格式筛选']) expect(html).not.toContain(removed)
// 文档工程师看不到 dws 导入面板与「去掉来源」:两者都是管理员动作。
// 文档工程师看不到添加向导与「去掉来源」:两者都是管理员动作。
const engineer = view('engineer')
expect(engineer).not.toContain('dws 导入(钉钉)')
expect(engineer).not.toContain('+ 添加数据源')
expect(engineer).not.toContain('去掉来源</button>')
expect(engineer).toContain('PP 导入')
})
+4 -4
View File
@@ -6,7 +6,7 @@ import { MembersSection } from './MembersSection'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import { RoutingBindingsSection } from './RoutingBindingsSection'
import { ProductionBindingsSection } from './ProductionBindingsSection'
import { DingtalkSourceDiscovery } from './DingtalkSourceDiscovery'
import { SourceWizard } from './SourceWizard'
import { useOptionalAuth } from './auth'
import { fetchMonitorOverview } from './monitor'
import type { MonitorOverview } from './monitor'
@@ -501,13 +501,13 @@ export function SystemSettingsView({
<div>
<h2>数据源配置</h2>
<p>
只留两个入口:<strong>dws 导入</strong>(按当前钉钉账号列出可访问的知识库与钉盘空间)与
<strong>PP 导入</strong>。逐来源设置自动刷新周期或保持仅手动,保存会先生成修订、验证后原子启用。
一个入口添加:<strong>钉钉知识库 / 钉盘</strong> 用当前钉钉账号列出可加入的空间,
单篇文档或表格把链接发给我用 dws 导入。对内/对外不在这里配置,去「来源与目录」标注。
</p>
</div>
</div>
{identity?.role === 'admin' ? (
<DingtalkSourceDiscovery
<SourceWizard
registeredNames={connections.map((item) => item.name)}
onRegistered={onSourcesChanged}
/>
+147
View File
@@ -0,0 +1,147 @@
.source-wizard {
display: flex;
flex-direction: column;
gap: 12px;
}
.source-wizard-bar {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
}
.source-wizard-bar h3 {
margin: 0 0 4px;
font-size: 15px;
}
.source-wizard-open {
border: 1px solid var(--accent, #2563eb);
color: var(--accent, #2563eb);
background: transparent;
border-radius: 8px;
padding: 7px 12px;
font: inherit;
font-weight: 600;
cursor: pointer;
white-space: nowrap;
}
.source-wizard-panel {
border: 1px solid var(--border, #d6dbe3);
border-radius: 12px;
padding: 14px 16px;
display: flex;
flex-direction: column;
gap: 12px;
}
.source-wizard-steps {
display: flex;
gap: 6px;
margin: 0;
padding: 0;
list-style: none;
}
.source-wizard-steps li {
flex: 1 1 60px;
border-top: 2px solid var(--border, #d6dbe3);
padding-top: 6px;
font-size: 11.5px;
color: #6b7280;
}
.source-wizard-steps li.done {
border-top-color: var(--accent, #2563eb);
color: inherit;
}
.source-wizard-steps li.now {
border-top-color: var(--accent, #2563eb);
color: var(--accent, #2563eb);
font-weight: 600;
}
.source-wizard-types {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 8px;
}
.source-wizard-types .type {
display: flex;
flex-direction: column;
gap: 2px;
text-align: left;
border: 1px solid var(--border, #d6dbe3);
border-radius: 9px;
padding: 10px 12px;
background: transparent;
font: inherit;
cursor: pointer;
}
.source-wizard-types .type.sel {
border-color: var(--accent, #2563eb);
box-shadow: inset 0 0 0 1px var(--accent, #2563eb);
}
.source-wizard-types .type span {
font-size: 15px;
}
.source-wizard-types .type small {
color: #6b7280;
font-size: 11.5px;
}
.source-wizard-auth .row,
.source-wizard-verify li {
display: flex;
justify-content: space-between;
gap: 12px;
border: 1px solid var(--border, #d6dbe3);
border-radius: 8px;
padding: 8px 10px;
font-size: 12.5px;
margin-bottom: 8px;
}
.source-wizard-verify ul {
list-style: none;
margin: 0;
padding: 0;
}
.source-wizard-check {
display: flex;
flex-direction: column;
gap: 6px;
max-height: 280px;
overflow: auto;
}
.source-wizard-check label {
display: flex;
align-items: center;
gap: 8px;
font-size: 12.5px;
border: 1px solid var(--border, #d6dbe3);
border-radius: 8px;
padding: 7px 10px;
}
.source-wizard-foot {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.source-wizard-foot .primary {
border: 1px solid var(--accent, #2563eb);
background: transparent;
color: var(--accent, #2563eb);
border-radius: 8px;
padding: 7px 12px;
font: inherit;
font-weight: 600;
cursor: pointer;
}
.source-wizard-foot .primary:disabled {
opacity: 0.45;
cursor: not-allowed;
}
.source-wizard-foot .ghost {
border: 1px solid var(--border, #d6dbe3);
background: transparent;
border-radius: 8px;
padding: 7px 12px;
font: inherit;
cursor: pointer;
}
.source-wizard .ok {
color: #15803d;
}