import FollowButton from '@/components/FollowButton'
import Nip05 from '@/components/Nip05'
import NoteList from '@/components/NoteList'
import ProfileAbout from '@/components/ProfileAbout'
import ProfileBanner from '@/components/ProfileBanner'
import PubkeyCopy from '@/components/PubkeyCopy'
import QrCodePopover from '@/components/QrCodePopover'
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
import { useFetchFollowings, useFetchProfile } from '@/hooks'
import { useFetchRelayList } from '@/hooks/useFetchRelayList'
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
import {
toFollowingList,
toOthersRelaySettings,
toProfileEditor,
toRelaySettings
} from '@/lib/link'
import { generateImageByPubkey } from '@/lib/pubkey'
import { SecondaryPageLink, useSecondaryPage } from '@/PageManager'
import { useFeed } from '@/providers/FeedProvider'
import { useFollowList } from '@/providers/FollowListProvider'
import { useNostr } from '@/providers/NostrProvider'
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import NotFoundPage from '../NotFoundPage'
export default function ProfilePage({ id, index }: { id?: string; index?: number }) {
const { t } = useTranslation()
const { push } = useSecondaryPage()
const { profile, isFetching } = useFetchProfile(id)
const { relayList, isFetching: isFetchingRelayInfo } = useFetchRelayList(profile?.pubkey)
const { relayUrls: currentRelayUrls } = useFeed()
const relayUrls = useMemo(
() =>
relayList.write.length < 4
? relayList.write.concat(currentRelayUrls).slice(0, 4)
: relayList.write.slice(0, 4),
[relayList, currentRelayUrls]
)
const { pubkey: accountPubkey } = useNostr()
const { followings: selfFollowings } = useFollowList()
const { followings } = useFetchFollowings(profile?.pubkey)
const isFollowingYou = useMemo(() => {
return (
!!accountPubkey && accountPubkey !== profile?.pubkey && followings.includes(accountPubkey)
)
}, [followings, profile, accountPubkey])
const defaultImage = useMemo(
() => (profile?.pubkey ? generateImageByPubkey(profile?.pubkey) : ''),
[profile]
)
const relayCount = useMemo(
() => new Set(relayList.write.concat(relayList.read)).size,
[relayList]
)
const isSelf = accountPubkey === profile?.pubkey
if (!profile && isFetching) {
return (
)
}
if (!profile) return
const { banner, username, nip05, about, avatar, pubkey } = profile
return (
{isFollowingYou && (
{t('Follows you')}
)}
{isSelf ? (
) : (
)}
{username}
{nip05 &&
}
{isSelf ? selfFollowings.length : followings.length}
{t('Following')}
{relayCount}
{t('Relays')}
{!isFetchingRelayInfo && (
)}
)
}