generated from idea2app/Next-Bootstrap-ts
-
Notifications
You must be signed in to change notification settings - Fork 6
[add] Wealth Product selector page #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4162281
feat: Add finance MVP with AkShare data service and landing page
dethan3 f8ca608
[fix] PNPM lock dismatch
TechQuery 3a1f964
feat(finance): internationalize finance page and fund cards
dethan3 4d89463
[refactor] split Spark Line component into an independent module
TechQuery 712096f
[optimize] generate Next.js types automatically
TechQuery c1ca5c7
chore: document /finance page in README and remove finance PRD
dethan3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ package-lock.json | |
| /coverage | ||
|
|
||
| # next.js | ||
| next-env.d.ts | ||
| /.next/ | ||
| /out/ | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| .fundCard { | ||
| transition: | ||
| transform 0.2s ease, | ||
| box-shadow 0.2s ease; | ||
| box-shadow: 0 16px 40px rgba(15, 23, 42, 0.08); | ||
| border: 1px solid rgba(15, 23, 42, 0.08); | ||
| border-radius: 20px; | ||
| &:hover { | ||
| transform: translateY(-4px); | ||
| box-shadow: 0 24px 60px rgba(15, 23, 42, 0.12); | ||
| } | ||
|
|
||
| .header small { | ||
| line-height: 1.3; | ||
| } | ||
| } | ||
|
|
||
| .metric { | ||
| gap: 1.5rem; | ||
| .value { | ||
| font-weight: 700; | ||
| font-size: 2rem; | ||
| } | ||
| } | ||
| .tagBadge { | ||
| border: 1px solid rgba(15, 23, 42, 0.15); | ||
| border-radius: 999px; | ||
| font-weight: 500; | ||
| } | ||
|
|
||
| .mutedText { | ||
| color: rgba(15, 23, 42, 0.6); | ||
| } | ||
|
|
||
| .positiveText { | ||
| color: #198754; | ||
| } | ||
|
|
||
| .negativeText { | ||
| color: #dc3545; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { FC, useContext, useMemo } from 'react'; | ||
| import { Badge, Card } from 'react-bootstrap'; | ||
|
|
||
| import { INDEX_CATEGORY_LABEL_KEYS, INDEX_RISK_LABEL_KEYS } from '../../constants/finance'; | ||
| import { I18nContext } from '../../models/Translation'; | ||
| import { IndexFundSnapshot } from '../../types/finance'; | ||
| import styles from './Finance.module.less'; | ||
| import { SparkLine } from './SparkLine'; | ||
|
|
||
| const formatNumber = (value?: number | null, locale = 'zh-CN') => | ||
| value != null ? value.toLocaleString(locale, { maximumFractionDigits: 2 }) : '--'; | ||
|
|
||
| const formatPercent = (value?: number | null) => | ||
| value != null ? `${(value * 100).toFixed(2)}%` : '--'; | ||
|
|
||
| const valueTone = (value?: number | null) => | ||
| value != null ? (value >= 0 ? styles.positiveText : styles.negativeText) : styles.mutedText; | ||
|
|
||
| export const FundCard: FC<IndexFundSnapshot> = ({ | ||
| symbol, | ||
| displayName, | ||
| category, | ||
| riskLevel, | ||
| latestValue, | ||
| dailyChangePct, | ||
| oneYearReturnPct, | ||
| maxDrawdownPct, | ||
| tags, | ||
| sparkline, | ||
| updatedAt, | ||
| fallback, | ||
| description, | ||
| source, | ||
| }) => { | ||
| const { currentLanguage, t } = useContext(I18nContext); | ||
| const sparklineId = useMemo(() => `fund-${symbol}`, [symbol]); | ||
| const updatedAtISO = updatedAt ? new Date(updatedAt).toJSON() : undefined; | ||
|
|
||
| return ( | ||
| <Card className={`${styles.fundCard} h-100`}> | ||
| <Card.Body className="d-flex flex-column gap-3"> | ||
| <div className={`${styles.header} 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"> | ||
| {t(INDEX_CATEGORY_LABEL_KEYS[category])} | ||
| </Badge> | ||
| <Badge | ||
| bg={ | ||
| riskLevel === 'aggressive' | ||
| ? 'danger' | ||
| : riskLevel === 'balanced' | ||
| ? 'warning' | ||
| : 'success' | ||
| } | ||
| > | ||
| {t(INDEX_RISK_LABEL_KEYS[riskLevel])} | ||
| </Badge> | ||
| {fallback && ( | ||
| <Badge bg="secondary" text="light"> | ||
| {t('offline_data')} | ||
| </Badge> | ||
| )} | ||
| </div> | ||
| </div> | ||
| <small className="text-muted text-end"> | ||
| {t('data_source')} <br /> | ||
| {source.historyEndpoint} | ||
| </small> | ||
| </div> | ||
|
|
||
| <p className="text-muted mb-0">{description}</p> | ||
|
|
||
| <dl className={`${styles.metric} d-flex flex-wrap gap-4 align-items-center`}> | ||
| <dt className="text-muted mb-1">{t('index_metric_latest_value')}</dt> | ||
| <dd className={styles.value}>{formatNumber(latestValue, currentLanguage)}</dd> | ||
|
|
||
| <dt className="text-muted mb-1">{t('index_metric_daily_change')}</dt> | ||
| <dd className={valueTone(dailyChangePct)}>{formatPercent(dailyChangePct)}</dd> | ||
|
|
||
| <dt className="text-muted mb-1">{t('index_metric_one_year_return')}</dt> | ||
| <dd className={valueTone(oneYearReturnPct)}>{formatPercent(oneYearReturnPct)}</dd> | ||
|
|
||
| <dt className="text-muted mb-1">{t('index_metric_max_drawdown')}</dt> | ||
| <dd className={valueTone(maxDrawdownPct)}>{formatPercent(maxDrawdownPct)}</dd> | ||
| </dl> | ||
|
|
||
| <SparkLine points={sparkline} chartId={sparklineId} /> | ||
|
|
||
| <ul className="list-unstyled m-0 d-flex flex-wrap gap-2"> | ||
| {tags?.map(tag => ( | ||
| <Badge key={tag} as="li" bg="light" text="dark" className={styles.tagBadge}> | ||
| {tag} | ||
| </Badge> | ||
| ))} | ||
| </ul> | ||
|
Comment on lines
+91
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 标签(tags)未国际化处理。 当前 tags 数组中的字符串(如 '宽基'、'蓝筹')直接渲染,在非中文环境下会显示中文文本,违反国际化规范。 建议方案: 方案 1(推荐):在 constants/finance.ts 中定义标签映射,类似 INDEX_CATEGORY_LABEL_KEYS: // constants/finance.ts
export const INDEX_TAG_LABEL_KEYS: Record<string, string> = {
'宽基': 'fund_tag_broad_base',
'蓝筹': 'fund_tag_blue_chip',
'成长': 'fund_tag_growth',
// ... 其他标签
};然后在组件中映射: <ul className="list-unstyled m-0 d-flex flex-wrap gap-2">
{tags?.map(tag => (
<Badge key={tag} as="li" bg="light" text="dark" className={styles.tagBadge}>
- {tag}
+ {t(INDEX_TAG_LABEL_KEYS[tag] || tag)}
</Badge>
))}
</ul>方案 2:API 返回标签的 key 而非中文字符串,组件通过 key 查找翻译。 基于 learnings:所有用户可见文本必须使用 t() 函数。 🤖 Prompt for AI Agents |
||
|
|
||
| <time className="small text-muted" dateTime={updatedAtISO}> | ||
| {t('updated_at')} {updatedAt || '--'} | ||
| </time> | ||
|
|
||
| <a href={`/finance/${symbol}`} className="text-primary fw-semibold"> | ||
| {t('view_details')} | ||
| </a> | ||
| </Card.Body> | ||
| </Card> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .sparkline { | ||
| width: 100%; | ||
| height: 80px; | ||
|
|
||
| svg { | ||
| width: 100%; | ||
| height: 100%; | ||
| } | ||
|
|
||
| .placeholder { | ||
| border: 1px dashed rgba(15, 23, 42, 0.2); | ||
| border-radius: 12px; | ||
| padding: 1rem; | ||
| color: rgba(15, 23, 42, 0.6); | ||
| text-align: center; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { computed } from 'mobx'; | ||
| import { observer } from 'mobx-react'; | ||
| import { ObservedComponent } from 'mobx-react-helper'; | ||
|
|
||
| import { i18n, I18nContext } from '../../models/Translation'; | ||
| import { IndexHistoryPoint } from '../../types/finance'; | ||
| import styles from './SparkLine.module.less'; | ||
|
|
||
| export interface SparkLineProps { | ||
| points: IndexHistoryPoint[]; | ||
| chartId: string; | ||
| } | ||
|
|
||
| @observer | ||
| export class SparkLine extends ObservedComponent<SparkLineProps, typeof i18n> { | ||
| static contextType = I18nContext; | ||
|
|
||
| @computed | ||
| get chartData() { | ||
| const { points } = this.observedProps; | ||
|
|
||
| if (!points.length) return { polyline: '', gradientStops: [] as number[] }; | ||
|
|
||
| const values = points.map(({ value }) => value); | ||
| const min = Math.min(...values); | ||
| const max = Math.max(...values); | ||
| const delta = max - min || 1; | ||
|
|
||
| const polylinePoints = points | ||
| .map(({ value }, index, { length }) => { | ||
| const x = (index / Math.max(length - 1, 1)) * 100; | ||
| const y = ((max - value) / delta) * 40; | ||
|
|
||
| return `${x.toFixed(2)},${y.toFixed(2)}`; | ||
| }) | ||
| .join(' '); | ||
|
|
||
| const gradientOffsets = [0, 50, 100]; | ||
|
|
||
| return { polyline: polylinePoints, gradientStops: gradientOffsets }; | ||
| } | ||
|
|
||
| renderContent() { | ||
| const { t } = this.observedContext, | ||
| { chartId } = this.observedProps, | ||
| { polyline, gradientStops } = this.chartData; | ||
| const gradientId = `${chartId}-gradient`; | ||
|
|
||
| return ( | ||
| <div className={styles.sparkline}> | ||
| <svg viewBox="0 0 100 40" role="img" aria-label={t('index_sparkline_60d_label')}> | ||
| <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> | ||
| ); | ||
| } | ||
|
|
||
| render() { | ||
| const { t } = this.observedContext, | ||
| { points } = this.props; | ||
|
|
||
| return points.length ? ( | ||
| this.renderContent() | ||
| ) : ( | ||
| <div className={styles.sparkline}> | ||
| <div className={styles.placeholder}>{t('data_preparing')}</div> | ||
| </div> | ||
| ); | ||
| } | ||
| } |
dethan3 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { IndexFundCategory, IndexRiskLevel } from '../types/finance'; | ||
| import { I18nKey } from '../models/Translation'; | ||
|
|
||
| export const INDEX_CATEGORY_LABEL_KEYS: Record<IndexFundCategory, I18nKey> = { | ||
| broad: 'index_category_broad', | ||
| sector: 'index_category_sector', | ||
| theme: 'index_category_theme', | ||
| }; | ||
|
|
||
| export const INDEX_RISK_LABEL_KEYS: Record<IndexRiskLevel, I18nKey> = { | ||
| conservative: 'index_risk_conservative', | ||
| balanced: 'index_risk_balanced', | ||
| aggressive: 'index_risk_aggressive', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { buildURLData } from 'web-utility'; | ||
|
|
||
| const DEFAULT_HOSTS = [ | ||
| process.env.AKSHARE_API_BASE, | ||
| 'https://akshare.xyz/api/public', | ||
| 'https://akshare.akfamily.xyz/api/public', | ||
| ].filter((value): value is string => !!value); | ||
|
|
||
| export async function requestAkShareJSON<T>( | ||
| endpoint: string, | ||
| params?: Record<string, string | number | undefined>, | ||
| ): Promise<T> { | ||
| let lastError: Error | null = null; | ||
|
|
||
| for (const host of DEFAULT_HOSTS) { | ||
| try { | ||
| const url = new URL(endpoint.replace(/^\//, ''), host.endsWith('/') ? host : `${host}/`); | ||
|
|
||
| if (params) url.search = buildURLData(params) + ''; | ||
|
|
||
| const response = await fetch(url.toString(), { | ||
| headers: { accept: 'application/json' }, | ||
| cache: 'no-store', | ||
| }); | ||
|
|
||
| if (!response.ok) | ||
| throw new Error( | ||
| `AkShare responded with ${response.status} ${response.statusText} for ${url}`, | ||
| ); | ||
|
|
||
| const contentType = response.headers.get('content-type'); | ||
|
|
||
| if (!contentType?.includes('application/json')) { | ||
| const preview = (await response.text()).slice(0, 200); | ||
|
|
||
| throw new Error(`Unexpected content-type ${contentType} for ${url}: ${preview}`); | ||
| } | ||
|
|
||
| return (await response.json()) as T; | ||
| } catch (error) { | ||
| lastError = error as Error; | ||
| console.warn(`[AkShare] Falling back after error on host ${host}:`, lastError.message); | ||
| } | ||
| } | ||
| throw lastError ?? new Error('AkShare request failed without specific error'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.