import { NDKKind } from '@nostr-dev-kit/ndk'
import { ProfileLink } from 'components/ProfileSection'
import { nip19 } from 'nostr-tools'
import React, { useCallback, useContext, useMemo, useState } from 'react'
import { Fragment } from 'react/jsx-runtime'
import { BlogPreview } from './internal/BlogPreview'
import { ModPreview } from './internal/ModPreview'
import { NoteWrapper } from './internal/NoteWrapper'
import {
getIdFromYoutubeLink,
isValidAudioUrl,
isValidImageUrl,
isValidUrl,
isValidVideoUrl,
isYoutubeLink
} from 'utils'
import FsLightbox from 'fslightbox-react'
import { createPortal } from 'react-dom'
import { CommentDepthContext } from 'contexts/CommentDepthContext'
import { Link } from 'react-router-dom'
import { appRoutes } from 'routes'
import { truncate } from 'lodash'
interface NoteRenderProps {
content: string
}
const link =
/(?:https?:\/\/|www\.)(?:[a-zA-Z0-9.-]+\.[a-zA-Z]+(?::\d+)?)(?:[/?#][\p{L}\p{N}\p{M}&.-/?=#\-@%+_,:!~*]*)?/u
const nostrMention =
/(?:nostr:|@)?(?:npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}/i
const nostrEntity =
/(npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,}/i
const nostrNip05Mention = /(?:nostr:|@)[^\s]{1,64}@[^\s]+\.[^\s]{2,}/i
const nip05Entity = /(?:nostr:|@)([^\s]{1,64}@[^\s]+\.[^\s]{2,})/i
export const NoteRender = ({ content }: NoteRenderProps) => {
const depth = useContext(CommentDepthContext)
const [lightBoxController, setLightBoxController] = useState({
toggler: false,
slide: 1
})
const slides = useMemo(() => {
const sources: { url: string; type: 'image' | 'video' | 'youtube' }[] = []
const parts = content.matchAll(new RegExp(`${link.source}`, 'gui'))
;[...parts]
.filter((p) => typeof p !== 'undefined')
.forEach((part) => {
const [href] = part
if (href && isValidUrl(href)) {
if (isValidImageUrl(href)) {
sources.push({ url: href, type: 'image' })
} else if (isValidVideoUrl(href)) {
sources.push({ url: href, type: 'video' })
} else if (isYoutubeLink(href)) {
const id = getIdFromYoutubeLink(href)
if (id) {
sources.push({
url: `https://www.youtube.com/embed/${id}`,
type: 'youtube'
})
}
}
}
})
return sources
}, [content])
const openLightBoxOnSlide = useCallback(
(url: string) => {
slides.length &&
setLightBoxController((prev) => ({
toggler: !prev.toggler,
slide: slides.findIndex((s) => s.url === url) + 1
}))
},
[slides]
)
const _content = useMemo(() => {
if (!content) return
const parts = content.split(
new RegExp(
`(${link.source})|(${nostrMention.source})|(${nostrNip05Mention.source})`,
'gui'
)
)
const _parts = parts
.filter((p) => typeof p !== 'undefined')
.map((part, index) => {
const key = `${index}-${part}`
if (link.test(part)) {
const [href] = part.match(link) || []
if (href && isValidUrl(href)) {
if (isValidImageUrl(href)) {
// Image
return (
openLightBoxOnSlide(href)}
/>
)
} else if (isValidVideoUrl(href)) {
// Video
return (
)
} else if (isValidAudioUrl(href)) {
return
} else if (isYoutubeLink(href)) {
// Youtube
const id = getIdFromYoutubeLink(href)
if (id) {
return (
)
}
}
}
// Link
return (
{href}
)
} else if (nostrMention.test(part)) {
const [encoded] = part.match(nostrEntity) || []
if (!encoded) return part
try {
const decoded = nip19.decode(encoded)
const baseUrl = appRoutes.feed + '/'
switch (decoded.type) {
case 'nprofile':
return