FullTextSearchResult.tsx 6.0 KB

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