feat: 优化首页性能
This commit is contained in:
40
app/components/RefreshButton.jsx
Normal file
40
app/components/RefreshButton.jsx
Normal file
@@ -0,0 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { RefreshIcon } from './Icons';
|
||||
|
||||
export default function RefreshButton({ refreshCycleStartRef, refreshMs, manualRefresh, refreshing, fundsLength }) {
|
||||
|
||||
// 刷新周期进度 0~1,用于环形进度条
|
||||
const [refreshProgress, setRefreshProgress] = useState(0);
|
||||
|
||||
// 刷新进度条:每 100ms 更新一次进度
|
||||
useEffect(() => {
|
||||
if (fundsLength === 0 || refreshMs <= 0) return;
|
||||
const t = setInterval(() => {
|
||||
const elapsed = Date.now() - refreshCycleStartRef.current;
|
||||
const p = Math.min(1, elapsed / refreshMs);
|
||||
setRefreshProgress(p);
|
||||
}, 100);
|
||||
return () => clearInterval(t);
|
||||
}, [fundsLength, refreshMs]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="refresh-btn-wrap"
|
||||
style={{ '--progress': refreshProgress }}
|
||||
title={`刷新周期 ${Math.round(refreshMs / 1000)} 秒`}
|
||||
>
|
||||
<button
|
||||
className="icon-button"
|
||||
aria-label="立即刷新"
|
||||
onClick={manualRefresh}
|
||||
disabled={refreshing || fundsLength === 0}
|
||||
aria-busy={refreshing}
|
||||
title="立即刷新"
|
||||
>
|
||||
<RefreshIcon className={refreshing ? 'spin' : ''} width="18" height="18" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
37
app/page.jsx
37
app/page.jsx
@@ -36,6 +36,7 @@ import TradeModal from "./components/TradeModal";
|
||||
import TransactionHistoryModal from "./components/TransactionHistoryModal";
|
||||
import AddHistoryModal from "./components/AddHistoryModal";
|
||||
import UpdatePromptModal from "./components/UpdatePromptModal";
|
||||
import RefreshButton from "./components/RefreshButton";
|
||||
import WeChatModal from "./components/WeChatModal";
|
||||
import DcaModal from "./components/DcaModal";
|
||||
import githubImg from "./assets/github.svg";
|
||||
@@ -311,8 +312,6 @@ export default function HomePage() {
|
||||
|
||||
// 全局刷新状态
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
// 刷新周期进度 0~1,用于环形进度条
|
||||
const [refreshProgress, setRefreshProgress] = useState(0);
|
||||
|
||||
// 收起/展开状态
|
||||
const [collapsedCodes, setCollapsedCodes] = useState(new Set());
|
||||
@@ -2134,17 +2133,6 @@ export default function HomePage() {
|
||||
};
|
||||
}, [funds, refreshMs]);
|
||||
|
||||
// 刷新进度条:每 100ms 更新一次进度
|
||||
useEffect(() => {
|
||||
if (funds.length === 0 || refreshMs <= 0) return;
|
||||
const t = setInterval(() => {
|
||||
const elapsed = Date.now() - refreshCycleStartRef.current;
|
||||
const p = Math.min(1, elapsed / refreshMs);
|
||||
setRefreshProgress(p);
|
||||
}, 100);
|
||||
return () => clearInterval(t);
|
||||
}, [funds.length, refreshMs]);
|
||||
|
||||
const performSearch = async (val) => {
|
||||
if (!val.trim()) {
|
||||
setSearchResults([]);
|
||||
@@ -3447,22 +3435,13 @@ export default function HomePage() {
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
<div
|
||||
className="refresh-btn-wrap"
|
||||
style={{ '--progress': refreshProgress }}
|
||||
title={`刷新周期 ${Math.round(refreshMs / 1000)} 秒`}
|
||||
>
|
||||
<button
|
||||
className="icon-button"
|
||||
aria-label="立即刷新"
|
||||
onClick={manualRefresh}
|
||||
disabled={refreshing || funds.length === 0}
|
||||
aria-busy={refreshing}
|
||||
title="立即刷新"
|
||||
>
|
||||
<RefreshIcon className={refreshing ? 'spin' : ''} width="18" height="18" />
|
||||
</button>
|
||||
</div>
|
||||
<RefreshButton
|
||||
refreshMs={refreshMs}
|
||||
manualRefresh={manualRefresh}
|
||||
refreshing={refreshing}
|
||||
fundsLength={funds.length}
|
||||
refreshCycleStartRef={refreshCycleStartRef}
|
||||
/>
|
||||
{/*<button*/}
|
||||
{/* className="icon-button"*/}
|
||||
{/* aria-label="打开设置"*/}
|
||||
|
||||
Reference in New Issue
Block a user