DiscussionTopicChildren.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { List, message, Skeleton } from "antd";
  2. import { IconType } from "antd/lib/notification";
  3. import { useEffect, useState } from "react";
  4. import { useIntl } from "react-intl";
  5. import { get } from "../../request";
  6. import type { ICommentListResponse } from "../../api/Comment";
  7. import type {
  8. ISentHistoryData,
  9. ISentHistoryListResponse,
  10. } from "../corpus/SentHistory";
  11. import SentHistoryGroup from "../corpus/SentHistoryGroup";
  12. import DiscussionCreate from "./DiscussionCreate";
  13. import DiscussionItem, { type IComment } from "./DiscussionItem";
  14. import type { TResType } from "./DiscussionListCard";
  15. interface IItem {
  16. type: "comment" | "sent";
  17. comment?: IComment;
  18. sent?: ISentHistoryData[];
  19. oldSent?: string;
  20. date: number;
  21. }
  22. interface IWidget {
  23. topic?: IComment;
  24. resId?: string;
  25. resType?: TResType;
  26. topicId?: string;
  27. focus?: string;
  28. hideReply?: boolean;
  29. onItemCountChange?: Function;
  30. onTopicCreate?: Function;
  31. }
  32. const DiscussionTopicChildrenWidget = ({
  33. topic,
  34. resId,
  35. resType,
  36. topicId,
  37. focus,
  38. hideReply = false,
  39. onItemCountChange,
  40. onTopicCreate,
  41. }: IWidget) => {
  42. const intl = useIntl();
  43. const [data, setData] = useState<IComment[]>([]);
  44. const [loading, setLoading] = useState(false);
  45. const [history, setHistory] = useState<ISentHistoryData[]>([]);
  46. const [items, setItems] = useState<IItem[]>();
  47. useEffect(() => {
  48. if (loading === false) {
  49. const ele = document.getElementById(`answer-${focus}`);
  50. ele?.scrollIntoView({
  51. behavior: "smooth",
  52. block: "center",
  53. });
  54. console.log("after render");
  55. }
  56. });
  57. useEffect(() => {
  58. const comment: IItem[] = data.map((item) => {
  59. const date = new Date(item.createdAt ? item.createdAt : "").getTime();
  60. return {
  61. type: "comment",
  62. comment: item,
  63. date: date,
  64. };
  65. });
  66. const topicTime = new Date(
  67. topic?.createdAt ? topic?.createdAt : ""
  68. ).getTime();
  69. let firstHis = history.findIndex(
  70. (value) =>
  71. new Date(value.created_at ? value.created_at : "").getTime() > topicTime
  72. );
  73. if (firstHis < 0) {
  74. firstHis = history.length;
  75. }
  76. const hisFiltered = history.filter(
  77. (_value, index) => index >= firstHis - 1
  78. );
  79. const his: IItem[] = hisFiltered.map((item, index) => {
  80. return {
  81. type: "sent",
  82. sent: [item],
  83. date: new Date(item.created_at ? item.created_at : "").getTime(),
  84. oldSent: index > 0 ? hisFiltered[index - 1].content : undefined,
  85. };
  86. });
  87. const mixItems = [...comment, ...his];
  88. mixItems.sort((a, b) => a.date - b.date);
  89. console.log("mixItems", mixItems);
  90. const newMixItems: IItem[] = [];
  91. let currSent: ISentHistoryData[] = [];
  92. let currOldSent: string | undefined;
  93. let sentBegin = false;
  94. mixItems.forEach((value, _index, _array) => {
  95. if (value.type === "comment") {
  96. if (sentBegin) {
  97. sentBegin = false;
  98. newMixItems.push({
  99. type: "sent",
  100. sent: currSent,
  101. date: 0,
  102. oldSent: currOldSent ? currOldSent : currSent[0].content,
  103. });
  104. }
  105. newMixItems.push(value);
  106. } else {
  107. if (value.sent && value.sent.length > 0) {
  108. if (sentBegin) {
  109. currSent.push(value.sent[0]);
  110. } else {
  111. sentBegin = true;
  112. currSent = value.sent;
  113. currOldSent = value.oldSent;
  114. }
  115. }
  116. }
  117. });
  118. if (sentBegin) {
  119. sentBegin = false;
  120. newMixItems.push({
  121. type: "sent",
  122. sent: currSent,
  123. date: 0,
  124. oldSent: currOldSent ? currOldSent : currSent[0].content,
  125. });
  126. }
  127. setItems(newMixItems);
  128. }, [data, history, topic?.createdAt]);
  129. useEffect(() => {
  130. if (resType === "sentence" && resId) {
  131. const url = `/v2/sent_history?view=sentence&id=${resId}&order=created_at&dir=asc`;
  132. setLoading(true);
  133. get<ISentHistoryListResponse>(url)
  134. .then((res) => {
  135. if (res.ok) {
  136. setHistory(res.data.rows);
  137. }
  138. })
  139. .finally(() => setLoading(false));
  140. }
  141. }, [resId, resType]);
  142. useEffect(() => {
  143. console.log("topicId", topicId);
  144. if (typeof topicId === "undefined") {
  145. return;
  146. }
  147. setLoading(true);
  148. const url = `/v2/discussion?view=answer&id=${topicId}`;
  149. console.info("api request", url);
  150. get<ICommentListResponse>(url)
  151. .then((json) => {
  152. console.debug("api response", json);
  153. if (json.ok) {
  154. const discussions: IComment[] = json.data.rows.map((item) => {
  155. return {
  156. id: item.id,
  157. resId: item.res_id,
  158. resType: item.res_type,
  159. type: item.type,
  160. user: item.editor,
  161. parent: item.parent,
  162. title: item.title,
  163. content: item.content,
  164. status: item.status,
  165. childrenCount: item.children_count,
  166. createdAt: item.created_at,
  167. updatedAt: item.updated_at,
  168. };
  169. });
  170. setData(discussions);
  171. } else {
  172. message.error(json.message);
  173. }
  174. })
  175. .finally(() => {
  176. setLoading(false);
  177. })
  178. .catch((e) => {
  179. message.error(e.message);
  180. });
  181. }, [intl, topicId]);
  182. return (
  183. <div>
  184. {loading ? (
  185. <Skeleton title={{ width: 200 }} paragraph={{ rows: 1 }} active />
  186. ) : (
  187. <List
  188. pagination={false}
  189. size="small"
  190. itemLayout="horizontal"
  191. dataSource={items}
  192. renderItem={(item) => {
  193. return (
  194. <List.Item>
  195. {item.type === "comment" ? (
  196. item.comment ? (
  197. <DiscussionItem
  198. data={item.comment}
  199. isFocus={item.comment.id === focus ? true : false}
  200. onDelete={() => {
  201. if (typeof onItemCountChange !== "undefined") {
  202. onItemCountChange(
  203. data.length - 1,
  204. item.comment?.parent
  205. );
  206. }
  207. setData((origin) => {
  208. return origin.filter(
  209. (value) => value.id !== item.comment?.id
  210. );
  211. });
  212. }}
  213. />
  214. ) : undefined
  215. ) : (
  216. <SentHistoryGroup
  217. data={item.sent}
  218. oldContent={item.oldSent}
  219. />
  220. )}
  221. </List.Item>
  222. );
  223. }}
  224. />
  225. )}
  226. {hideReply ? (
  227. <></>
  228. ) : (
  229. <DiscussionCreate
  230. resId={resId}
  231. resType={resType}
  232. contentType="markdown"
  233. parent={topicId}
  234. topicId={topicId}
  235. topic={topic}
  236. onCreated={(value: IComment) => {
  237. const newData = JSON.parse(JSON.stringify(value));
  238. setData([...data, newData]);
  239. if (typeof onItemCountChange !== "undefined") {
  240. onItemCountChange(data.length + 1, value.parent);
  241. }
  242. }}
  243. onTopicCreated={(value: IconType) => {
  244. if (typeof onTopicCreate !== "undefined") {
  245. onTopicCreate(value);
  246. }
  247. }}
  248. />
  249. )}
  250. </div>
  251. );
  252. };
  253. export default DiscussionTopicChildrenWidget;