-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFundCard.tsx
More file actions
171 lines (150 loc) · 5.44 KB
/
FundCard.tsx
File metadata and controls
171 lines (150 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { FC, useMemo } from 'react';
import { Badge, Card } from 'react-bootstrap';
import { INDEX_CATEGORY_LABELS, INDEX_RISK_LABELS } from '../../constants/finance';
import styles from '../../styles/Finance.module.scss';
import { IndexFundSnapshot, IndexHistoryPoint } from '../../types/finance';
export interface FundCardProps {
data: IndexFundSnapshot;
}
const formatNumber = (value: number | null | undefined) =>
value == null ? '--' : value.toLocaleString('zh-CN', { maximumFractionDigits: 2 });
const formatPercent = (value: number | null | undefined) =>
value == null ? '--' : `${(value * 100).toFixed(2)}%`;
const valueTone = (value: number | null | undefined) =>
value == null ? styles.mutedText : value >= 0 ? styles.positiveText : styles.negativeText;
const Sparkline: FC<{ points: IndexHistoryPoint[]; chartId: string }> = ({ points, chartId }) => {
const { polyline, gradientStops } = useMemo(() => {
if (!points.length) return { polyline: '', gradientStops: [] as number[] };
const values = points.map(point => point.value);
const min = Math.min(...values);
const max = Math.max(...values);
const delta = max - min || 1;
const polylinePoints = points
.map((point, index) => {
const x = (index / Math.max(points.length - 1, 1)) * 100;
const y = ((max - point.value) / delta) * 40;
return `${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(' ');
const gradientOffsets = [0, 50, 100];
return { polyline: polylinePoints, gradientStops: gradientOffsets };
}, [points]);
if (!points.length) return <div className={styles.sparklinePlaceholder}>数据准备中</div>;
const gradientId = `${chartId}-gradient`;
return (
<div className={styles.sparkline}>
<svg viewBox="0 0 100 40" role="img" aria-label="近 60 日走势">
<defs>
<linearGradient id={gradientId} x1="0%" y1="0%" x2="100%" y2="0%">
{gradientStops.map(offset => (
<stop
key={offset}
offset={`${offset}%`}
stopColor="var(--bs-primary)"
stopOpacity="0.5"
/>
))}
</linearGradient>
</defs>
<polyline
fill="none"
stroke={`url(#${gradientId})`}
strokeWidth="2"
strokeLinejoin="round"
strokeLinecap="round"
points={polyline}
/>
</svg>
</div>
);
};
export const FundCard: FC<FundCardProps> = ({ data }) => {
const {
displayName,
category,
riskLevel,
latestValue,
dailyChangePct,
oneYearReturnPct,
maxDrawdownPct,
tags,
sparkline,
updatedAt,
fallback,
description,
} = data;
const sparklineId = useMemo(() => `fund-${data.symbol}`, [data.symbol]);
return (
<Card className={`${styles.fundCard} h-100`}>
<Card.Body className="d-flex flex-column gap-3">
<div
className={`${styles.fundCardHeader} d-flex justify-content-between align-items-start`}
>
<div>
<h3 className="h5 mb-1">{displayName}</h3>
<div className="d-flex flex-wrap gap-2 align-items-center">
<Badge bg="light" text="dark">
{INDEX_CATEGORY_LABELS[category]}
</Badge>
<Badge
bg={
riskLevel === 'aggressive'
? 'danger'
: riskLevel === 'balanced'
? 'warning'
: 'success'
}
>
{INDEX_RISK_LABELS[riskLevel]}
</Badge>
{fallback && (
<Badge bg="secondary" text="light">
离线数据
</Badge>
)}
</div>
</div>
<small className="text-muted text-end">
数据源 <br />
{data.source.historyEndpoint}
</small>
</div>
<p className="text-muted mb-0">{description}</p>
<div className={`${styles.metricRow} d-flex flex-wrap gap-4 align-items-center`}>
<div>
<p className="text-muted mb-1">最新点位</p>
<span className={styles.metricValue}>{formatNumber(latestValue)}</span>
</div>
<div>
<p className="text-muted mb-1">日涨跌</p>
<strong className={valueTone(dailyChangePct)}>{formatPercent(dailyChangePct)}</strong>
</div>
<div>
<p className="text-muted mb-1">近 1 年收益</p>
<strong className={valueTone(oneYearReturnPct)}>
{formatPercent(oneYearReturnPct)}
</strong>
</div>
<div>
<p className="text-muted mb-1">最大回撤</p>
<strong className={valueTone(maxDrawdownPct)}>{formatPercent(maxDrawdownPct)}</strong>
</div>
</div>
<Sparkline points={sparkline} chartId={sparklineId} />
<div className="d-flex flex-wrap gap-2">
{tags?.map(tag => (
<Badge key={tag} bg="light" text="dark" className={styles.tagBadge}>
{tag}
</Badge>
))}
</div>
<div className="d-flex justify-content-between align-items-center">
<small className="text-muted">更新于 {updatedAt || '--'}</small>
<a href={`/finance/${data.symbol}`} className="text-primary fw-semibold">
查看详情 →
</a>
</div>
</Card.Body>
</Card>
);
};