import { useFetchProfile } from '@/hooks'
import { toNostrBuildThumbUrl } from '@/lib/nostr-build'
import { isVideo } from '@/lib/url'
import { cn } from '@/lib/utils'
import { Calendar } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
function profileAvatarThumbUrl(avatar: string | undefined): string {
const a = avatar?.trim()
if (!a || !/^https?:\/\//i.test(a)) return ''
if (isVideo(a)) return a
return toNostrBuildThumbUrl(a)
}
/**
* NIP-52 calendar card cover: event `image` tag, else author profile picture, else calendar icon.
*/
export function CalendarEventCoverImage({
coverUrl,
pubkey,
className,
iconClassName
}: {
coverUrl: string
pubkey: string
className?: string
/** Passed to the Lucide {@link Calendar} icon when event image and profile avatar are unavailable. */
iconClassName?: string
}) {
const trimmedCover = coverUrl?.trim() ?? ''
const { profile } = useFetchProfile(pubkey)
const profileThumb = useMemo(() => profileAvatarThumbUrl(profile?.avatar), [profile?.avatar])
const [profileImgFailed, setProfileImgFailed] = useState(false)
useEffect(() => {
setProfileImgFailed(false)
}, [profileThumb, trimmedCover, pubkey])
if (trimmedCover) {
return (
)
}
if (profileThumb && !profileImgFailed) {
return (
setProfileImgFailed(true)}
/>
)
}
return (