财务分析清理同步相关代码
移除能力检查、同步按钮、进度追踪等未用代码, 仅靠 status.available 判断数据是否存在。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { useState, useEffect } from 'react'
|
import { useState } from 'react'
|
||||||
import { RefreshCw, Lock, Loader2, X, Search, FileText, Database, CheckCircle2, Hourglass, Lightbulb, ExternalLink } from 'lucide-react'
|
import { Loader2, Search, FileText, Database, Lightbulb, ExternalLink, X } from 'lucide-react'
|
||||||
import { PageHeader } from '@/components/PageHeader'
|
import { PageHeader } from '@/components/PageHeader'
|
||||||
import { EmptyState } from '@/components/EmptyState'
|
import { EmptyState } from '@/components/EmptyState'
|
||||||
import { useFinancialStatus } from '@/lib/useFinancials'
|
import { useFinancialStatus } from '@/lib/useFinancials'
|
||||||
@@ -9,7 +9,6 @@ import { ReportHistoryPanel } from '@/components/financials/ReportHistoryPanel'
|
|||||||
import { LastStockChip } from '@/components/LastStockChip'
|
import { LastStockChip } from '@/components/LastStockChip'
|
||||||
import { useLastStock } from '@/lib/useLastStock'
|
import { useLastStock } from '@/lib/useLastStock'
|
||||||
import { fmtBigNum } from '@/lib/format'
|
import { fmtBigNum } from '@/lib/format'
|
||||||
import { toast } from '@/components/Toast'
|
|
||||||
|
|
||||||
const TABLE_LABELS: Record<string, string> = {
|
const TABLE_LABELS: Record<string, string> = {
|
||||||
metrics: '核心指标',
|
metrics: '核心指标',
|
||||||
@@ -70,70 +69,13 @@ export function Financials() {
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// 有同步数据, 继续展示
|
// 有同步数据, 继续展示
|
||||||
}
|
|
||||||
|
|
||||||
const handleSync = (table: string) => {
|
|
||||||
// 防重复点击:syncing 中不再触发(后端 trigger 也有 _is_syncing 兜底)
|
|
||||||
if (syncing) return
|
|
||||||
// 记录开始时间: 全量同步判断所有 4 张表, 单表同步只判断这一张
|
|
||||||
setSyncStartedAt(Date.now())
|
|
||||||
setSyncSingleTable(table === 'all' ? null : table)
|
|
||||||
syncMut.mutate(table, {
|
|
||||||
onSuccess: (r) => {
|
|
||||||
// 后端 trigger 立即返回 started 状态;若被防并发跳过(已有同步在进行),
|
|
||||||
// 给用户明确反馈,并清空本次误设的记录。
|
|
||||||
if (!r.synced?.started) {
|
|
||||||
if (r.synced?.reason === 'already running') {
|
|
||||||
toast('财务数据正在同步中,请稍候', 'success')
|
|
||||||
} else if (r.synced?.reason === 'no FINANCIAL capability') {
|
|
||||||
// 能力未就绪:通常发生在升级/刷新 Key 后调度器状态未同步 —— 提示用户检查 Key
|
|
||||||
toast('财务数据能力未就绪,请检查 API Key 或刷新页面后重试', 'error')
|
|
||||||
} else {
|
|
||||||
toast(`同步未能开始${r.synced?.reason ? `:${r.synced.reason}` : ''}`, 'error')
|
|
||||||
}
|
|
||||||
setSyncStartedAt(null)
|
|
||||||
setSyncSingleTable(null)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onError: () => {
|
|
||||||
// 请求失败:清空本次记录(request 已弹错误 toast)
|
|
||||||
setSyncStartedAt(null)
|
|
||||||
setSyncSingleTable(null)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const tables = status?.tables ?? {}
|
const tables = status?.tables ?? {}
|
||||||
const available = status?.available ?? false
|
const available = status?.available ?? false
|
||||||
const lastSync = status?.last_sync ?? {}
|
|
||||||
// 本次同步进度: 仅当 syncStartedAt 存在且 syncing 时, 按 last_sync 时间戳判断
|
|
||||||
const isFullSync = syncing && syncStartedAt && !syncSingleTable // 全量同步
|
|
||||||
const isSingleSync = syncing && syncStartedAt && !!syncSingleTable // 单表同步
|
|
||||||
const TABLE_ORDER = ['metrics', 'income', 'balance_sheet', 'cash_flow'] as const
|
|
||||||
const tableDoneThisRound = (key: string): boolean => {
|
|
||||||
if (!syncStartedAt || !syncing) return false
|
|
||||||
// 单表同步: 只判断这一张表是否完成
|
|
||||||
if (syncSingleTable && key !== syncSingleTable) return false
|
|
||||||
const ls = lastSync[key]
|
|
||||||
if (!ls) return false
|
|
||||||
return new Date(ls).getTime() >= syncStartedAt
|
|
||||||
}
|
|
||||||
// 当前正在同步的表:
|
|
||||||
// 全量同步 → 第一个未完成的; 单表同步 → 那张表(未完成时)
|
|
||||||
const currentSyncingTable = syncing && syncStartedAt
|
|
||||||
? (syncSingleTable
|
|
||||||
? (tableDoneThisRound(syncSingleTable) ? null : syncSingleTable)
|
|
||||||
: TABLE_ORDER.find(t => !tableDoneThisRound(t)) ?? null)
|
|
||||||
: null
|
|
||||||
const syncedCount = TABLE_ORDER.filter(t => tableDoneThisRound(t)).length
|
|
||||||
// 卡片三态: 仅全量同步时未轮到的表显示"等待"; 单表同步时其他表保持原样
|
|
||||||
const isWaitingTable = (key: string): boolean =>
|
|
||||||
!!isFullSync && !tableDoneThisRound(key) && currentSyncingTable !== key
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -143,40 +85,13 @@ export function Financials() {
|
|||||||
right={
|
right={
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<LastStockChip stock={lastStock} onSelect={pick} />
|
<LastStockChip stock={lastStock} onSelect={pick} />
|
||||||
{syncing && (
|
|
||||||
<span className="text-xs text-accent/80 flex items-center gap-1.5">
|
|
||||||
<Loader2 className="w-3 h-3 animate-spin" />
|
|
||||||
{isFullSync
|
|
||||||
? `已同步 ${syncedCount}/4 张表…`
|
|
||||||
: isSingleSync
|
|
||||||
? `同步${TABLE_LABELS[syncSingleTable!] ?? syncSingleTable}…`
|
|
||||||
: '同步中…'}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
{hasFinancial && (
|
|
||||||
<button
|
|
||||||
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-btn bg-gradient-to-r from-accent/25 to-accent/10 border border-accent/30 text-accent text-xs font-medium hover:from-accent/35 hover:to-accent/20 transition-all duration-150 disabled:opacity-40 disabled:cursor-not-allowed"
|
|
||||||
onClick={() => handleSync('all')}
|
|
||||||
disabled={syncing}
|
|
||||||
title={syncing ? '正在同步,请稍候…' : '同步全部财务表'}
|
|
||||||
>
|
|
||||||
{syncing
|
|
||||||
? <Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
||||||
: <RefreshCw className="h-3.5 w-3.5" />}
|
|
||||||
{syncing ? '同步中…' : '全部同步'}
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="px-8 py-6 space-y-6 max-w-7xl">
|
<div className="px-8 py-6 space-y-6 max-w-7xl">
|
||||||
{syncing && (
|
|
||||||
<div className="flex items-center gap-2 rounded-card border border-accent/30 bg-accent/[0.06] px-3 py-2 text-xs text-accent">
|
|
||||||
<Loader2 className="h-3.5 w-3.5 animate-spin shrink-0" />
|
|
||||||
正在从 TickFlow 拉取财务数据,请稍候…
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* 同步状态卡片 —— 始终显示,反映本地财务数据概况 */}
|
{/* 同步状态卡片 —— 始终显示,反映本地财务数据概况 */}
|
||||||
{!isLoading && available && (
|
{!isLoading && available && (
|
||||||
@@ -187,49 +102,22 @@ export function Financials() {
|
|||||||
const TIcon = TABLE_ICON[key] ?? Database
|
const TIcon = TABLE_ICON[key] ?? Database
|
||||||
const hasData = (info?.rows ?? 0) > 0
|
const hasData = (info?.rows ?? 0) > 0
|
||||||
// 本次同步三态: 完成 / 同步中 / 等待 (仅全量同步时未轮到的表才"等待")
|
// 本次同步三态: 完成 / 同步中 / 等待 (仅全量同步时未轮到的表才"等待")
|
||||||
const doneThisRound = tableDoneThisRound(key)
|
|
||||||
const isThisSyncing = currentSyncingTable === key
|
|
||||||
const isWaiting = isWaitingTable(key)
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={key}
|
key={key}
|
||||||
className={`rounded-card border p-3.5 transition-colors flex flex-col ${
|
className={`rounded-card border p-3.5 transition-colors flex flex-col ${
|
||||||
isThisSyncing
|
hasData
|
||||||
? 'border-accent/40 bg-accent/[0.04]'
|
|
||||||
: isWaiting
|
|
||||||
? 'border-border/50 bg-elevated/15'
|
|
||||||
: hasData
|
|
||||||
? 'border-border bg-surface'
|
? 'border-border bg-surface'
|
||||||
: 'border-dashed border-border/60 bg-elevated/20'
|
: 'border-dashed border-border/60 bg-elevated/20'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<div className="flex items-center gap-1.5">
|
<div className="flex items-center gap-1.5">
|
||||||
{doneThisRound ? (
|
|
||||||
<CheckCircle2 className="h-3.5 w-3.5 text-emerald-400" />
|
|
||||||
) : isThisSyncing ? (
|
|
||||||
<Loader2 className="h-3.5 w-3.5 animate-spin text-accent" />
|
|
||||||
) : isWaiting ? (
|
|
||||||
<Hourglass className="h-3.5 w-3.5 text-muted/60" />
|
|
||||||
) : (
|
|
||||||
<TIcon className={`h-3.5 w-3.5 ${hasData ? 'text-accent' : 'text-muted'}`} />
|
<TIcon className={`h-3.5 w-3.5 ${hasData ? 'text-accent' : 'text-muted'}`} />
|
||||||
)}
|
|
||||||
<span className="text-xs font-medium text-foreground">{label}</span>
|
<span className="text-xs font-medium text-foreground">{label}</span>
|
||||||
</div>
|
</div>
|
||||||
{hasFinancial ? (
|
|
||||||
<button
|
|
||||||
className="text-muted hover:text-accent transition-colors disabled:opacity-30 disabled:cursor-not-allowed"
|
|
||||||
onClick={() => handleSync(key)}
|
|
||||||
disabled={syncing}
|
|
||||||
title={syncing ? '正在同步…' : `同步${label}`}
|
|
||||||
>
|
|
||||||
{syncing
|
|
||||||
? <Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
||||||
: <RefreshCw className="h-3.5 w-3.5" />}
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<div className="h-3.5 w-3.5" />
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-2 text-xl font-semibold tabular-nums text-foreground">
|
<div className="mt-2 text-xl font-semibold tabular-nums text-foreground">
|
||||||
{fmtBigNum(info?.rows ?? 0)}
|
{fmtBigNum(info?.rows ?? 0)}
|
||||||
|
|||||||
Reference in New Issue
Block a user