import './SourceManagement.css' import { useEffect, useState } from 'react' import { archiveSourceConnection, fetchSourceConnectionList, fetchSourceSettingsRevisions } from './sourceConnections' import { useOptionalAuth } from './auth' import type { SourceConnectionListItem } from './sourceConnections' import { fetchSourceAudit, sourceAuditActions } from './sourceSettingsAudit' import type { SourceAuditItem } from './sourceSettingsAudit' import { configuredInterval, saveSourceRefresh } from './sourceRefreshPolicy' const types: Record = { dingtalk: '钉钉知识库', pp: 'PP 资料下载中心', alist: 'AList 文件源', manual: '本地手工导入', } const choices = [ { value: 0, label: '仅手动刷新' }, { value: 3600, label: '每小时' }, { value: 21600, label: '每6小时' }, { value: 86400, label: '每24小时' }, ] const errorText = (error: unknown) => (error instanceof Error ? error.message : '操作失败,请重试') export function DingtalkAccountStatus() { const [status, setStatus] = useState('读取中…') const [expires, setExpires] = useState('') useEffect(() => { const controller = new AbortController() void fetch('/api/v1/dingtalk-spaces/credential-status', { headers: { Accept: 'application/json' }, signal: controller.signal, }) .then(async (response) => { if (!response.ok) throw new Error('读取失败') const value = await response.json() const data = value.data if (!data || typeof data.authenticated !== 'boolean' || typeof data.refresh_token_valid !== 'boolean') throw new Error('状态格式错误') if (controller.signal.aborted) return setStatus(!data.authenticated ? '未登录' : data.refresh_token_valid ? '已登录' : '凭证需更新') setExpires(data.authenticated && typeof data.refresh_expires_at === 'string' ? data.refresh_expires_at : '') }) .catch(() => { if (!controller.signal.aborted) setStatus('状态读取失败') }) return () => controller.abort() }, []) return (
DWS 凭证 · {status} {expires ? ( · 续期凭证有效期至 {new Date(expires).toLocaleString('zh-CN', { hour12: false })} ) : null}
仅显示本地凭证状态,不检测目录权限或业务接口。
) } export function SourceSettingsCard({ connection, onSourcesChanged, }: { connection: SourceConnectionListItem onSourcesChanged: () => void }) { const [open, setOpen] = useState(false) const [confirm, setConfirm] = useState(false) const [confirmRemove, setConfirmRemove] = useState(false) const isAdmin = useOptionalAuth()?.identity?.role === 'admin' const [busy, setBusy] = useState(false) const [message, setMessage] = useState('') const [error, setError] = useState('') const toggle = async () => { setBusy(true) setError('') setMessage('') try { await saveSourceRefresh(connection, !connection.enabled) setMessage(connection.enabled ? '已暂停自动刷新;已有知识不受影响。' : '已恢复刷新调度;仅手动策略不会自动扫描。') setConfirm(false) onSourcesChanged() } catch (cause) { setError(errorText(cause)) } finally { setBusy(false) } } const remove = async () => { if (!isAdmin || busy) return setBusy(true) setError('') try { await archiveSourceConnection(connection.id) setConfirmRemove(false) onSourcesChanged() } catch (cause) { setError(errorText(cause)) } finally { setBusy(false) } } const manual = connection.source_type === 'manual' return (
{connection.name} {types[connection.source_type] ?? connection.source_type}
{manual ? '手工来源' : !connection.enabled ? '自动刷新已暂停' : connection.next_refresh_at ? '自动刷新中' : '仅手动刷新'}
{!manual || isAdmin ? ( ) : null} {open ? (
{!manual ? ( ) : null} {isAdmin ? ( ) : null} 仅管理平台连接,不删除源端文件或已入库知识。
) : null} {message ? (

{message}

) : null} {error ? (

{error}

) : null} {confirmRemove ? (

移除「{connection.name}」的连接?

移除后将不再展示该来源,也不再安排新的自动扫描。

源端文件和已入库知识不会删除,历史记录保留。

{error ? (

{error}

) : null}
) : null} {confirm ? (

暂停「{connection.name}」自动刷新?

停止新的自动扫描任务入队,不取消已运行任务。已有知识、目录快照和标注均保留。

) : null}
) } export function SourceManagementDetails({ connections, onSourcesChanged, }: { connections: SourceConnectionListItem[] onSourcesChanged: () => void }) { const [policyOpen, setPolicyOpen] = useState(false) const [auditOpen, setAuditOpen] = useState(false) return (
setPolicyOpen(event.currentTarget.open)}> 刷新设置 {policyOpen ? : null}
setAuditOpen(event.currentTarget.open)}> 数据源设置审计 {auditOpen ? : null}
) } export function SourceRefreshPolicy({ connections, onSourcesChanged, }: { connections: SourceConnectionListItem[] onSourcesChanged: () => void }) { const [interval, setInterval] = useState(null) const [summary, setSummary] = useState('读取当前策略…') const [busy, setBusy] = useState(false) const [loading, setLoading] = useState(true) const [error, setError] = useState('') const [result, setResult] = useState('') useEffect(() => { let cancelled = false setLoading(true) setError('') const rows = connections.filter((item) => item.source_type !== 'manual') void Promise.all(rows.map(async (row) => configuredInterval(row, await fetchSourceSettingsRevisions(row.id)))) .then((values) => { if (cancelled) return const unique = [...new Set(values)] setInterval(unique.length === 1 ? unique[0] : null) setSummary( !values.length ? '尚无在线来源' : unique.length === 1 ? `当前策略:${choices.find((c) => c.value === unique[0])?.label ?? `每 ${unique[0]} 秒`}` : '当前来源策略不一致,请选择统一周期。' ) }) .catch((cause) => { if (!cancelled) setError(errorText(cause)) }) .finally(() => { if (!cancelled) setLoading(false) }) return () => { cancelled = true } }, [connections]) const save = async () => { if (interval === null) return setBusy(true) setError('') setResult('') let completed = 0 const failed: string[] = [] try { const rows = (await fetchSourceConnectionList()).filter((row) => row.source_type !== 'manual') for (const row of rows) { try { await saveSourceRefresh(row, row.enabled, interval) completed++ } catch (cause) { failed.push(`${row.name}:${errorText(cause)}`) } } setResult(`已保存 ${completed} 个来源${failed.length ? `,失败 ${failed.length} 个` : ''}。`) if (failed.length) setError(failed.join(';')) onSourcesChanged() } catch (cause) { setError(errorText(cause)) } finally { setBusy(false) } } return (

数据源自动刷新

{summary}

统一应用到当前已接入的在线来源,保留各来源的暂停状态。新增来源仍为手动刷新,需在此应用策略;不触发文件入库。

{result ?

{result}

: null} {error ? (

{error}

) : null}
) } export function SourceSettingsAudit({ connections }: { connections: SourceConnectionListItem[] }) { const [rows, setRows] = useState([]) const [total, setTotal] = useState(0) const [offset, setOffset] = useState(0) const [retry, setRetry] = useState(0) const [error, setError] = useState('') const [loading, setLoading] = useState(true) useEffect(() => { setOffset(0) setRows([]) setTotal(0) }, [connections]) useEffect(() => { const controller = new AbortController() setLoading(true) setError('') void fetchSourceAudit(offset, controller.signal) .then((page) => { if (controller.signal.aborted) return setRows((previous) => offset === 0 ? page.items : [...new Map([...previous, ...page.items].map((row) => [row.id, row])).values()] ) setTotal(page.total) }) .catch((cause) => { if (!controller.signal.aborted) setError(errorText(cause)) }) .finally(() => { if (!controller.signal.aborted) setLoading(false) }) return () => controller.abort() }, [offset, retry, connections]) return (

数据源设置审计

全部数据源的配置操作记录,按时间倒序展示,包含已移除的连接。

{rows.length ? (
{rows.map((row) => ( ))}
时间 操作人 数据源 操作 变更内容
{row.actor || '未记录'} {row.source_name} {sourceAuditActions[row.action] ?? '配置操作'} {row.detail}
) : null} {loading ?

加载操作记录中…

: null} {error ? (

{error}{' '}

) : null} {!loading && !error && !rows.length ?

暂无数据源配置操作记录。

: null} {rows.length ? (

已展示 {rows.length} / {total} 条记录

) : null} {!loading && !error && rows.length < total ? ( ) : null}
) }