'use client'; import React from 'react'; import { Image as ImageIcon, Video } from 'lucide-react'; import type { VideoAsset } from '@/components/video-page/types'; const URL_REGEX = /(https?:\/\/[^\s]+)/g; const ASSET_MENTION_REGEX = /@\[(.+?)\]\(asset:([a-z0-9]+)\)/gi; interface CommentRichTextProps { text: string; onAssetMentionClick?: (assetId: string) => void; assets?: VideoAsset[]; } function renderUrls(text: string): React.ReactNode[] { const parts = text.split(URL_REGEX); return parts.map((part, index) => { if (/^https?:\/\/[^\s]+$/.test(part)) { return ( event.stopPropagation()} > {part} ); } return {part}; }); } export function CommentRichText({ text, onAssetMentionClick, assets = [] }: CommentRichTextProps) { const nodes: React.ReactNode[] = []; let lastIndex = 0; for (const match of text.matchAll(ASSET_MENTION_REGEX)) { const mentionIndex = match.index ?? -1; if (mentionIndex < 0) continue; if (mentionIndex > lastIndex) { nodes.push(...renderUrls(text.slice(lastIndex, mentionIndex))); } const fallbackLabel = match[1] || 'asset'; const assetId = match[2] || ''; const matchedAsset = assets.find((asset) => asset.id === assetId); const label = matchedAsset?.displayName || fallbackLabel; const isVideoAsset = matchedAsset?.kind === 'VIDEO'; nodes.push( ); lastIndex = mentionIndex + match[0].length; } if (lastIndex < text.length) { nodes.push(...renderUrls(text.slice(lastIndex))); } return <>{nodes}; }