pages_xunlian.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { useState } from "react";
  2. import dayjs from "dayjs";
  3. import { useQuery } from "@tanstack/react-query";
  4. import type { XunlianCode } from "../share/types_stock.ts";
  5. import { useNavigate } from "react-router";
  6. import { XunlianCodeAPI } from "./api/xunlian_codes.ts";
  7. export function XunlianPage() {
  8. const [visibleStocks, setVisibleStocks] = useState<Record<number, boolean>>({});
  9. const navigate = useNavigate();
  10. const { data: codes = [], isLoading } = useQuery({
  11. queryKey: ["xunlian-codes"],
  12. queryFn: async (): Promise<XunlianCode[]> => {
  13. const response = await XunlianCodeAPI.getXunlianCodes({});
  14. return response.data;
  15. },
  16. });
  17. const toggleStockVisibility = (id: number, e: React.MouseEvent) => {
  18. e.stopPropagation();
  19. setVisibleStocks((prev: Record<number, boolean>) => ({
  20. ...prev,
  21. [id]: !prev[id]
  22. }));
  23. };
  24. const handleCardClick = (code: XunlianCode) => {
  25. navigate(`/mobile/stock?code=${code.code}`);
  26. };
  27. return (
  28. <div className="p-4">
  29. <div className="flex justify-between items-center mb-4">
  30. <h1 className="text-2xl font-bold">训练案例</h1>
  31. </div>
  32. <div className="grid gap-4">
  33. {isLoading ? (
  34. <div className="text-center text-gray-500 py-8">
  35. <div className="animate-spin inline-block w-6 h-6 border-[3px] border-current border-t-transparent text-blue-600 rounded-full" role="status" aria-label="loading">
  36. <span className="sr-only">加载中...</span>
  37. </div>
  38. <div className="mt-2">加载中...</div>
  39. </div>
  40. ) : codes.length === 0 ? (
  41. <div className="text-center text-gray-500 py-8">
  42. 暂无训练案例
  43. </div>
  44. ) : (
  45. codes.map((code: XunlianCode) => (
  46. <div
  47. key={code.id}
  48. className="border rounded-lg p-4 hover:shadow-lg transition-shadow cursor-pointer"
  49. onClick={() => handleCardClick(code)}
  50. >
  51. <div className="flex justify-between items-start">
  52. <div>
  53. <h2 className="text-lg font-semibold flex items-center gap-2">
  54. {visibleStocks[code.id] ? (
  55. <>
  56. {code.stock_name} {code.name} ({code.code})
  57. <button
  58. onClick={(e) => toggleStockVisibility(code.id, e)}
  59. className="text-gray-500 hover:text-gray-700"
  60. title="隐藏股票信息"
  61. >
  62. <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-5 h-5">
  63. <path strokeLinecap="round" strokeLinejoin="round" d="M2.036 12.322a1.012 1.012 0 010-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178z" />
  64. <path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
  65. </svg>
  66. </button>
  67. </>
  68. ) : (
  69. <>
  70. {code.name}
  71. <button
  72. onClick={(e) => toggleStockVisibility(code.id, e)}
  73. className="text-gray-400 hover:text-gray-600"
  74. title="显示股票信息"
  75. >
  76. <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-5 h-5">
  77. <path strokeLinecap="round" strokeLinejoin="round" d="M3.98 8.223A10.477 10.477 0 001.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.45 10.45 0 0112 4.5c4.756 0 8.773 3.162 10.065 7.498a10.523 10.523 0 01-4.293 5.774M6.228 6.228L3 3m3.228 3.228l3.65 3.65m7.894 7.894L21 21m-3.228-3.228l-3.65-3.65m0 0a3 3 0 10-4.243-4.243m4.242 4.242L9.88 9.88" />
  78. </svg>
  79. </button>
  80. </>
  81. )}
  82. </h2>
  83. {code.description && (
  84. <p className="text-gray-600 mt-1">{code.description}</p>
  85. )}
  86. </div>
  87. <div className="text-sm text-gray-500">
  88. <div>{dayjs(code.trade_date).format("YYYY-MM-DD")}</div>
  89. </div>
  90. </div>
  91. {code.type && (
  92. <div className="mt-2">
  93. <span className="inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">
  94. {code.type}
  95. </span>
  96. </div>
  97. )}
  98. </div>
  99. ))
  100. )}
  101. </div>
  102. </div>
  103. );
  104. }