useSentSim.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { message } from "antd";
  2. import { useEffect, useRef, useState } from "react";
  3. import {
  4. fetchSentSim as defaultFetcher,
  5. type ISimSent,
  6. type ISentenceSimListResponse,
  7. type ISentSimParams,
  8. } from "../api/sent-sim";
  9. type Fetcher = (params: ISentSimParams) => Promise<ISentenceSimListResponse>;
  10. interface IUseSentSimOptions {
  11. book: number;
  12. para: number;
  13. wordStart: number;
  14. wordEnd: number;
  15. limit?: number;
  16. channelsId?: string[];
  17. /** 可替换为 mock 函数,默认使用真实 fetchSentSim */
  18. fetcher?: Fetcher;
  19. }
  20. interface IUseSentSimResult {
  21. sentData: ISimSent[];
  22. remain: number;
  23. initLoading: boolean;
  24. loading: boolean;
  25. toggleSim: (checked: boolean) => void;
  26. loadMore: () => void;
  27. reload: () => void;
  28. }
  29. export function useSentSim({
  30. book,
  31. para,
  32. wordStart,
  33. wordEnd,
  34. limit = 5,
  35. channelsId,
  36. fetcher = defaultFetcher,
  37. }: IUseSentSimOptions): IUseSentSimResult {
  38. const [sim, setSim] = useState(0);
  39. const [offset, setOffset] = useState(0);
  40. const [reloadKey, setReloadKey] = useState(0);
  41. const [sentData, setSentData] = useState<ISimSent[]>([]);
  42. const [remain, setRemain] = useState(0);
  43. const [initLoading, setInitLoading] = useState(true);
  44. const [loading, setLoading] = useState(false);
  45. const isFirstFetch = useRef(true);
  46. // 用 ref 持有 fetcher,避免函数引用变化触发 effect
  47. const fetcherRef = useRef<Fetcher>(fetcher);
  48. useEffect(() => {
  49. fetcherRef.current = fetcher;
  50. }, [fetcher]);
  51. useEffect(() => {
  52. let cancelled = false;
  53. setLoading(true);
  54. fetcherRef
  55. .current({
  56. book,
  57. para,
  58. wordStart,
  59. wordEnd,
  60. limit,
  61. offset,
  62. sim,
  63. channelsId,
  64. })
  65. .then((json) => {
  66. if (cancelled) return;
  67. if (json.ok) {
  68. setSentData((prev) => {
  69. const next =
  70. offset === 0 ? [...json.data.rows] : [...prev, ...json.data.rows];
  71. setRemain(json.data.count - next.length);
  72. return next;
  73. });
  74. } else {
  75. message.error(json.message);
  76. }
  77. })
  78. .finally(() => {
  79. if (cancelled) return;
  80. setLoading(false);
  81. if (isFirstFetch.current) {
  82. setInitLoading(false);
  83. isFirstFetch.current = false;
  84. }
  85. });
  86. return () => {
  87. cancelled = true;
  88. };
  89. // eslint-disable-next-line react-hooks/exhaustive-deps
  90. }, [
  91. book,
  92. para,
  93. wordStart,
  94. wordEnd,
  95. limit,
  96. sim,
  97. offset,
  98. reloadKey,
  99. JSON.stringify(channelsId),
  100. ]);
  101. const toggleSim = (checked: boolean) => {
  102. setSim(checked ? 1 : 0);
  103. setOffset(0);
  104. setSentData([]);
  105. isFirstFetch.current = true;
  106. setInitLoading(true);
  107. };
  108. const loadMore = () => {
  109. setOffset((prev) => prev + limit);
  110. };
  111. const reload = () => {
  112. setSentData([]);
  113. setOffset(0);
  114. isFirstFetch.current = true;
  115. setInitLoading(true);
  116. setReloadKey((prev) => prev + 1);
  117. };
  118. return {
  119. sentData,
  120. remain,
  121. initLoading,
  122. loading,
  123. toggleSim,
  124. loadMore,
  125. reload,
  126. };
  127. }