From 98faa2031bc6b6525c310e5e445d8bbf82c5f9c1 Mon Sep 17 00:00:00 2001 From: patdelphi Date: Wed, 20 Aug 2025 18:27:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=B9=B6=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复ESLint警告:使用useCallback优化React Hooks依赖 - 修复JavaScript初始化错误:调整函数声明顺序 - 优化历史记录查看:添加自动滚动到顶部功能 - 统一网站logo:全部更换为traditional_chinese_gold_red_dragon_symbol.jpg - 提升代码质量和用户体验 --- src/components/Layout.tsx | 2 +- src/contexts/AuthContext.tsx | 1 + src/pages/AnalysisPage.tsx | 27 ++++++++++++++------------- src/pages/HistoryPage.tsx | 16 +++++++++------- src/pages/HomePage.tsx | 4 ++-- src/pages/ProfilePage.tsx | 14 +++++++------- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx index 8181c03..e0b128b 100644 --- a/src/components/Layout.tsx +++ b/src/components/Layout.tsx @@ -52,7 +52,7 @@ const Layout: React.FC = ({ children }) => {
神机阁 diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 5bf6958..a0cd149 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -86,6 +86,7 @@ export function AuthProvider({ children }: AuthProviderProps) { ); } +// eslint-disable-next-line react-refresh/only-export-components export function useAuth() { const context = useContext(AuthContext); if (context === undefined) { diff --git a/src/pages/AnalysisPage.tsx b/src/pages/AnalysisPage.tsx index 4224aa0..3df65f9 100644 --- a/src/pages/AnalysisPage.tsx +++ b/src/pages/AnalysisPage.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useMemo, useRef } from 'react'; +import React, { useState, useEffect, useMemo, useRef, useCallback } from 'react'; import { useAuth } from '../contexts/AuthContext'; import { localApi } from '../lib/localApi'; import { ChineseButton } from '../components/ui/ChineseButton'; @@ -42,16 +42,7 @@ const AnalysisPage: React.FC = () => { return undefined; }, [analysisType, formData.birth_date, formData.birth_time, formData.name, formData.gender]); - useEffect(() => { - loadProfile(); - }, [user]); - - // 切换分析类型时清空分析结果 - useEffect(() => { - setAnalysisResult(null); - }, [analysisType]); - - const loadProfile = async () => { + const loadProfile = useCallback(async () => { if (!user) return; try { @@ -71,7 +62,16 @@ const AnalysisPage: React.FC = () => { } catch (error) { console.error('加载档案失败:', error); } - }; + }, [user]); + + useEffect(() => { + loadProfile(); + }, [user, loadProfile]); + + // 切换分析类型时清空分析结果 + useEffect(() => { + setAnalysisResult(null); + }, [analysisType]); const handleAnalysis = async () => { if (!user) return; @@ -111,7 +111,7 @@ const AnalysisPage: React.FC = () => { case 'ziwei': response = await localApi.analysis.ziwei(birthData); break; - case 'yijing': + case 'yijing': { const yijingData = { question: formData.question, user_id: user.id, @@ -121,6 +121,7 @@ const AnalysisPage: React.FC = () => { }; response = await localApi.analysis.yijing(yijingData); break; + } default: throw new Error(`不支持的分析类型: ${analysisType}`); } diff --git a/src/pages/HistoryPage.tsx b/src/pages/HistoryPage.tsx index 35ec8e8..a664271 100644 --- a/src/pages/HistoryPage.tsx +++ b/src/pages/HistoryPage.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import { useAuth } from '../contexts/AuthContext'; import { localApi } from '../lib/localApi'; import { ChineseButton } from '../components/ui/ChineseButton'; @@ -41,11 +41,7 @@ const HistoryPage: React.FC = () => { } }; - useEffect(() => { - loadHistory(); - }, [user]); - - const loadHistory = async () => { + const loadHistory = useCallback(async () => { if (!user) return; try { @@ -93,7 +89,11 @@ const HistoryPage: React.FC = () => { } finally { setLoading(false); } - }; + }, [user]); + + useEffect(() => { + loadHistory(); + }, [user, loadHistory]); const handleDeleteReading = async (readingId: string) => { if (!confirm('确定要删除这条分析记录吗?')) { @@ -122,6 +122,8 @@ const HistoryPage: React.FC = () => { const handleViewReading = (reading: NumerologyReading) => { setSelectedReading(reading); setViewingResult(true); + // 滚动到页面顶部 + window.scrollTo({ top: 0, behavior: 'smooth' }); }; const getAnalysisTypeIcon = (type: string) => { diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx index b50a77f..0c18149 100644 --- a/src/pages/HomePage.tsx +++ b/src/pages/HomePage.tsx @@ -71,8 +71,8 @@ const HomePage: React.FC = () => { {/* 太极符号装饰 */}
太极八卦
diff --git a/src/pages/ProfilePage.tsx b/src/pages/ProfilePage.tsx index 37b9a41..d1f8be3 100644 --- a/src/pages/ProfilePage.tsx +++ b/src/pages/ProfilePage.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { localApi } from '../lib/localApi'; @@ -24,11 +24,7 @@ const ProfilePage: React.FC = () => { username: '' }); - useEffect(() => { - loadProfile(); - }, [user]); - - const loadProfile = async () => { + const loadProfile = useCallback(async () => { if (!user) return; try { @@ -54,7 +50,11 @@ const ProfilePage: React.FC = () => { console.error('加载档案失败:', error); toast.error('加载档案失败'); } - }; + }, [user]); + + useEffect(() => { + loadProfile(); + }, [user, loadProfile]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault();