import { useCallback, useState } from 'react' import { Link } from 'react-router-dom' import { BookMarked, Check, ExternalLink, Info, RotateCw } from 'lucide-react' import clsx from 'clsx' import { ApiError, api } from '../api/client' import { Notice, Spinner, toast } from '../components/ui' import { CatalogImpactList } from '../components/CatalogImpact' import { CATALOG_PATCH_TEXT as T, CATALOG_STATUS_LABEL, CATALOG_UI_TEXT as UT } from '../lib/terms' import type { CatalogPatchChange } from '../types' import { beforeStatusShown, catalogTableHref, codeMeaning, patchBeforeText, patchSubmit, patchTarget, patchValueText, patchWhere, type CatalogPatch, } from './catalogPatch' // =========================================================================== // 「建议更新数据目录」卡片:助手把用户说的一条数据事实整理成目录修改提案(catalog_patch),这里逐项列出改前、 // 改后和理由,人点「保存到数据目录」才写入——服务端从不自动写。 // // 保存带着提案对照的版本:别人在这之后改过这张表就 409,卡片写明「这张表刚被修改过」,「重新载入」按最新的 // 目录重算改前和改后(预览接口,只读),人看过再保存。保存成功后列出受影响的模板(影响面),给到数据目录那张表的入口。 // // 卡片的结局(已保存、已忽略、重新载入后的内容)记在模块级的表里:画布右栏在对话层和运行层之间切换会卸载 // 卡片,回来时不能又变回「未保存」,再点一次只会撞上 409。 // =========================================================================== type Phase = 'open' | 'saving' | 'conflict' | 'reloading' | 'saved' | 'ignored' interface CardState { phase: Phase changes: CatalogPatchChange[] version: number /** 重新载入后已无法按建议保存的项(列已删除……) */ problems: string[] /** 保存被拒(422)时服务端的原话 */ error: string | null savedVersion?: number } const remembered = new Map() export function CatalogPatchCard({ patch, turnId, dense = false }: { patch: CatalogPatch /** 所在轮次:和 patch.key 一起认出「同一张卡片」 */ turnId: string dense?: boolean }) { const memo = `${turnId}:${patch.key}` const [state, setStateRaw] = useState(() => remembered.get(memo) ?? { phase: 'open', changes: patch.changes, version: patch.version, problems: [], error: null }) const setState = useCallback((next: Partial) => { setStateRaw((cur) => { const merged = { ...cur, ...next } remembered.set(memo, merged) return merged }) }, [memo]) const label = patch.tableLabel ?? patch.table const pending = state.changes.filter((c) => c.state !== 'same') const href = catalogTableHref(patch.sourceId, patch.table) const save = async () => { if (!pending.length) return setState({ phase: 'saving', error: null }) try { const d = await api.dataCatalog.patch(patch.sourceId, patch.table, patchSubmit(state.changes), state.version) setState({ phase: 'saved', savedVersion: d.version }) toast.ok(T.savedToast(label)) } catch (e) { if (e instanceof ApiError && e.status === 409) setState({ phase: 'conflict' }) else if (e instanceof ApiError && e.kind === 'http' && (e.status === 422 || e.status === 404)) { setState({ phase: 'open', error: e.message }) } else { setState({ phase: 'open' }) toast.error(e) } } } const reload = async () => { setState({ phase: 'reloading', error: null }) try { // 交回的是助手原来的提案:码值的补充要落在最新的目录上重算,不能沿用上一次算好的「改后」 const p = await api.dataCatalog.patchPreview(patch.sourceId, patch.table, patchSubmit(patch.changes)) setState({ phase: 'open', changes: p.changes, version: p.version, problems: p.problems }) } catch (e) { setState({ phase: 'conflict' }) toast.error(e) } } if (state.phase === 'ignored') { return (
{T.ignored}
) } const busy = state.phase === 'saving' || state.phase === 'reloading' const saved = state.phase === 'saved' const settled = saved || (!pending.length && state.phase === 'open') return (
{T.title} 「{label}」{patch.tableLabel && {patch.table}} · {T.sourceOf(patch.source)}
    {state.changes.map((c) => )}
{state.problems.length > 0 && (

{T.problems}

    {state.problems.map((p) =>
  • {p}
  • )}
)} {state.error && {state.error}} {state.phase === 'conflict' || state.phase === 'reloading' ? (

{T.conflictTitle}

{T.conflictBody}

) : settled ? (

{saved && state.savedVersion != null ? T.saved(state.savedVersion) : T.allSame} {T.open}

{saved && }
) : (
{T.hint}
)}
) } /** 一项:改到哪儿、改前、改后、理由 */ function ChangeRow({ change: c, dense }: { change: CatalogPatchChange; dense: boolean }) { const isCodes = patchTarget(c.path)?.kind === 'column' && c.path.endsWith('.codes') return (
  • {patchWhere(c)} {c.state === 'confirm' && {T.stateConfirm}} {c.state === 'same' && {T.stateSame}}
    {T.before}
    {patchBeforeText(c)} {beforeStatusShown(c) && ({CATALOG_STATUS_LABEL[c.before_status!]})}
    {T.after}
    {/* 被驳回的码值不算:保存时整项换成新的,改后的码值都是新加的 */} {isCodes ? : patchValueText(c.path, c.after)}
    {c.reason && ( <>
    {T.reason}
    {c.reason}
    )}
    {/* 服务端的说明(新增关系推算不出基数时):保存照样能存,但要让人知道哪一处空着、影响哪条检查 */} {c.note && (

    {UT.patchNote}:{c.note}

    )}
  • ) } /** 码值的改后:新加和改过的码加重,原有的照常写——一眼看出这条提案补了什么 */ function CodesAfter({ before, after }: { before: unknown; after: unknown }) { const prev = (before && typeof before === 'object' ? before : {}) as Record const entries = Object.entries((after && typeof after === 'object' ? after : {}) as Record) return ( {entries.map(([k, v], i) => ( {i > 0 && '、'} {k}={codeMeaning(v)} ))} ) }