SuggestionList.tsx 3.0 KB

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