| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import { List, message, Skeleton } from "antd";
- import { IconType } from "antd/lib/notification";
- import { useEffect, useState } from "react";
- import { useIntl } from "react-intl";
- import { get } from "../../request";
- import type { ICommentListResponse } from "../../api/Comment";
- import type {
- ISentHistoryData,
- ISentHistoryListResponse,
- } from "../corpus/SentHistory";
- import SentHistoryGroup from "../corpus/SentHistoryGroup";
- import DiscussionCreate from "./DiscussionCreate";
- import DiscussionItem, { type IComment } from "./DiscussionItem";
- import type { TResType } from "./DiscussionListCard";
- interface IItem {
- type: "comment" | "sent";
- comment?: IComment;
- sent?: ISentHistoryData[];
- oldSent?: string;
- date: number;
- }
- interface IWidget {
- topic?: IComment;
- resId?: string;
- resType?: TResType;
- topicId?: string;
- focus?: string;
- hideReply?: boolean;
- onItemCountChange?: Function;
- onTopicCreate?: Function;
- }
- const DiscussionTopicChildrenWidget = ({
- topic,
- resId,
- resType,
- topicId,
- focus,
- hideReply = false,
- onItemCountChange,
- onTopicCreate,
- }: IWidget) => {
- const intl = useIntl();
- const [data, setData] = useState<IComment[]>([]);
- const [loading, setLoading] = useState(false);
- const [history, setHistory] = useState<ISentHistoryData[]>([]);
- const [items, setItems] = useState<IItem[]>();
- useEffect(() => {
- if (loading === false) {
- const ele = document.getElementById(`answer-${focus}`);
- ele?.scrollIntoView({
- behavior: "smooth",
- block: "center",
- });
- console.log("after render");
- }
- });
- useEffect(() => {
- const comment: IItem[] = data.map((item) => {
- const date = new Date(item.createdAt ? item.createdAt : "").getTime();
- return {
- type: "comment",
- comment: item,
- date: date,
- };
- });
- const topicTime = new Date(
- topic?.createdAt ? topic?.createdAt : ""
- ).getTime();
- let firstHis = history.findIndex(
- (value) =>
- new Date(value.created_at ? value.created_at : "").getTime() > topicTime
- );
- if (firstHis < 0) {
- firstHis = history.length;
- }
- const hisFiltered = history.filter(
- (_value, index) => index >= firstHis - 1
- );
- const his: IItem[] = hisFiltered.map((item, index) => {
- return {
- type: "sent",
- sent: [item],
- date: new Date(item.created_at ? item.created_at : "").getTime(),
- oldSent: index > 0 ? hisFiltered[index - 1].content : undefined,
- };
- });
- const mixItems = [...comment, ...his];
- mixItems.sort((a, b) => a.date - b.date);
- console.log("mixItems", mixItems);
- const newMixItems: IItem[] = [];
- let currSent: ISentHistoryData[] = [];
- let currOldSent: string | undefined;
- let sentBegin = false;
- mixItems.forEach((value, _index, _array) => {
- if (value.type === "comment") {
- if (sentBegin) {
- sentBegin = false;
- newMixItems.push({
- type: "sent",
- sent: currSent,
- date: 0,
- oldSent: currOldSent ? currOldSent : currSent[0].content,
- });
- }
- newMixItems.push(value);
- } else {
- if (value.sent && value.sent.length > 0) {
- if (sentBegin) {
- currSent.push(value.sent[0]);
- } else {
- sentBegin = true;
- currSent = value.sent;
- currOldSent = value.oldSent;
- }
- }
- }
- });
- if (sentBegin) {
- sentBegin = false;
- newMixItems.push({
- type: "sent",
- sent: currSent,
- date: 0,
- oldSent: currOldSent ? currOldSent : currSent[0].content,
- });
- }
- setItems(newMixItems);
- }, [data, history, topic?.createdAt]);
- useEffect(() => {
- if (resType === "sentence" && resId) {
- const url = `/v2/sent_history?view=sentence&id=${resId}&order=created_at&dir=asc`;
- setLoading(true);
- get<ISentHistoryListResponse>(url)
- .then((res) => {
- if (res.ok) {
- setHistory(res.data.rows);
- }
- })
- .finally(() => setLoading(false));
- }
- }, [resId, resType]);
- useEffect(() => {
- console.log("topicId", topicId);
- if (typeof topicId === "undefined") {
- return;
- }
- setLoading(true);
- const url = `/v2/discussion?view=answer&id=${topicId}`;
- console.info("api request", url);
- get<ICommentListResponse>(url)
- .then((json) => {
- console.debug("api response", json);
- if (json.ok) {
- const discussions: IComment[] = json.data.rows.map((item) => {
- return {
- id: item.id,
- resId: item.res_id,
- resType: item.res_type,
- type: item.type,
- user: item.editor,
- parent: item.parent,
- title: item.title,
- content: item.content,
- status: item.status,
- childrenCount: item.children_count,
- createdAt: item.created_at,
- updatedAt: item.updated_at,
- };
- });
- setData(discussions);
- } else {
- message.error(json.message);
- }
- })
- .finally(() => {
- setLoading(false);
- })
- .catch((e) => {
- message.error(e.message);
- });
- }, [intl, topicId]);
- return (
- <div>
- {loading ? (
- <Skeleton title={{ width: 200 }} paragraph={{ rows: 1 }} active />
- ) : (
- <List
- pagination={false}
- size="small"
- itemLayout="horizontal"
- dataSource={items}
- renderItem={(item) => {
- return (
- <List.Item>
- {item.type === "comment" ? (
- item.comment ? (
- <DiscussionItem
- data={item.comment}
- isFocus={item.comment.id === focus ? true : false}
- onDelete={() => {
- if (typeof onItemCountChange !== "undefined") {
- onItemCountChange(
- data.length - 1,
- item.comment?.parent
- );
- }
- setData((origin) => {
- return origin.filter(
- (value) => value.id !== item.comment?.id
- );
- });
- }}
- />
- ) : undefined
- ) : (
- <SentHistoryGroup
- data={item.sent}
- oldContent={item.oldSent}
- />
- )}
- </List.Item>
- );
- }}
- />
- )}
- {hideReply ? (
- <></>
- ) : (
- <DiscussionCreate
- resId={resId}
- resType={resType}
- contentType="markdown"
- parent={topicId}
- topicId={topicId}
- topic={topic}
- onCreated={(value: IComment) => {
- const newData = JSON.parse(JSON.stringify(value));
- setData([...data, newData]);
- if (typeof onItemCountChange !== "undefined") {
- onItemCountChange(data.length + 1, value.parent);
- }
- }}
- onTopicCreated={(value: IconType) => {
- if (typeof onTopicCreate !== "undefined") {
- onTopicCreate(value);
- }
- }}
- />
- )}
- </div>
- );
- };
- export default DiscussionTopicChildrenWidget;
|