Some checks failed
Deploy Production / Build, push, deploy, verify (push) Has been cancelled
- 移除已迁移业务 AppData 运行时 fallback,改走领域 API 和关系表快读 - 补齐需求产品负责人、版本计划任务 JSON 和成员 username 回填迁移 - 统一治理字典入口,并补充 AI provider、数据源契约和领域服务测试 Co-Authored-By: Codex GPT-5 <codex@openai.com>
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
'use client';
|
|
import { create } from 'zustand';
|
|
import {
|
|
upsertDailySnapshot,
|
|
upsertInsight,
|
|
type XiaobaoRiskInsightCacheItem,
|
|
} from '@/lib/xiaobao-risk-cache';
|
|
import type { XiaobaoRiskSnapshot } from '@/lib/xiaobao-risk-trend';
|
|
|
|
interface XiaobaoRiskState {
|
|
snapshots: XiaobaoRiskSnapshot[];
|
|
insights: XiaobaoRiskInsightCacheItem[];
|
|
pendingInsightKeys: string[];
|
|
insightRequestAttempts: Record<string, string>;
|
|
riskDataLoaded: boolean;
|
|
error?: string;
|
|
fetchRiskData: () => Promise<void>;
|
|
saveSnapshot: (item: XiaobaoRiskSnapshot) => Promise<void>;
|
|
saveInsight: (item: XiaobaoRiskInsightCacheItem) => Promise<void>;
|
|
beginInsightUpdate: (key: string) => void;
|
|
finishInsightUpdate: (key: string) => void;
|
|
}
|
|
|
|
const RISK_DATA_CACHE_MS = 30_000;
|
|
let lastRiskDataFetchAt = 0;
|
|
|
|
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
|
snapshots: [],
|
|
insights: [],
|
|
pendingInsightKeys: [],
|
|
insightRequestAttempts: {},
|
|
riskDataLoaded: false,
|
|
error: undefined,
|
|
|
|
fetchRiskData: async () => {
|
|
if (get().riskDataLoaded && Date.now() - lastRiskDataFetchAt < RISK_DATA_CACHE_MS) return;
|
|
lastRiskDataFetchAt = Date.now();
|
|
set({
|
|
riskDataLoaded: true,
|
|
error: undefined,
|
|
});
|
|
},
|
|
|
|
saveSnapshot: async (item) => {
|
|
const optimistic = upsertDailySnapshot(get().snapshots, item);
|
|
set({ snapshots: optimistic, error: undefined });
|
|
},
|
|
|
|
saveInsight: async (item) => {
|
|
const optimistic = upsertInsight(get().insights, item);
|
|
set({ insights: optimistic, error: undefined });
|
|
},
|
|
|
|
beginInsightUpdate: (key) => {
|
|
if (!key) return;
|
|
if (get().pendingInsightKeys.includes(key)) return;
|
|
set({
|
|
pendingInsightKeys: [...get().pendingInsightKeys, key],
|
|
insightRequestAttempts: {
|
|
...get().insightRequestAttempts,
|
|
[key]: new Date().toISOString(),
|
|
},
|
|
});
|
|
},
|
|
|
|
finishInsightUpdate: (key) => {
|
|
if (!key) return;
|
|
set({ pendingInsightKeys: get().pendingInsightKeys.filter((item) => item !== key) });
|
|
},
|
|
}));
|