013ce4a24e
build / build (push) Successful in 1m3s
收录 main、注释开发快照和完整运行 SHA;开发说明绑定原提交,补记 0.1.1–0.1.25。 进行中同步按仓库合并,完成后重新执行;启动入队,部署脚本读回版本证据。 开发快照独立于正式标签与 Release,既有冲突标签不移动。 已运行:整仓 make verify,Go 673 通过、前端 274 通过,92 个已声明可选跳过,必需跳过 0。 隔离 PostgreSQL 重复同步队列用例及 -race 通过;未更新现有 8088、未修改生产问答。
289 lines
11 KiB
TypeScript
289 lines
11 KiB
TypeScript
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 ? new Date(value).toLocaleString('zh-CN', { hour12: false }) : '—')
|
||
const kindLabel: Record<string, string> = { 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
|
||
canSync: boolean
|
||
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 ?? []
|
||
return (
|
||
<section className="version-history" aria-label="版本记录">
|
||
<header className="version-history-header">
|
||
<div>
|
||
<h2>版本记录</h2>
|
||
<p>
|
||
{page
|
||
? runningText(page.current_running)
|
||
: build
|
||
? `当前运行:${build.identity_available ? build.display_version : '未知'}`
|
||
: '当前运行版本未知'}
|
||
</p>
|
||
<p className="version-history-sync-line">
|
||
{page ? syncText(page.sync) : '正在读取同步状态…'}
|
||
{page?.sync.status === 'running' || page?.sync.status === 'pending' ? (
|
||
<span className="version-history-badge pending">同步中</span>
|
||
) : null}
|
||
{page && page.sync.status === 'failed' ? (
|
||
<span className="version-history-badge warn">同步失败</span>
|
||
) : null}
|
||
</p>
|
||
</div>
|
||
{props.canSync ? (
|
||
<button type="button" onClick={props.onSync} disabled={props.loading || !page?.sync.available}>
|
||
{page?.sync.status === 'running' || page?.sync.status === 'pending' ? '同步进行中…' : '同步记录'}
|
||
</button>
|
||
) : null}
|
||
</header>
|
||
|
||
{props.error ? (
|
||
<div className="error-banner" role="alert">
|
||
{props.error}
|
||
</div>
|
||
) : null}
|
||
{props.notice ? <div className="version-history-notice">{props.notice}</div> : null}
|
||
|
||
<nav className="version-history-filters" aria-label="按记录类型筛选">
|
||
{FILTERS.map((entry) => (
|
||
<button
|
||
type="button"
|
||
key={entry.id}
|
||
className={props.filter === entry.id ? 'active' : ''}
|
||
aria-pressed={props.filter === entry.id}
|
||
onClick={() => props.onFilter(entry.id)}
|
||
>
|
||
{entry.label}
|
||
</button>
|
||
))}
|
||
</nav>
|
||
|
||
{items.length === 0 && !props.loading ? (
|
||
<div className="empty-state">
|
||
{page?.sync.available
|
||
? '暂无记录(先同步一次,或该筛选下没有条目)'
|
||
: '版本记录未接线:等待配置只读的仓库来源'}
|
||
</div>
|
||
) : null}
|
||
|
||
<ol className="version-history-timeline">
|
||
{items.map((item) => (
|
||
<TimelineEntry
|
||
key={item.commit_sha}
|
||
item={item}
|
||
// 「当前运行」以页面级判定为准(同一份事实),条目标志只做兜底
|
||
currentMatchedSHA={page?.current_running.state === 'matched' ? page.current_running.matched_commit_sha : ''}
|
||
expanded={props.expanded === item.commit_sha}
|
||
onExpand={() => props.onExpand(props.expanded === item.commit_sha ? null : item.commit_sha)}
|
||
/>
|
||
))}
|
||
</ol>
|
||
|
||
{props.loading ? <div className="empty-state">正在读取版本记录…</div> : null}
|
||
{page?.has_more ? (
|
||
<button type="button" className="version-history-more" onClick={props.onLoadMore} disabled={props.loadingMore}>
|
||
{props.loadingMore ? '正在加载…' : '加载更多'}
|
||
</button>
|
||
) : null}
|
||
</section>
|
||
)
|
||
}
|
||
|
||
function TimelineEntry({
|
||
item,
|
||
currentMatchedSHA,
|
||
expanded,
|
||
onExpand,
|
||
}: {
|
||
item: VersionHistoryItem
|
||
currentMatchedSHA: string
|
||
expanded: boolean
|
||
onExpand: () => void
|
||
}) {
|
||
const isRelease = item.record_kind === 'release' && item.version_state === 'verified'
|
||
const heading = isRelease ? item.version_label : item.version_label || `历史提交 · ${item.short_sha}`
|
||
const note = item.version_state === 'verified' ? item.release_notes : ''
|
||
const hasDetail = Boolean(item.body) || Boolean(note)
|
||
return (
|
||
<li className={`version-history-entry ${item.record_kind}`}>
|
||
<div className="version-history-entry-head">
|
||
<strong>{heading}</strong>
|
||
<span>{stamp(item.committed_at)}</span>
|
||
</div>
|
||
<div className="version-history-entry-meta">
|
||
<span className={`version-history-badge ${item.record_kind}`}>{kindLabel[item.record_kind] ?? '历史提交'}</span>
|
||
<span className="version-history-sha">{item.short_sha}</span>
|
||
{item.commit_sha && item.commit_sha === currentMatchedSHA ? (
|
||
<span className="version-history-badge current">当前运行</span>
|
||
) : null}
|
||
<span className="version-history-author">{item.author_display}</span>
|
||
</div>
|
||
<p className="version-history-subject">{item.subject}</p>
|
||
{!item.version_label && item.record_kind === 'historical' ? (
|
||
<p className="version-history-reason">版本号未能核验:{reasonLabel(item.version_reason)}</p>
|
||
) : null}
|
||
{hasDetail ? (
|
||
<>
|
||
<button type="button" className="version-history-toggle" onClick={onExpand} aria-expanded={expanded}>
|
||
{expanded ? '收起详情' : '展开详情'}
|
||
</button>
|
||
{expanded ? (
|
||
<div className="version-history-detail">
|
||
{note ? <pre>{note}</pre> : null}
|
||
{item.body ? <pre>{item.body}</pre> : null}
|
||
</div>
|
||
) : null}
|
||
</>
|
||
) : null}
|
||
<div className="version-history-links">
|
||
{item.commit_url ? (
|
||
<a href={item.commit_url} target="_blank" rel="noreferrer noopener">
|
||
查看提交
|
||
</a>
|
||
) : null}
|
||
{/* 没有链接就不渲染:空 href 点下去像"没反应/空白",比不显示更糟 */}
|
||
{item.release_tags
|
||
.filter((tag) => tag.url)
|
||
.map((tag) => (
|
||
<a key={tag.name} href={tag.url} target="_blank" rel="noreferrer noopener">
|
||
查看 tag {tag.name}
|
||
</a>
|
||
))}
|
||
</div>
|
||
</li>
|
||
)
|
||
}
|
||
|
||
export function VersionHistory() {
|
||
const auth = useOptionalAuth()
|
||
const [page, setPage] = useState<VersionHistoryPage | null>(null)
|
||
const [build, setBuild] = useState<BuildInfo | null>(null)
|
||
const [filter, setFilter] = useState('all')
|
||
const [loading, setLoading] = useState(true)
|
||
const [loadingMore, setLoadingMore] = useState(false)
|
||
const [error, setError] = useState<string | null>(null)
|
||
const [notice, setNotice] = useState<string | null>(null)
|
||
const [expanded, setExpanded] = useState<string | null>(null)
|
||
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<ViewProps>(
|
||
() => ({
|
||
page,
|
||
build,
|
||
filter,
|
||
loading,
|
||
loadingMore,
|
||
error,
|
||
notice,
|
||
expanded,
|
||
canSync,
|
||
onFilter: setFilter,
|
||
onLoadMore: () => void load(filter, true),
|
||
onSync: () => void onSync(),
|
||
onExpand: setExpanded,
|
||
}),
|
||
[page, build, filter, loading, loadingMore, error, notice, expanded, canSync, load, onSync]
|
||
)
|
||
|
||
return <VersionHistoryView {...viewProps} />
|
||
}
|