diff --git a/src/components/note/atoms/user.tsx b/src/components/note/atoms/user.tsx index 8d8713f1..1a4f43df 100644 --- a/src/components/note/atoms/user.tsx +++ b/src/components/note/atoms/user.tsx @@ -42,11 +42,11 @@ export const User = memo(function User({ pubkey, time }: { pubkey: string; time:
-
+
{userData?.name ? userData.name : truncate(pubkey, 16, ' .... ')} - · + · {time} diff --git a/src/components/note/content/ImagePreview.tsx b/src/components/note/content/ImagePreview.tsx deleted file mode 100644 index de9482a8..00000000 --- a/src/components/note/content/ImagePreview.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import Image from 'next/image'; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export default function ImagePreview({ data }: { data: object }) { - return ( -
-
- image preview -
-
- ); -} diff --git a/src/components/note/content/index.tsx b/src/components/note/content/index.tsx index f8d68312..d0aa1b72 100644 --- a/src/components/note/content/index.tsx +++ b/src/components/note/content/index.tsx @@ -1,16 +1,20 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import ImageCard from '@components/note/content/preview/imageCard'; +import Video from '@components/note/content/preview/video'; + import { MarkdownPreviewProps } from '@uiw/react-markdown-preview'; import dynamic from 'next/dynamic'; -import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import ReactPlayer from 'react-player'; const MarkdownPreview = dynamic(() => import('@uiw/react-markdown-preview'), { ssr: false, }); export default function Content({ data }: { data: string }) { - const imagesRef = useRef([]); - const videosRef = useRef([]); + const [preview, setPreview] = useState({}); + const content = useRef(data); const urls = useMemo( () => data.match( @@ -19,52 +23,56 @@ export default function Content({ data }: { data: string }) { [data] ); - const extractURL = useCallback((urls: any[]) => { - if (urls !== null && urls.length > 0) { - urls.forEach((url: string | URL) => { - const parseURL = new URL(url); - const path = parseURL.pathname.toLowerCase(); - switch (path) { - case path.match(/\.(jpg|jpeg|gif|png|webp)$/)?.input: - imagesRef.current.push(parseURL.href); - break; - case path.match( - /(http:|https:)?\/\/(www\.)?(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/ - )?.input: - videosRef.current.push(parseURL.href); - break; - case path.match(/\.(mp4|webm|m4v|mov|avi|mkv|flv)$/)?.input: - videosRef.current.push(parseURL.href); - break; - default: - break; - } - }); - } - }, []); - useEffect(() => { - extractURL(urls); - }, [extractURL, urls]); + if (urls !== null && urls.length > 0) { + const parseURL = new URL(urls[0]); + + 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 preview + } + }, [urls]); + + const previewAttachment = useCallback(() => { + if (Object.keys(preview).length > 0) { + switch (preview['type']) { + case 'image': + return ; + case 'video': + return