|
| 1 | +import React, { Fragment } from 'react'; |
| 2 | +import dayjs from 'dayjs'; |
| 3 | +import Markdown from '@/component/Markdown'; |
| 4 | + |
| 5 | +import { Comment, Avatar, Divider } from 'antd'; |
| 6 | +import { |
| 7 | + LikeFilled, |
| 8 | + EditFilled, |
| 9 | + DeleteFilled, |
| 10 | + CommentOutlined, |
| 11 | +} from '@ant-design/icons'; |
| 12 | + |
| 13 | +import * as styles from './index.less'; |
| 14 | + |
| 15 | +const unflatten = (array: Node[], parent?: Node, tree?: Node[]) => { |
| 16 | + let _parent = parent || { id: null, children: [] }; |
| 17 | + let _tree = tree || []; |
| 18 | + |
| 19 | + const children = array.filter((child) => child.reply_id === _parent.id); |
| 20 | + |
| 21 | + if (children.length > 0) { |
| 22 | + if (!_parent.id) { |
| 23 | + _tree = children; |
| 24 | + } else { |
| 25 | + _parent.children = children; |
| 26 | + } |
| 27 | + children.forEach((child) => unflatten(array, child)); |
| 28 | + } |
| 29 | + |
| 30 | + return _tree; |
| 31 | +}; |
| 32 | + |
| 33 | +const CommentList: React.FC<Props> = (props) => { |
| 34 | + const { list, onReply, replyRender } = props; |
| 35 | + const tree = unflatten(list); |
| 36 | + |
| 37 | + const ComentDetail: React.FC<{ |
| 38 | + data: Node; |
| 39 | + }> = ({ data }) => { |
| 40 | + const { id, author, content, create_at, children } = data; |
| 41 | + |
| 42 | + return ( |
| 43 | + <Fragment key={`fragment-${id}`}> |
| 44 | + <Divider type="horizontal" key={`divider-${id}`} /> |
| 45 | + |
| 46 | + <Comment |
| 47 | + key={id} |
| 48 | + actions={[ |
| 49 | + <LikeFilled />, |
| 50 | + <EditFilled />, |
| 51 | + <DeleteFilled />, |
| 52 | + <CommentOutlined |
| 53 | + onClick={() => { |
| 54 | + onReply(data); |
| 55 | + }} |
| 56 | + />, |
| 57 | + ]} |
| 58 | + author={<span>{author.loginname}</span>} |
| 59 | + datetime={ |
| 60 | + <span>{dayjs(create_at).format('YYYY-MM-DD hh:mm:ss')}</span> |
| 61 | + } |
| 62 | + avatar={<Avatar src={author.avatar_url} alt={author.loginname} />} |
| 63 | + content={ |
| 64 | + <div className={styles.detail}> |
| 65 | + <Markdown type="render" value={content} /> |
| 66 | + </div> |
| 67 | + } |
| 68 | + > |
| 69 | + {replyRender(id)} |
| 70 | + |
| 71 | + {children?.map((item) => ( |
| 72 | + <ComentDetail key={`detail-${id}-${item.id}`} data={item} /> |
| 73 | + ))} |
| 74 | + </Comment> |
| 75 | + </Fragment> |
| 76 | + ); |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className={styles.list}> |
| 81 | + {tree.map((item) => ( |
| 82 | + <ComentDetail key={`list-${item.id}`} data={item} /> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export default CommentList; |
| 89 | + |
| 90 | +interface Props { |
| 91 | + list: ReplyModel[]; |
| 92 | + |
| 93 | + onReply: (record: Node) => void; |
| 94 | + replyRender: (id: string) => React.ReactNode; |
| 95 | +} |
| 96 | + |
| 97 | +interface Node extends ReplyModel { |
| 98 | + children?: Node[]; |
| 99 | +} |
0 commit comments