SentSim.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Button, Divider, List, Space, Switch, message } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { ReloadOutlined } from "@ant-design/icons";
  4. import { get } from "../../../request";
  5. import type { ISentenceSimListResponse, ISimSent } from "../../../api/Corpus";
  6. import MdView from "../MdView";
  7. import SentCanRead from "./SentCanRead";
  8. interface IWidget {
  9. book: number;
  10. para: number;
  11. wordStart: number;
  12. wordEnd: number;
  13. channelsId?: string[];
  14. limit?: number;
  15. reload?: boolean;
  16. onReload?: Function;
  17. onCreate?: Function;
  18. }
  19. const SentSimWidget = ({
  20. book,
  21. para,
  22. wordStart,
  23. wordEnd,
  24. limit = 5,
  25. channelsId,
  26. reload = false,
  27. onReload,
  28. onCreate,
  29. }: IWidget) => {
  30. const [initLoading, setInitLoading] = useState(true);
  31. const [loading, setLoading] = useState(false);
  32. const [sim, setSim] = useState(0);
  33. const [offset, setOffset] = useState(0);
  34. const [sentData, setSentData] = useState<ISimSent[]>([]);
  35. const [remain, setRemain] = useState(0);
  36. const load = () => {
  37. let url = `/v2/sent-sim?view=sentence&book=${book}&paragraph=${para}&start=${wordStart}&end=${wordEnd}&mode=edit`;
  38. url += `&limit=${limit}`;
  39. url += `&offset=${offset}`;
  40. url += `&sim=${sim}`;
  41. url += channelsId ? `&channels=${channelsId.join()}` : "";
  42. setLoading(true);
  43. console.log("url", url);
  44. get<ISentenceSimListResponse>(url)
  45. .then((json) => {
  46. if (json.ok) {
  47. console.log("sim load", json.data.rows);
  48. setSentData([...sentData, ...json.data.rows]);
  49. setRemain(json.data.count - sentData.length - json.data.rows.length);
  50. } else {
  51. message.error(json.message);
  52. }
  53. })
  54. .finally(() => {
  55. setInitLoading(false);
  56. setLoading(false);
  57. if (reload && typeof onReload !== "undefined") {
  58. onReload();
  59. }
  60. });
  61. };
  62. useEffect(() => {
  63. load();
  64. }, [offset, sim]);
  65. return (
  66. <>
  67. <SentCanRead
  68. book={book}
  69. para={para}
  70. wordStart={wordStart}
  71. wordEnd={wordEnd}
  72. type="similar"
  73. channelsId={channelsId}
  74. onCreate={onCreate}
  75. />
  76. <List
  77. loading={initLoading}
  78. header={
  79. <div style={{ display: "flex", justifyContent: "space-between" }}>
  80. <span></span>
  81. <Space>
  82. {"只显示相同句"}
  83. <Switch
  84. onChange={(checked: boolean) => {
  85. if (checked) {
  86. setSim(1);
  87. } else {
  88. setSim(0);
  89. }
  90. setOffset(0);
  91. setSentData([]);
  92. }}
  93. />
  94. <Button
  95. type="link"
  96. shape="round"
  97. icon={<ReloadOutlined />}
  98. onClick={() => {}}
  99. />
  100. </Space>
  101. </div>
  102. }
  103. itemLayout="horizontal"
  104. split={false}
  105. loadMore={
  106. <Divider>
  107. <Button
  108. disabled={remain <= 0}
  109. onClick={() => {
  110. setOffset((origin) => origin + limit);
  111. }}
  112. loading={loading}
  113. >
  114. load more
  115. </Button>
  116. </Divider>
  117. }
  118. dataSource={sentData}
  119. renderItem={(item, index) => (
  120. <List.Item>
  121. <MdView html={item.sent} key={index} style={{ width: "100%" }} />
  122. </List.Item>
  123. )}
  124. />
  125. </>
  126. );
  127. };
  128. export default SentSimWidget;