diff --git a/web/src/SourceWizard.tsx b/web/src/SourceWizard.tsx new file mode 100644 index 0000000..e43cbf5 --- /dev/null +++ b/web/src/SourceWizard.tsx @@ -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(null) + const [list, setList] = useState(null) + const [loading, setLoading] = useState(false) + const [loadError, setLoadError] = useState(null) + const [checked, setChecked] = useState([]) + const [busy, setBusy] = useState(false) + const [failure, setFailure] = useState(null) + const [message, setMessage] = useState(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 ( +
+
+
+

数据源

+

+ 一个入口添加:{kinds.map((item) => item.title).join(' / ')}。对内/对外不在这里配置,去「来源与目录」标注。 + 单篇钉钉文档或表格把链接发给我,我用 dws 直接导入。 +

+
+ +
+ + {open ? ( +
+
    + {steps.map((label, index) => ( +
  1. + {index + 1} {label} +
  2. + ))} +
+ + {step === 0 ? ( +
+ {kinds.map((item) => ( + + ))} +
+ ) : null} + + {step === 1 ? ( +
+ {kind === 'pp' ? ( + <> +
+ 授权方式 + 公开只读 +
+
+ 凭据 + 由服务端持有,无需填写 +
+

资料下载中心的登录凭据由运维放在服务端,向导里不录入任何密码。

+ + ) : ( + <> +
+ 授权方式 + 当前钉钉账号扫码会话 +
+
+ 读取权限 + 只读;不写回、不删除源文件 +
+

+ 用的是平台已登录的 dws 凭据,不需要每个来源单独扫码;会话失效时会在保存时弹二维码。 +

+ + )} +
+ ) : null} + + {step === 2 ? ( +
+ {kind === 'pp' ? ( +

PP 只有一个公开只读门户,添加即可接入;文件清单在首次扫描时生成。

+ ) : loading ? ( +

正在列举当前账号可见的来源…

+ ) : loadError ? ( +

{loadError}

+ ) : visible.length === 0 ? ( +

+ 当前账号没有可添加的{kind === 'wiki' ? '知识库' : '钉盘空间'};已加入的不会重复列出。 +

+ ) : ( + <> +
+ {visible.map((item) => ( + + ))} +
+

+ 已选 {selected.length} 项。勾选空间即可,目录细节加入后再在「来源与目录」里标注。 +

+ + )} +
+ ) : null} + + {step === 3 ? ( +
+
    + {(kind === 'pp' ? [] : selected).map((item: DiscoveryCandidate) => ( +
  • + {item.name} + 仍可见 +
  • + ))} + {kind === 'pp' ? ( +
  • + Partner Portal 公开目录 + 公开只读可达 +
  • + ) : null} +
+

添加时会按当前账号再读一次;如果已看不到,会拒绝并提示刷新。

+
+ ) : null} + + {step === 4 ? ( +
+ {message ?

{message}

: null} + {failure ?

{failure}

: null} + {!message && !failure ? ( +

+ 将加入{' '} + {(kind === 'pp' ? ['Partner Portal'] : selected.map((item) => item.name)).join('、') || '(未选择)'} + 。 +

+ ) : null} + {loginRequired ? ( + { + setLoginRequired(false) + void submit(true) + }} + /> + ) : null} +
+ ) : null} + +
+ + {step < 3 ? ( + + ) : step === 3 ? ( + + ) : ( + + )} +
+
+ ) : null} +
+ ) +} diff --git a/web/src/SystemSettings.test.tsx b/web/src/SystemSettings.test.tsx index 3a92d63..ddebbb3 100644 --- a/web/src/SystemSettings.test.tsx +++ b/web/src/SystemSettings.test.tsx @@ -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('去掉来源') 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('去掉来源') expect(engineer).toContain('PP 导入') }) diff --git a/web/src/SystemSettings.tsx b/web/src/SystemSettings.tsx index 66581bf..51c03ab 100644 --- a/web/src/SystemSettings.tsx +++ b/web/src/SystemSettings.tsx @@ -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({

数据源配置

- 只留两个入口:dws 导入(按当前钉钉账号列出可访问的知识库与钉盘空间)与 - PP 导入。逐来源设置自动刷新周期或保持仅手动,保存会先生成修订、验证后原子启用。 + 一个入口添加:钉钉知识库 / 钉盘 用当前钉钉账号列出可加入的空间, + 单篇文档或表格把链接发给我用 dws 导入。对内/对外不在这里配置,去「来源与目录」标注。

{identity?.role === 'admin' ? ( - item.name)} onRegistered={onSourcesChanged} /> diff --git a/web/src/sourceWizard.css b/web/src/sourceWizard.css new file mode 100644 index 0000000..941d2e2 --- /dev/null +++ b/web/src/sourceWizard.css @@ -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; +}