"use client"; import { useState } from "react"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { ExternalLink } from "lucide-react"; interface TelegramPostPreviewProps { text: string; mediaItems?: Array<{ media_type: string; media_file_id: string }>; buttons?: Array<{ text: string; url: string }>; inviteLinkPreview?: string; } export function TelegramPostPreview({ text, mediaItems, buttons, inviteLinkPreview = "https://t.me/+AbCdEfGhIjK", }: TelegramPostPreviewProps) { const [hoveredLink, setHoveredLink] = useState(null); const renderTextWithLinks = () => { const result: React.ReactNode[] = []; let lastIndex = 0; const patterns = [ { regex: /([^<]*)<\/a>/gi, type: 'tg-link' }, { regex: /\{invite_link\}/gi, type: 'single' }, ]; const matches: { start: number; end: number; type: string; text?: string }[] = []; patterns.forEach(({ regex, type }) => { let match; while ((match = regex.exec(text)) !== null) { matches.push({ start: match.index!, end: match.index! + match[0].length, type, text: match[1], }); } }); matches.sort((a, b) => a.start - b.start); matches.forEach((match) => { if (lastIndex < match.start) { result.push({text.slice(lastIndex, match.start)}); } const linkContent = match.type === 'tg-link' && match.text ? match.text : '[ссылка]'; result.push( setHoveredLink(open ? `${match.start}-${match.end}` : null)}> e.preventDefault()} className="text-primary font-medium underline hover:opacity-80 transition-opacity" onMouseEnter={() => setHoveredLink(`${match.start}-${match.end}`)} onMouseLeave={() => setHoveredLink(null)} > {linkContent}

Сюда будет подставлена ссылка на канал:

{inviteLinkPreview}
); lastIndex = match.end; }); if (lastIndex < text.length) { result.push({text.slice(lastIndex)}); } return result; }; const renderButton = (button: { text: string; url: string }, index: number) => { const hasInviteLink = button.url === '{{invite_link}}'; return ( setHoveredLink(open ? `button-${index}` : null)}> {hasInviteLink && (

Сюда будет подставлена ссылка на канал:

{inviteLinkPreview}
)}
); }; const getMediaGrid = () => { if (!mediaItems || mediaItems.length === 0) return null; const count = mediaItems.length; if (count === 1) { return (
); } if (count === 2) { return (
{mediaItems.map((media, index) => (
))}
); } if (count === 3) { return (
); } if (count >= 4) { return (
{mediaItems.slice(0, 4).map((media, index) => (
))} {mediaItems.length > 4 && (
+{mediaItems.length - 4}
)}
); } return null; }; return (
{mediaItems && mediaItems.length > 0 && (
{getMediaGrid()}
)}
{renderTextWithLinks()}
{buttons && buttons.length > 0 && (
{buttons.map((button, index) => renderButton(button, index))}
)}
); } function MediaPreview({ media }: { media: { media_type: string; media_file_id: string } }) { return (
{media.media_type === "photo" && ( 📷 )} {media.media_type === "video" && ( 🎬 )} {media.media_type === "animation" && ( 🎞️ )} {media.media_file_id.slice(0, 12)}...
); }