FtsBookList.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Badge, List } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { get } from "../../request";
  4. interface IFtsData {
  5. book: number;
  6. paragraph: number;
  7. title?: string;
  8. paliTitle: string;
  9. pcdBookId: number;
  10. count: number;
  11. }
  12. interface IFtsResponse {
  13. ok: boolean;
  14. string: string;
  15. data: {
  16. rows: IFtsData[];
  17. count: number;
  18. };
  19. }
  20. interface IFtsItem {
  21. book: number;
  22. paragraph: number;
  23. title?: string;
  24. paliTitle?: string;
  25. pcdBookId: number;
  26. count: number;
  27. }
  28. interface IWidget {
  29. keyWord?: string;
  30. keyWords?: string[];
  31. engin?: "wbw" | "tulip";
  32. tags?: string[];
  33. bookId?: string | null;
  34. book?: number;
  35. para?: number;
  36. match?: string | null;
  37. keyWord2?: string;
  38. view?: string;
  39. onSelect?: Function;
  40. }
  41. const FtsBookListWidget = ({
  42. keyWord,
  43. keyWords,
  44. engin = "wbw",
  45. tags,
  46. bookId,
  47. book,
  48. para,
  49. keyWord2,
  50. match,
  51. view = "pali",
  52. onSelect,
  53. }: IWidget) => {
  54. const [ftsData, setFtsData] = useState<IFtsItem[]>();
  55. const [total, setTotal] = useState<number>();
  56. const focusBooks = bookId?.split(",");
  57. console.log("focusBooks", focusBooks);
  58. useEffect(() => {
  59. let words;
  60. let api = "";
  61. switch (engin) {
  62. case "wbw":
  63. api = "search-pali-wbw-books";
  64. words = keyWords?.join();
  65. break;
  66. case "tulip":
  67. api = "search-book-list";
  68. words = keyWord;
  69. break;
  70. default:
  71. break;
  72. }
  73. let url = `/v2/${api}?view=${view}&key=${words}`;
  74. if (typeof tags !== "undefined") {
  75. url += `&tags=${tags}`;
  76. }
  77. if (match) {
  78. url += `&match=${match}`;
  79. }
  80. console.log("url", url);
  81. get<IFtsResponse>(url).then((json) => {
  82. if (json.ok) {
  83. console.log("data", json.data.rows);
  84. let totalResult = 0;
  85. for (const iterator of json.data.rows) {
  86. totalResult += iterator.count;
  87. }
  88. const result: IFtsItem[] = json.data.rows.map((item) => {
  89. return item;
  90. });
  91. setFtsData([
  92. {
  93. book: 0,
  94. paragraph: 0,
  95. title: "全部",
  96. pcdBookId: 0,
  97. count: totalResult,
  98. },
  99. ...result,
  100. ]);
  101. setTotal(json.data.count);
  102. }
  103. });
  104. }, [keyWord, match, tags]);
  105. return (
  106. <List
  107. header={`总计:` + total}
  108. itemLayout="vertical"
  109. size="small"
  110. dataSource={ftsData}
  111. renderItem={(item, id) => (
  112. <List.Item>
  113. <div
  114. style={{
  115. padding: 4,
  116. borderRadius: 4,
  117. display: "flex",
  118. justifyContent: "space-between",
  119. cursor: "pointer",
  120. backgroundColor: focusBooks?.includes(item.pcdBookId.toString())
  121. ? "lightblue"
  122. : "unset",
  123. }}
  124. onClick={() => {
  125. if (typeof onSelect !== "undefined") {
  126. onSelect(item.pcdBookId);
  127. }
  128. }}
  129. >
  130. <span>
  131. {id + 1}.{item.title ? item.title : item.paliTitle}
  132. </span>
  133. <Badge color="geekblue" count={item.count} />
  134. </div>
  135. </List.Item>
  136. )}
  137. />
  138. );
  139. };
  140. export default FtsBookListWidget;