steelforesight/src/data/risks.ts

70 lines
2.2 KiB
TypeScript

/* ─────────────────────────────────────────────────────────────
Market risk alerts (Scanner page). Static fallback mirrors the
original hardcoded list; replaced at startup by bootstrapRisks()
when the panel is reachable. Mutated in place so importers stay live.
───────────────────────────────────────────────────────────── */
export type RiskLevel = 'high' | 'medium' | 'low';
export interface RiskAlert {
title: string;
desc: string;
level: RiskLevel;
}
export const riskAlerts: RiskAlert[] = [
{
title: 'فشار صادرات چین',
desc: 'افزایش ۱۸٪ صادرات ارزان چین فشار قیمتی روی بازار آسیا',
level: 'high',
},
{
title: 'محدودیت انرژی داخلی',
desc: 'کاهش تولید زمستانی به دلیل محدودیت گاز صنعتی',
level: 'medium',
},
{
title: 'فرصت بازار عراق',
desc: 'کاهش واردات ترکیه، فرصت افزایش سهم بازار برای ایران',
level: 'low',
},
];
/* panel risk levels (critical/high/medium/low/opportunity) → Scanner's 3 levels */
function mapLevel(level: unknown): RiskLevel {
switch (level) {
case 'critical':
case 'high':
return 'high';
case 'medium':
return 'medium';
case 'low':
case 'opportunity':
default:
return 'low';
}
}
const PANEL_API =
(import.meta as ImportMeta & { env?: Record<string, string> }).env?.VITE_PANEL_API ||
'';
export async function bootstrapRisks(): Promise<void> {
try {
const res = await fetch(`${PANEL_API}/api/risks?limit=100`);
if (!res.ok) return;
const fetched = await res.json();
if (!Array.isArray(fetched) || fetched.length === 0) return;
riskAlerts.length = 0;
riskAlerts.push(
...fetched.map((r: Record<string, unknown>) => ({
title: String(r.name ?? ''),
desc: String(r.quote ?? ''),
level: mapLevel(r.level),
})),
);
} catch {
/* panel offline → keep static fallback */
}
}