'use client' import React, { useCallback, useState } from 'react' import { RiDeleteBinLine, RiSendPlane2Line, RiUser3Line } from '@remixicon/react' import useSWR from 'swr' import { Mention, MentionsInput } from 'react-mentions' import classNames from '@/utils/classnames' import { formatDistanceToNow } from '@/utils/time' import { useToastContext } from '@/app/components/base/toast' import { del, get, post } from '@/service/base' import styles from './segment-comments.module.css' type SegmentCommentsProps = { datasetId: string segmentId: string } type Comment = { id: string content: string account_id: string account_name: string account_avatar: string mentioned_account_ids: string[] created_at: string } const SegmentComments: React.FC = ({ datasetId, segmentId, }) => { const { notify } = useToastContext() const [content, setContent] = useState('') const [mentionedIds, setMentionedIds] = useState([]) const [isSubmitting, setIsSubmitting] = useState(false) // 获取团队成员列表(用于@提及) const { data: membersData } = useSWR('/workspaces/current/members', get) const members = membersData?.accounts || [] // 获取评论列表 const { data: commentsData, mutate } = useSWR( `/datasets/${datasetId}/segments/${segmentId}/comments`, get, ) const comments: Comment[] = commentsData?.comments || [] // 提交评论 const handleSubmit = useCallback(async () => { if (!content.trim() || isSubmitting) return setIsSubmitting(true) // 调试日志 console.log('Submitting comment with mentioned_account_ids:', mentionedIds) console.log('Comment content:', content) try { await post(`/datasets/${datasetId}/segments/${segmentId}/comments`, { body: { content: content.trim(), mentioned_account_ids: mentionedIds, }, }) setContent('') setMentionedIds([]) mutate() // 刷新评论列表 notify({ type: 'success', message: '评论发送成功', }) } catch { notify({ type: 'error', message: '评论发送失败', }) } finally { setIsSubmitting(false) } }, [content, datasetId, segmentId, mentionedIds, mutate, notify, isSubmitting]) // 删除评论 const handleDelete = useCallback(async (commentId: string) => { try { await del(`/datasets/${datasetId}/segments/${segmentId}/comments/${commentId}`) mutate() // 刷新评论列表 notify({ type: 'success', message: '删除成功', }) } catch { notify({ type: 'error', message: '删除失败', }) } }, [datasetId, segmentId, mutate, notify]) // 处理@提及 const handleMentionChange = useCallback((event: any, newValue: string, newPlainTextValue: string, mentions: any[]) => { console.log('Mention change - mentions:', mentions) console.log('Mention change - newValue:', newValue) setContent(newValue) setMentionedIds(mentions.map(m => m.id)) }, []) return (
评论 ({comments.length})
{/* 评论输入框 */}
({ id: m.id, display: m.name, }))} markup="@[__display__](__id__)" displayTransform={(_id, display) => `@${display}`} />
使用 @ 提及团队成员
{/* 评论列表 */}
{comments.length === 0 ? (

暂无评论,快来发表第一条评论吧!

) : ( comments.map(comment => (
{/* 头像 */} {comment.account_avatar ? ( {comment.account_name} ) : (
)} {/* 名称和时间 */}
{comment.account_name}
{formatDistanceToNow(new Date(comment.created_at))}
{/* 删除按钮 */}
{/* 评论内容 */}
{comment.content}
)) )}
) } export default SegmentComments