import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { fetchVersionHistory, fetchBuildInfo, reasonLabel, syncVersionHistory, type BuildInfo, type VersionHistoryItem, type VersionHistoryPage, } from './versionHistory' import { useOptionalAuth } from './auth' import './versionHistory.css' // 「版本记录」页:追踪远端 main、已交付开发快照及当前运行 SHA 的提交,按提交时间倒序。 // 只读:不提供打 tag / 发布 / 部署按钮;版本号只按证据展示,证据不足就说"历史提交"。 const FILTERS: ReadonlyArray<{ id: string; label: string }> = [ { id: 'all', label: '全部' }, { id: 'release', label: '正式' }, { id: 'development', label: '开发' }, { id: 'historical', label: '历史' }, ] const stamp = (value: string) => value && Number.isFinite(new Date(value).getTime()) ? new Date(value).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false, }) : '—' const kindLabel: Record = { release: '正式版本', development: '开发版本', historical: '历史提交' } const runningText = (running: VersionHistoryPage['current_running']) => { if (running.state === 'matched') return `当前运行:${running.display_version}(${running.commit_sha.slice(0, 12)},已匹配到下面的提交)` if (running.state === 'development') { return `当前运行:开发构建 ${running.display_version}(${running.commit_sha.slice(0, 12)})—— 这份产物不在已同步的提交里` } return '当前运行版本未知(产物没有注入可核验的提交标识)' } const syncText = (sync: VersionHistoryPage['sync']) => { if (!sync.available) return '未接线:还没有配置只读的仓库来源' if (!sync.status) return '尚未同步过' if (sync.status === 'running' || sync.status === 'pending') return `同步中(${sync.status})` if (sync.status === 'failed') { return `上次同步失败(${sync.error_code || '未知原因'})· 仍展示上次成功的数据(${stamp(sync.last_success_at)})` } if (!sync.complete) { return `上次同步:部分历史(${sync.fetched_count}/${sync.expected_count ?? '?'})· ${stamp(sync.last_success_at)}` } return `最后同步:${stamp(sync.last_success_at)}(${sync.fetched_count} 条提交)` } interface ViewProps { page: VersionHistoryPage | null build: BuildInfo | null filter: string loading: boolean loadingMore: boolean error: string | null notice: string | null expanded: string | null | undefined canSync: boolean onRefresh: () => void onFilter: (id: string) => void onLoadMore: () => void onSync: () => void onExpand: (sha: string | null) => void } export function VersionHistoryView(props: ViewProps) { const { page, build } = props const items = page?.items ?? [] const expandedSHA = props.expanded === undefined ? (items.find( (item) => page?.current_running.state === 'matched' && item.commit_sha === page.current_running.matched_commit_sha )?.commit_sha ?? items[0]?.commit_sha ?? null) : props.expanded return (

版本记录

查看当前构建,以及每次更新带来的变化。

{props.canSync ? ( ) : null}

当前版本

运行版本
{page ? page.current_running.state === 'unknown' ? '未知' : page.current_running.display_version : build?.identity_available ? build.display_version : '未知'}
构建类型
{build?.build_kind === 'development' ? '开发构建' : build?.build_kind === 'release' ? '正式构建' : '未知'}
提交
{(page ? page.current_running.commit_sha : build?.identity_available ? build.commit_sha : '') || '—'}
构建时间(北京时间)
{build && (!page || build.commit_sha === page.current_running.commit_sha) ? stamp(build.built_at) : '—'}

{page ? runningText(page.current_running) : '当前运行版本未知'}

每一版改了什么

记录来自仓库,只读展示,不执行发布或部署。

{page ? syncText(page.sync) : '正在读取同步状态…'} {page?.sync.status === 'running' || page?.sync.status === 'pending' ? ( 同步中 ) : null} {page?.sync.status === 'failed' ? 同步失败 : null}

{props.error ? (
{props.error}
) : null} {props.notice ?
{props.notice}
: null} {items.length === 0 && !props.loading ? (
{page?.sync.available ? '暂无记录(先同步一次,或该筛选下没有条目)' : '版本记录未接线:等待配置只读的仓库来源'}
) : null}
    {items.map((item) => ( props.onExpand(expandedSHA === item.commit_sha ? null : item.commit_sha)} /> ))}
{props.loading ?
正在读取版本记录…
: null} {page?.has_more ? ( ) : null}
) } function ChangeNotes({ text }: { text: string }) { const categories: Record = { Added: { label: '新增', tone: 'added' }, Changed: { label: '调整', tone: 'changed' }, Fixed: { label: '修复', tone: 'fixed' }, Security: { label: '安全', tone: 'security' }, } let category = { label: '说明', tone: 'note' } return (
{text.split(/\r?\n/).map((line, index) => { const trimmed = line.trim() if (!trimmed) return null const heading = /^#{1,6}\s+(.+)$/.exec(trimmed) if (heading) { if (categories[heading[1]]) { category = categories[heading[1]] return null } return

{heading[1]}

} const content = trimmed.replace(/^[-*]\s+/, '') return (

{category.label} {content .split(/(\*\*[^*]+\*\*|`[^`]+`)/g) .map((part, i) => part.startsWith('**') && part.endsWith('**') ? ( {part.slice(2, -2)} ) : part.startsWith('`') && part.endsWith('`') ? ( {part.slice(1, -1)} ) : ( part ) )}

) })}
) } function TimelineEntry({ item, currentMatchedSHA, expanded, onExpand, }: { item: VersionHistoryItem currentMatchedSHA: string expanded: boolean onExpand: () => void }) { const heading = item.version_label || `历史提交 · ${item.short_sha}` const note = item.version_state === 'verified' ? item.release_notes : '' const isCurrent = Boolean(item.commit_sha && item.commit_sha === currentMatchedSHA) const detailID = 'version-detail-' + item.commit_sha return (
  • {!item.version_label && item.record_kind === 'historical' ? (

    版本号未能核验:{reasonLabel(item.version_reason)}

    ) : null} {expanded ? (
    {note ? ( ) : (

    此提交未附单独的变更说明,可查看下方原始提交。

    )}
    原始记录与提交信息
    提交
    {item.commit_sha}
    作者
    {item.author_display || '—'}
    提交标题
    {item.subject}
    {note ?
    {note}
    : null} {item.body ?
    {item.body}
    : null}
    {item.commit_url ? ( 查看提交 ) : null} {item.release_tags .filter((tag) => tag.url) .map((tag) => ( 查看 tag {tag.name} ))}
    ) : null}
  • ) } export function VersionHistory() { const auth = useOptionalAuth() const [page, setPage] = useState(null) const [build, setBuild] = useState(null) const [filter, setFilter] = useState('all') const [loading, setLoading] = useState(true) const [loadingMore, setLoadingMore] = useState(false) const [error, setError] = useState(null) const [notice, setNotice] = useState(null) const [expanded, setExpanded] = useState(undefined) const cursor = useRef('') const load = useCallback(async (kind: string, append: boolean) => { append ? setLoadingMore(true) : setLoading(true) try { const next = await fetchVersionHistory({ kind, cursor: append ? cursor.current : '' }) cursor.current = next.next_cursor setPage((current) => (append && current ? { ...next, items: [...current.items, ...next.items] } : next)) setError(null) } catch (cause) { setError(cause instanceof Error ? cause.message : '加载版本记录失败') } finally { append ? setLoadingMore(false) : setLoading(false) } }, []) useEffect(() => { void load(filter, false) }, [filter, load]) useEffect(() => { let cancelled = false void fetchBuildInfo() .then((value) => { if (!cancelled) setBuild(value) }) .catch(() => undefined) return () => { cancelled = true } }, []) // 只有管理员能看到同步入口;其他人只能看(服务端也会再判一次角色) const canSync = auth?.identity?.role === 'admin' const onSync = useCallback(async () => { setNotice(null) try { const accepted = await syncVersionHistory() setNotice(accepted.reused ? '已有同步任务在跑,本次复用它。' : '已提交同步任务,稍后刷新即可看到新记录。') await load(filter, false) } catch (cause) { setError(cause instanceof Error ? cause.message : '触发同步失败') } }, [filter, load]) const viewProps = useMemo( () => ({ page, build, filter, loading, loadingMore, error, notice, expanded, canSync, onRefresh: () => void load(filter, false), onFilter: setFilter, onLoadMore: () => void load(filter, true), onSync: () => void onSync(), onExpand: setExpanded, }), [page, build, filter, loading, loadingMore, error, notice, expanded, canSync, load, onSync] ) return }