FullTextSearchResult.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { List, Space, Tag, Typography } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { Link } from "react-router-dom";
  4. import { get } from "../../request";
  5. import TocPath, { ITocPathNode } from "../corpus/TocPath";
  6. import Marked from "../general/Marked";
  7. import "./search.css";
  8. const { Title, Text } = Typography;
  9. interface IFtsData {
  10. rank?: number;
  11. highlight?: string;
  12. book: number;
  13. paragraph: number;
  14. content?: string;
  15. title?: string;
  16. paliTitle?: string;
  17. path?: ITocPathNode[];
  18. }
  19. interface IFtsResponse {
  20. ok: boolean;
  21. string: string;
  22. data: {
  23. rows: IFtsData[];
  24. count: number;
  25. };
  26. }
  27. interface IFtsItem {
  28. book: number;
  29. paragraph: number;
  30. title?: string;
  31. paliTitle?: string;
  32. content?: string;
  33. path?: ITocPathNode[];
  34. }
  35. interface IWidget {
  36. keyWord?: string;
  37. tags?: string[];
  38. bookId?: string | null;
  39. book?: number;
  40. para?: number;
  41. orderBy?: string | null;
  42. match?: string | null;
  43. keyWord2?: string;
  44. view?: string;
  45. pageType?: string;
  46. }
  47. const FullTxtSearchResultWidget = ({
  48. keyWord,
  49. tags,
  50. bookId,
  51. book,
  52. para,
  53. orderBy,
  54. match,
  55. keyWord2,
  56. view = "pali",
  57. pageType,
  58. }: IWidget) => {
  59. const [ftsData, setFtsData] = useState<IFtsItem[]>();
  60. const [total, setTotal] = useState<number>();
  61. const [currPage, setCurrPage] = useState<number>(1);
  62. useEffect(() => {
  63. let url = `/v2/search?view=${view}&key=${keyWord}`;
  64. if (typeof tags !== "undefined") {
  65. url += `&tags=${tags}`;
  66. }
  67. if (bookId) {
  68. url += `&book=${bookId}`;
  69. }
  70. if (orderBy) {
  71. url += `&orderby=${orderBy}`;
  72. }
  73. if (match) {
  74. url += `&match=${match}`;
  75. }
  76. if (pageType) {
  77. url += `&type=${pageType}`;
  78. }
  79. const offset = (currPage - 1) * 10;
  80. url += `&limit=10&offset=${offset}`;
  81. console.log("fetch url", url);
  82. get<IFtsResponse>(url).then((json) => {
  83. if (json.ok) {
  84. const result: IFtsItem[] = json.data.rows.map((item) => {
  85. return {
  86. book: item.book,
  87. paragraph: item.paragraph,
  88. title: item.title ? item.title : item.paliTitle,
  89. paliTitle: item.paliTitle,
  90. content: item.highlight
  91. ? item.highlight.replaceAll("** ti ", "**ti ")
  92. : item.content,
  93. path: item.path,
  94. };
  95. });
  96. setFtsData(result);
  97. setTotal(json.data.count);
  98. }
  99. });
  100. }, [bookId, currPage, keyWord, match, orderBy, pageType, tags, view]);
  101. return (
  102. <List
  103. itemLayout="vertical"
  104. size="small"
  105. dataSource={ftsData}
  106. pagination={{
  107. onChange: (page) => {
  108. console.log(page);
  109. setCurrPage(page);
  110. },
  111. showQuickJumper: true,
  112. showSizeChanger: false,
  113. pageSize: 10,
  114. total: total,
  115. position: "both",
  116. showTotal: (total) => {
  117. return `结果: ${total}`;
  118. },
  119. }}
  120. renderItem={(item) => (
  121. <List.Item>
  122. <Title level={5}>
  123. <Link to={`/article/para?book=${item.book}&par=${item.paragraph}`}>
  124. {item.title}
  125. </Link>
  126. </Title>
  127. <div>
  128. <Text type="secondary">{item.paliTitle}</Text>
  129. </div>
  130. <Space>
  131. <TocPath data={item.path} />
  132. {"/"}
  133. <Tag>{item.paragraph}</Tag>
  134. </Space>
  135. <div className="search_content">
  136. <Marked text={item.content} />
  137. </div>
  138. </List.Item>
  139. )}
  140. />
  141. );
  142. };
  143. export default FullTxtSearchResultWidget;