/* eslint-disable @typescript-eslint/no-explicit-any */ import Reaction from '@components/note/atoms/reaction'; import Reply from '@components/note/atoms/reply'; import { User } from '@components/note/atoms/user'; import { ImageCard } from '@components/note/content/preview/imageCard'; import { Video } from '@components/note/content/preview/video'; import dynamic from 'next/dynamic'; import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import ReactPlayer from 'react-player'; const MarkdownPreview = dynamic(() => import('@uiw/react-markdown-preview'), { ssr: false, loading: () =>
, }); export const Content = memo(function Content({ data }: { data: any }) { const [preview, setPreview] = useState({}); const content = useRef(data.content); const urls = useMemo( () => content.current.match( /((http|ftp|https):\/\/)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi ), [] ); useEffect(() => { if (urls !== null && urls.length > 0) { // #TODO: support multiple url let url = urls[0]; // make sure url alway have http:// if (!/^https?:\/\//i.test(url)) { url = 'http://' + url; } // parse url with new URL(); const parseURL = new URL(url, 'https://uselume.xyz'); // #TODO performance test if (parseURL.pathname.toLowerCase().match(/\.(jpg|jpeg|gif|png|webp)$/)) { // add image to preview setPreview({ image: parseURL.href, type: 'image' }); content.current = content.current.replace(parseURL.href, ''); } else if (ReactPlayer.canPlay(parseURL.href)) { // add video to preview setPreview({ url: parseURL.href, type: 'video' }); content.current = content.current.replace(parseURL.href, ''); } // #TODO: support multiple previ3ew } }, [urls]); const previewAttachment = useCallback(() => { if (Object.keys(preview).length > 0) { switch (preview['type']) { case 'image': return