|
| 1 | +import { FC, useContext, useMemo } from 'react'; |
| 2 | +import { Badge, Card } from 'react-bootstrap'; |
| 3 | + |
| 4 | +import { INDEX_CATEGORY_LABEL_KEYS, INDEX_RISK_LABEL_KEYS } from '../../constants/finance'; |
| 5 | +import { I18nContext } from '../../models/Translation'; |
| 6 | +import { IndexFundSnapshot } from '../../types/finance'; |
| 7 | +import styles from './Finance.module.less'; |
| 8 | +import { SparkLine } from './SparkLine'; |
| 9 | + |
| 10 | +const formatNumber = (value?: number | null, locale = 'zh-CN') => |
| 11 | + value != null ? value.toLocaleString(locale, { maximumFractionDigits: 2 }) : '--'; |
| 12 | + |
| 13 | +const formatPercent = (value?: number | null) => |
| 14 | + value != null ? `${(value * 100).toFixed(2)}%` : '--'; |
| 15 | + |
| 16 | +const valueTone = (value?: number | null) => |
| 17 | + value != null ? (value >= 0 ? styles.positiveText : styles.negativeText) : styles.mutedText; |
| 18 | + |
| 19 | +export const FundCard: FC<IndexFundSnapshot> = ({ |
| 20 | + symbol, |
| 21 | + displayName, |
| 22 | + category, |
| 23 | + riskLevel, |
| 24 | + latestValue, |
| 25 | + dailyChangePct, |
| 26 | + oneYearReturnPct, |
| 27 | + maxDrawdownPct, |
| 28 | + tags, |
| 29 | + sparkline, |
| 30 | + updatedAt, |
| 31 | + fallback, |
| 32 | + description, |
| 33 | + source, |
| 34 | +}) => { |
| 35 | + const { currentLanguage, t } = useContext(I18nContext); |
| 36 | + const sparklineId = useMemo(() => `fund-${symbol}`, [symbol]); |
| 37 | + const updatedAtISO = updatedAt ? new Date(updatedAt).toJSON() : undefined; |
| 38 | + |
| 39 | + return ( |
| 40 | + <Card className={`${styles.fundCard} h-100`}> |
| 41 | + <Card.Body className="d-flex flex-column gap-3"> |
| 42 | + <div className={`${styles.header} d-flex justify-content-between align-items-start`}> |
| 43 | + <div> |
| 44 | + <h3 className="h5 mb-1">{displayName}</h3> |
| 45 | + <div className="d-flex flex-wrap gap-2 align-items-center"> |
| 46 | + <Badge bg="light" text="dark"> |
| 47 | + {t(INDEX_CATEGORY_LABEL_KEYS[category])} |
| 48 | + </Badge> |
| 49 | + <Badge |
| 50 | + bg={ |
| 51 | + riskLevel === 'aggressive' |
| 52 | + ? 'danger' |
| 53 | + : riskLevel === 'balanced' |
| 54 | + ? 'warning' |
| 55 | + : 'success' |
| 56 | + } |
| 57 | + > |
| 58 | + {t(INDEX_RISK_LABEL_KEYS[riskLevel])} |
| 59 | + </Badge> |
| 60 | + {fallback && ( |
| 61 | + <Badge bg="secondary" text="light"> |
| 62 | + {t('offline_data')} |
| 63 | + </Badge> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + <small className="text-muted text-end"> |
| 68 | + {t('data_source')} <br /> |
| 69 | + {source.historyEndpoint} |
| 70 | + </small> |
| 71 | + </div> |
| 72 | + |
| 73 | + <p className="text-muted mb-0">{description}</p> |
| 74 | + |
| 75 | + <dl className={`${styles.metric} d-flex flex-wrap gap-4 align-items-center`}> |
| 76 | + <dt className="text-muted mb-1">{t('index_metric_latest_value')}</dt> |
| 77 | + <dd className={styles.value}>{formatNumber(latestValue, currentLanguage)}</dd> |
| 78 | + |
| 79 | + <dt className="text-muted mb-1">{t('index_metric_daily_change')}</dt> |
| 80 | + <dd className={valueTone(dailyChangePct)}>{formatPercent(dailyChangePct)}</dd> |
| 81 | + |
| 82 | + <dt className="text-muted mb-1">{t('index_metric_one_year_return')}</dt> |
| 83 | + <dd className={valueTone(oneYearReturnPct)}>{formatPercent(oneYearReturnPct)}</dd> |
| 84 | + |
| 85 | + <dt className="text-muted mb-1">{t('index_metric_max_drawdown')}</dt> |
| 86 | + <dd className={valueTone(maxDrawdownPct)}>{formatPercent(maxDrawdownPct)}</dd> |
| 87 | + </dl> |
| 88 | + |
| 89 | + <SparkLine points={sparkline} chartId={sparklineId} /> |
| 90 | + |
| 91 | + <ul className="list-unstyled m-0 d-flex flex-wrap gap-2"> |
| 92 | + {tags?.map(tag => ( |
| 93 | + <Badge key={tag} as="li" bg="light" text="dark" className={styles.tagBadge}> |
| 94 | + {tag} |
| 95 | + </Badge> |
| 96 | + ))} |
| 97 | + </ul> |
| 98 | + |
| 99 | + <time className="small text-muted" dateTime={updatedAtISO}> |
| 100 | + {t('updated_at')} {updatedAt || '--'} |
| 101 | + </time> |
| 102 | + |
| 103 | + <a href={`/finance/${symbol}`} className="text-primary fw-semibold"> |
| 104 | + {t('view_details')} |
| 105 | + </a> |
| 106 | + </Card.Body> |
| 107 | + </Card> |
| 108 | + ); |
| 109 | +}; |
0 commit comments