Skip to content

Commit e321db1

Browse files
committed
feat: 更新余额显示逻辑,优化高度计算
1 parent 53dbc97 commit e321db1

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

src/components/features/BalanceDisplay/KeyBalanceDisplay.jsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
import React, { useState, useEffect } from 'react';
22
import { getApiBalance } from '../../../services/api/base';
33
import { useLanguage } from '../../../hooks/useLanguage';
4+
import { useAppState } from '../../../contexts/AppStateContext';
45

56
const KeyBalanceDisplay = ({ apiKey, apiType, proxyUrl }) => {
67
const { t } = useLanguage();
7-
const [balanceInfo, setBalanceInfo] = useState(null);
8+
const { state, dispatch } = useAppState();
89
const [loading, setLoading] = useState(false);
9-
const [error, setError] = useState(null);
10+
11+
// 从keyResults中获取当前key的余额信息
12+
const keyResult = state.keyResults?.find(result => result.key === apiKey);
13+
const balanceInfo = keyResult?.balanceInfo;
14+
const balanceError = keyResult?.balanceError;
1015

1116
const fetchBalance = async () => {
1217
if (!apiKey || !apiType) return;
1318

19+
// 如果已经有余额信息,不重复查询
20+
if (balanceInfo || balanceError) return;
21+
1422
setLoading(true);
15-
setError(null);
1623

1724
try {
1825
const result = await getApiBalance(apiKey, apiType, proxyUrl);
19-
if (result.success) {
20-
setBalanceInfo(result);
21-
setError(null);
22-
} else {
23-
setError(result.error);
24-
setBalanceInfo(null);
25-
}
26+
27+
// 更新keyResults中的余额信息
28+
dispatch({
29+
type: 'UPDATE_KEY_STATUS',
30+
payload: {
31+
key: apiKey,
32+
balanceInfo: result.success ? result : null,
33+
balanceError: result.success ? null : result.error
34+
}
35+
});
2636
} catch (err) {
27-
setError(err.message);
28-
setBalanceInfo(null);
37+
// 更新错误信息
38+
dispatch({
39+
type: 'UPDATE_KEY_STATUS',
40+
payload: {
41+
key: apiKey,
42+
balanceError: err.message
43+
}
44+
});
2945
} finally {
3046
setLoading(false);
3147
}
@@ -49,7 +65,7 @@ const KeyBalanceDisplay = ({ apiKey, apiType, proxyUrl }) => {
4965
return <div className="key-model">{t('balance.title')}: {t('balance.refreshing')}</div>;
5066
}
5167

52-
if (error) {
68+
if (balanceError) {
5369
return <div className="key-model">{t('balance.title')}: {t('balance.fetchFailed')}</div>;
5470
}
5571

src/components/features/Results/VirtualizedList.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ const VirtualizedList = () => {
165165

166166
// 创建一个函数来获取每个项目的高度
167167
const getItemSize = (index) => {
168-
return getItemHeight(filteredKeys[index]);
168+
return getItemHeight(filteredKeys[index], state.apiType);
169169
};
170170

171171
// 当数据变化时重置虚拟化缓存

src/hooks/useVirtualization.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback } from 'react';
22

33
export const useVirtualization = () => {
4-
const getItemHeight = useCallback((keyData) => {
4+
const getItemHeight = useCallback((keyData, apiType) => {
55
if (!keyData) return 60; // 默认高度
66

77
// 基础高度:padding(12*2=24) + wrapper padding(2*2=4) + 基础内容(40) = 68px
@@ -43,6 +43,11 @@ export const useVirtualization = () => {
4343
baseHeight += lineHeight;
4444
}
4545

46+
// 如果是硅基流动且是有效密钥,为余额显示增加额外高度
47+
if (apiType === 'siliconcloud' && (keyData.status === 'valid' || keyData.status === 'paid')) {
48+
baseHeight += lineHeight; // 为余额显示增加一行高度
49+
}
50+
4651
return Math.max(baseHeight, 68); // 设置最小高度为68px
4752
}, []);
4853

src/styles/globals.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,8 @@ body {
748748
align-items: flex-start;
749749
gap: var(--spacing-sm);
750750
padding: 10px 12px;
751-
margin-bottom: 3px;
752-
min-height: 70px;
751+
margin-bottom: 6px;
752+
min-height: 80px;
753753
}
754754

755755
.key-content {
@@ -851,8 +851,8 @@ body {
851851

852852
.key-item {
853853
padding: 8px 10px;
854-
margin-bottom: 2px;
855-
min-height: 65px;
854+
margin-bottom: 5px;
855+
min-height: 75px;
856856
}
857857

858858
.key-text {
@@ -969,6 +969,7 @@ body {
969969
border-color: #444444;
970970
color: #ffffff;
971971
}
972+
972973
/* Results tooltip */
973974
.results-tooltip {
974975
display: flex;
@@ -1040,5 +1041,4 @@ body {
10401041
max-height: 80vh;
10411042
margin: var(--spacing-sm) auto;
10421043
}
1043-
}
1044-
1044+
}

0 commit comments

Comments
 (0)