Kaynağa Gözat

♻️ refactor(stock-chart): 重构股票查询钩子函数

- 移除不必要的useEffect和toast依赖,分离数据获取与UI交互逻辑
- 为queryFn添加显式返回类型注解,提高类型安全性
- 优化股票数据转换逻辑,将API返回的数字类型统一转换为字符串类型
- 改进备忘录数据处理,明确处理CRUD接口返回的分页格式数据
- 移除冗余的类型断言,利用TypeScript自动类型推断
yourname 6 ay önce
ebeveyn
işleme
8e0e290bfd

+ 29 - 26
src/client/mobile/components/stock/components/stock-chart/src/hooks/useStockQueries.ts

@@ -2,8 +2,6 @@ import { useQuery } from '@tanstack/react-query';
 import { stockDataClient } from '@/client/api';
 import { stockDataClient } from '@/client/api';
 import { dateNotesClient } from '@/client/api';
 import { dateNotesClient } from '@/client/api';
 import type { StockData, DateMemo } from '../types/index';
 import type { StockData, DateMemo } from '../types/index';
-import { toast } from 'react-toastify';
-import { useEffect } from 'react';
 import type { InferResponseType } from 'hono/client';
 import type { InferResponseType } from 'hono/client';
 
 
 // 定义响应类型
 // 定义响应类型
@@ -17,9 +15,9 @@ export function useStockQueries(code?: string) {
     isLoading: isLoadingStock,
     isLoading: isLoadingStock,
     error: stockError,
     error: stockError,
     refetch: refetchStock
     refetch: refetchStock
-  } = useQuery<StockHistoryResponse>({
+  } = useQuery({
     queryKey: ['stockHistory', code],
     queryKey: ['stockHistory', code],
-    queryFn: async () => {
+    queryFn: async (): Promise<StockHistoryResponse> => {
       if (!code) throw new Error('股票代码不能为空');
       if (!code) throw new Error('股票代码不能为空');
       const response = await stockDataClient.history[':code'].$get({
       const response = await stockDataClient.history[':code'].$get({
         param: { code }
         param: { code }
@@ -39,42 +37,47 @@ export function useStockQueries(code?: string) {
     isLoading: isLoadingMemo,
     isLoading: isLoadingMemo,
     error: memoError,
     error: memoError,
     refetch: refetchMemo
     refetch: refetchMemo
-  } = useQuery<DateNotesListResponse>({
+  } = useQuery({
     queryKey: ['memoData', code],
     queryKey: ['memoData', code],
-    queryFn: () => dateNotesClient.$get({
-      query: {
-        filters: JSON.stringify(code ? { code } : {}),
-        page: 1,
-        pageSize: 1000
+    queryFn: async (): Promise<DateNotesListResponse> => {
+      const response = await dateNotesClient.$get({
+        query: {
+          filters: JSON.stringify(code ? { code } : {}),
+          page: 1,
+          pageSize: 1000
+        }
+      });
+      if (!response.ok) {
+        throw new Error('获取股票历史数据失败');
       }
       }
-    }).then(res => res.json()),
+      return response.json();
+    },
     enabled: false,
     enabled: false,
   });
   });
 
 
-  // 转换数据格式
-  const stockData = (stockDataResponse?.data || []) as StockData[];
+  // 转换数据格式 - API返回数字类型,转换为前端需要的字符串类型
+  const stockData: StockData[] = (stockDataResponse?.data || []).map((item) => ({
+    d: item.d,
+    o: item.o.toString(),
+    h: item.h.toString(),
+    l: item.l.toString(),
+    c: item.c.toString(),
+    v: item.v.toString(),
+    zd: item.zd.toString(),
+    // 其他字段根据需要转换
+  }));
 
 
-  const memoData = (memoDataResponse?.data?.map(item => ({
+  // 处理date-notes响应格式(通用CRUD返回 { data: [], pagination: {} })
+  const memoData: DateMemo[] = (memoDataResponse?.data || []).map(item => ({
     _id: item.id,
     _id: item.id,
     代码: item.code,
     代码: item.code,
     日期: item.noteDate,
     日期: item.noteDate,
     提示: item.note,
     提示: item.note,
-  })) || []) as DateMemo[];
+  }));
 
 
   const isLoading = isLoadingStock || isLoadingMemo;
   const isLoading = isLoadingStock || isLoadingMemo;
   const error = stockError || memoError;
   const error = stockError || memoError;
 
 
-  useEffect(() => {
-    if (isLoading) {
-      toast.loading('正在加载数据...', { toastId: 'stockLoading' })
-    } else {
-      toast.done('stockLoading')
-      if (error instanceof Error) {
-        toast.error('加载数据失败,请稍后重试')
-      }
-    }
-  }, [isLoading, error]);
-
   // 提供一个函数来手动触发查询
   // 提供一个函数来手动触发查询
   const fetchData = async () => {
   const fetchData = async () => {
     if (!code) return;
     if (!code) return;