import { renderRichTextToReact } from '@kkk/richtext' import { differenceInSeconds, format, formatDistanceToNow } from 'date-fns' import { zhCN } from 'date-fns/locale' import { Heart, MessageCircle } from 'lucide-react' import React from 'react' import { isDark } from '../../../../utils/theme' import { DefaultLayout } from '../../../components/DefaultLayout' import { QRCodeWithAvatar } from '../../../components/QRCodeWithAvatar' import type { PosterProps } from '../../../types/ctx' import type { XiaohongshuCommentData } from './types' const xiaohongshuMentionClassName = 'text-[#13386c] dark:text-[#c7daef]' const renderXiaohongshuCommentRichText = (content: XiaohongshuCommentData['CommentsData'][0]['content']) => { return renderRichTextToReact(content, { mention: { className: xiaohongshuMentionClassName } }) } const formatXiaohongshuCommentTime = (timestamp: number): string => { if (!timestamp) { return '' } const commentDate = new Date(timestamp) const diffSeconds = differenceInSeconds(new Date(), commentDate) if (diffSeconds < 30) { return '刚刚' } if (diffSeconds < 7776000) { return formatDistanceToNow(commentDate, { locale: zhCN, addSuffix: true }) } return format(commentDate, 'yyyy-MM-dd') } const formatXiaohongshuLikeCount = (count: string): string => { const numberCount = Number(count) if (Number.isFinite(numberCount) && numberCount >= 10000) { return `${(numberCount / 10000).toFixed(1)}w` } return count || '0' } /** * 笔记信息头部组件 * @param props 组件属性 * @returns JSX元素 */ const NoteInfoHeader: React.FC = (props) => { return (
{/* Logo 图片区域 */}
小红书Logo { const target = e.target as HTMLImageElement target.style.display = 'none' const parent = target.parentElement if (parent) { parent.innerHTML = '
小红书
' } }} />
{/* 文字信息区域 */}
笔记类型:{props.Type}
评论数量:{props.CommentLength}条
{props.Type === '图文' && props.ImageLength && (
图片数量:{props.ImageLength}张
)}

扫码查看原笔记

) } /** * 单个评论组件 * @param props 组件属性 * @returns JSX元素 */ const CommentItemComponent: React.FC = ({ ...props }, isLast = false) => { return (
{/* 用户头像 */} 用户头像 {/* 评论内容 */}
{/* 用户信息 */}
{props.user_info.nickname} {props.show_tags && props.show_tags.length > 0 && props.show_tags.map((tag, index) => { if (tag === 'is_author') { return (
作者
) } else if (tag === 'user_top') { return (
置顶评论
) } else { return null } })}
{/* 评论文本 */}
{renderXiaohongshuCommentRichText(props.content)}
{/* 评论图片 */} {props.pictures && props.pictures.length > 0 && (
评论图片
)} {/* 底部信息和操作区域 */}
{formatXiaohongshuCommentTime(props.create_time)} {props.ip_location} {parseInt(props.sub_comment_count) > 0 ? ( 共{props.sub_comment_count}条回复 ) : ( 回复 )}
{/* 点赞按钮 */}
{formatXiaohongshuLikeCount(props.like_count)}
{/* 回复按钮 */}
{/* 子评论 */} {props.sub_comments && props.sub_comments.length > 0 && (
{props.sub_comments.map((subComment, index) => (
用户头像
{subComment.user_info.nickname} {subComment.show_tags && subComment.show_tags.length > 0 && subComment.show_tags.map((tag, tagIndex) => tag === 'is_author' ? (
作者
) : null )}
{renderRichTextToReact(subComment.content, { mention: { className: xiaohongshuMentionClassName } })}
{formatXiaohongshuCommentTime(subComment.create_time)} {subComment.ip_location}
{formatXiaohongshuLikeCount(subComment.like_count)}
))}
)}
) } /** * 小红书评论组件 * @param props 组件属性 * @returns JSX元素 */ export const XiaohongshuComment: React.FC> = React.memo((props) => { return (
{/* 页面头部 */} {/* 评论列表 */}
{props.data.CommentsData.length > 0 ? (
{props.data.CommentsData.map((comment, index) => ( ))}
) : (

{props.data.ErrorText ?? '暂无评论'}

)}
) }) export default XiaohongshuComment