import { useFetchRelayInfo } from '@/hooks' import { toRelay } from '@/lib/link' import { useSecondaryPage } from '@/PageManager' import { useNostr } from '@/providers/NostrProvider' import client from '@/services/client.service' import { useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import RelaySimpleInfo, { RelaySimpleInfoSkeleton } from '../RelaySimpleInfo' const SHOW_COUNT = 10 export default function FollowingFavoriteRelayList() { const { t } = useTranslation() const { pubkey } = useNostr() const [loading, setLoading] = useState(true) const [relays, setRelays] = useState<[string, string[]][]>([]) const [showCount, setShowCount] = useState(SHOW_COUNT) const bottomRef = useRef(null) useEffect(() => { setLoading(true) const init = async () => { if (!pubkey) return const relays = (await client.fetchFollowingFavoriteRelays(pubkey)) ?? [] setRelays(relays) } init().finally(() => { setLoading(false) }) }, [pubkey]) useEffect(() => { const options = { root: null, rootMargin: '10px', threshold: 1 } const observerInstance = new IntersectionObserver((entries) => { if (entries[0].isIntersecting && showCount < relays.length) { setShowCount((prev) => prev + SHOW_COUNT) } }, options) const currentBottomRef = bottomRef.current if (currentBottomRef) { observerInstance.observe(currentBottomRef) } return () => { if (observerInstance && currentBottomRef) { observerInstance.unobserve(currentBottomRef) } } }, [showCount, relays]) return (
{relays.slice(0, showCount).map(([url, users]) => ( ))} {showCount < relays.length &&
} {loading && } {!loading && (
{relays.length === 0 ? t('no relays found') : t('no more relays')}
)}
) } function RelayItem({ url, users }: { url: string; users: string[] }) { const { push } = useSecondaryPage() const { relayInfo } = useFetchRelayInfo(url) return ( { e.stopPropagation() push(toRelay(url)) }} /> ) }