FullTextSearchResult.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { _Button, List, Skeleton, Space, Tag, Typography } from "antd";
  2. import { Link } from "react-router";
  3. import { _get } from "../../request";
  4. import TocPath, {
  5. type ITocPathNode,
  6. } from "../../../src/components/tipitaka/TocPath";
  7. import type { TContentType } from "../discussion/DiscussionCreate";
  8. import Marked from "../general/Marked";
  9. import PaliText from "../template/Wbw/PaliText";
  10. import "./search.css";
  11. import AiTranslate from "../ai/AiTranslate";
  12. const { Title, Text } = Typography;
  13. export interface IFtsData {
  14. rank?: number;
  15. highlight?: string;
  16. book: number;
  17. paragraph: number;
  18. content?: string;
  19. content_type?: TContentType;
  20. title?: string;
  21. paliTitle?: string;
  22. path?: ITocPathNode[];
  23. }
  24. export interface IFtsResponse {
  25. ok: boolean;
  26. message: string;
  27. data: {
  28. rows: IFtsData[];
  29. count: number;
  30. };
  31. }
  32. export interface IFtsItem {
  33. book: number;
  34. paragraph: number;
  35. title?: string;
  36. paliTitle?: string;
  37. content?: string;
  38. path?: ITocPathNode[];
  39. rank?: number;
  40. }
  41. export type ISearchView = "pali" | "title" | "page" | "number";
  42. interface IWidget {
  43. view?: ISearchView;
  44. ftsData?: IFtsItem[];
  45. total?: number;
  46. loading: boolean;
  47. currPage: number;
  48. onChange?: (page: number, pageSize: number) => void;
  49. }
  50. const FullTxtSearchResultWidget = ({
  51. view,
  52. ftsData,
  53. total,
  54. loading = false,
  55. currPage = 1,
  56. onChange,
  57. }: IWidget) => {
  58. /*
  59. useEffect(
  60. () => setCurrPage(1),
  61. [view, keyWord, keyWords, tags, bookId, match, pageType, bold]
  62. );
  63. useEffect(() => {
  64. let words;
  65. let api = "";
  66. if (keyWord?.trim().includes(" ")) {
  67. api = "search";
  68. words = keyWord;
  69. } else {
  70. api = "search-pali-wbw";
  71. words = keyWords?.join();
  72. }
  73. let url = `/v2/${api}?view=${view}&key=${words}`;
  74. if (typeof tags !== "undefined") {
  75. url += `&tags=${tags}`;
  76. }
  77. if (bookId) {
  78. url += `&book=${bookId}`;
  79. }
  80. if (orderBy) {
  81. url += `&orderby=${orderBy}`;
  82. }
  83. if (match) {
  84. url += `&match=${match}`;
  85. }
  86. if (pageType) {
  87. url += `&type=${pageType}`;
  88. }
  89. if (bold) {
  90. url += `&bold=${bold}`;
  91. }
  92. const offset = (currPage - 1) * 10;
  93. url += `&limit=10&offset=${offset}`;
  94. console.log("fetch url", url);
  95. setLoading(true);
  96. get<IFtsResponse>(url)
  97. .then((json) => {
  98. if (json.ok) {
  99. console.log("data", json.data);
  100. const result: IFtsItem[] = json.data.rows.map((item) => {
  101. return {
  102. book: item.book,
  103. paragraph: item.paragraph,
  104. title: item.title ? item.title : item.paliTitle,
  105. paliTitle: item.paliTitle,
  106. content: item.highlight
  107. ? item.highlight.replaceAll("** ti ", "**ti ")
  108. : item.content,
  109. path: item.path,
  110. rank: item.rank,
  111. };
  112. });
  113. setFtsData(result);
  114. setTotal(json.data.count);
  115. onFound && onFound(result);
  116. } else {
  117. console.error(json.message);
  118. }
  119. })
  120. .finally(() => setLoading(false));
  121. }, [
  122. bookId,
  123. currPage,
  124. keyWord,
  125. keyWords,
  126. match,
  127. orderBy,
  128. pageType,
  129. tags,
  130. view,
  131. bold,
  132. ]);
  133. */
  134. return (
  135. <List
  136. style={{ width: "100%" }}
  137. itemLayout="vertical"
  138. size="small"
  139. dataSource={ftsData}
  140. pagination={{
  141. onChange: onChange,
  142. showQuickJumper: true,
  143. showSizeChanger: false,
  144. current: currPage,
  145. pageSize: 10,
  146. total: total,
  147. position: "both",
  148. showTotal: (total) => {
  149. return `结果: ${total}`;
  150. },
  151. }}
  152. renderItem={(item) => {
  153. let paragraph: number[];
  154. if (view === "title") {
  155. paragraph = [item.paragraph, item.paragraph + 1, item.paragraph + 2];
  156. } else {
  157. paragraph = [item.paragraph - 1, item.paragraph, item.paragraph + 1];
  158. }
  159. let link: string = "";
  160. switch (view) {
  161. case "pali":
  162. link = `/article/para/${item.book}-${item.paragraph}?book=${item.book}&par=${paragraph}&focus=${item.paragraph}`;
  163. break;
  164. case "title":
  165. link = `/article/chapter/${item.book}-${item.paragraph}`;
  166. break;
  167. case "page":
  168. link = `/article/chapter/${item.book}-${item.paragraph}`;
  169. break;
  170. default:
  171. break;
  172. }
  173. let title = "unnamed";
  174. if (item.paliTitle) {
  175. if (item.paliTitle.length > 0) {
  176. title = item.paliTitle;
  177. }
  178. }
  179. return (
  180. <List.Item>
  181. {loading ? (
  182. <div style={{ width: "100%" }}>
  183. <Skeleton active />
  184. </div>
  185. ) : (
  186. <div>
  187. <div>
  188. <PaliText text={item.path ? item.path[0].title : ""} />
  189. </div>
  190. <div>
  191. <Space style={{ color: "gray", fontSize: "80%" }}>
  192. <TocPath
  193. data={item.path?.slice(1)}
  194. style={{ fontSize: "80%" }}
  195. />
  196. {"/"}
  197. <Tag style={{ fontSize: "80%" }}>{item.paragraph}</Tag>
  198. </Space>
  199. </div>
  200. <Title level={4} style={{ fontWeight: 500 }}>
  201. <Link to={link} target="_blank">
  202. {item.title ? item.title : title}
  203. </Link>
  204. </Title>
  205. <div style={{ display: "none" }}>
  206. <Text type="secondary">{item.paliTitle}</Text>
  207. </div>
  208. <div>
  209. <Marked className="search_content" text={item.content} />
  210. </div>
  211. <div>
  212. <AiTranslate
  213. paragraph={`${item.book}-${item.paragraph}`}
  214. trigger
  215. />
  216. </div>
  217. </div>
  218. )}
  219. </List.Item>
  220. );
  221. }}
  222. />
  223. );
  224. };
  225. export default FullTxtSearchResultWidget;