SuggestionList.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Button, List, message, Skeleton, Space, Switch } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { ReloadOutlined } from "@ant-design/icons";
  4. import { get } from "../../../request";
  5. import type { ISuggestionListResponse } from "../../../api/Suggestion";
  6. import type { IChannel } from "../../channel/Channel";
  7. import type { ISentence } from "../SentEdit";
  8. import SentCell from "./SentCell";
  9. interface IWidget {
  10. book: number;
  11. para: number;
  12. wordStart: number;
  13. wordEnd: number;
  14. content?: string | null;
  15. channel: IChannel;
  16. enable?: boolean;
  17. reload?: boolean;
  18. onReload?: Function;
  19. onChange?: Function;
  20. }
  21. const SuggestionListWidget = ({
  22. book,
  23. para,
  24. wordStart,
  25. wordEnd,
  26. channel,
  27. content,
  28. reload = false,
  29. enable = true,
  30. onReload,
  31. onChange,
  32. }: IWidget) => {
  33. const [sentData, setSentData] = useState<ISentence[]>([]);
  34. const [loading, setLoading] = useState(false);
  35. const [showDiff, setShowDiff] = useState(true);
  36. const load = () => {
  37. if (!enable) {
  38. return;
  39. }
  40. const url = `/v2/sentpr?view=sent-info&book=${book}&para=${para}&start=${wordStart}&end=${wordEnd}&channel=${channel.id}`;
  41. console.log("url", url);
  42. setLoading(true);
  43. get<ISuggestionListResponse>(url)
  44. .then((json) => {
  45. if (json.ok) {
  46. const newData: ISentence[] = json.data.rows.map((item) => {
  47. return {
  48. id: item.id,
  49. uid: item.uid,
  50. content: item.content,
  51. html: item.html,
  52. book: item.book,
  53. para: item.paragraph,
  54. wordStart: item.word_start,
  55. wordEnd: item.word_end,
  56. editor: item.editor,
  57. channel: { name: item.channel.name, id: item.channel.id },
  58. updateAt: item.updated_at,
  59. };
  60. });
  61. setSentData(newData);
  62. if (typeof onChange !== "undefined") {
  63. onChange(json.data.count);
  64. }
  65. } else {
  66. message.error(json.message);
  67. }
  68. })
  69. .finally(() => {
  70. setLoading(false);
  71. if (reload && typeof onReload !== "undefined") {
  72. onReload();
  73. }
  74. });
  75. };
  76. useEffect(() => {
  77. load();
  78. }, [book, channel.id, para, reload, wordEnd, wordStart]);
  79. useEffect(() => {
  80. if (reload) {
  81. load();
  82. }
  83. }, [reload]);
  84. return (
  85. <>
  86. {loading ? (
  87. <Skeleton />
  88. ) : (
  89. <>
  90. <List
  91. header={
  92. <div style={{ textAlign: "right" }}>
  93. <Space>
  94. <Button
  95. type="link"
  96. size="small"
  97. icon={<ReloadOutlined />}
  98. onClick={() => load()}
  99. ></Button>
  100. {"文本比对"}
  101. <Switch
  102. size="small"
  103. defaultChecked
  104. onChange={(checked) => setShowDiff(checked)}
  105. />
  106. </Space>
  107. </div>
  108. }
  109. itemLayout="vertical"
  110. size="small"
  111. dataSource={sentData}
  112. renderItem={(item, id) => (
  113. <List.Item>
  114. <SentCell
  115. value={item}
  116. key={id}
  117. isPr={true}
  118. showDiff={showDiff}
  119. diffText={content}
  120. onDelete={() => load()}
  121. onChange={() => load()}
  122. />
  123. </List.Item>
  124. )}
  125. />
  126. </>
  127. )}
  128. </>
  129. );
  130. };
  131. export default SuggestionListWidget;