refactor: note list component
This commit is contained in:
28
src/pages/primary/NoteListPage/FollowingFeed.tsx
Normal file
28
src/pages/primary/NoteListPage/FollowingFeed.tsx
Normal file
@@ -0,0 +1,28 @@
|
||||
import NormalFeed from '@/components/NormalFeed'
|
||||
import { useFeed } from '@/providers/FeedProvider'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import client from '@/services/client.service'
|
||||
import { TNormalFeedSubRequest } from '@/types'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function FollowingFeed() {
|
||||
const { pubkey } = useNostr()
|
||||
const { feedInfo } = useFeed()
|
||||
const [subRequests, setSubRequests] = useState<TNormalFeedSubRequest[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
async function init() {
|
||||
if (feedInfo.feedType !== 'following' || !pubkey) {
|
||||
setSubRequests([])
|
||||
return
|
||||
}
|
||||
|
||||
const followings = await client.fetchFollowings(pubkey)
|
||||
setSubRequests(await client.generateSubRequestsForPubkeys(followings, pubkey))
|
||||
}
|
||||
|
||||
init()
|
||||
}, [feedInfo.feedType, pubkey])
|
||||
|
||||
return <NormalFeed subRequests={subRequests} />
|
||||
}
|
||||
36
src/pages/primary/NoteListPage/RelaysFeed.tsx
Normal file
36
src/pages/primary/NoteListPage/RelaysFeed.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import NormalFeed from '@/components/NormalFeed'
|
||||
import { checkAlgoRelay } from '@/lib/relay'
|
||||
import { useFeed } from '@/providers/FeedProvider'
|
||||
import relayInfoService from '@/services/relay-info.service'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export default function RelaysFeed() {
|
||||
const { feedInfo, relayUrls } = useFeed()
|
||||
const [isReady, setIsReady] = useState(false)
|
||||
const [areAlgoRelays, setAreAlgoRelays] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
const relayInfos = await relayInfoService.getRelayInfos(relayUrls)
|
||||
setAreAlgoRelays(relayInfos.every((relayInfo) => checkAlgoRelay(relayInfo)))
|
||||
setIsReady(true)
|
||||
}
|
||||
init()
|
||||
}, [relayUrls])
|
||||
|
||||
if (!isReady) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (
|
||||
feedInfo.feedType !== 'relay' &&
|
||||
feedInfo.feedType !== 'relays' &&
|
||||
feedInfo.feedType !== 'temporary'
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<NormalFeed subRequests={[{ urls: relayUrls, filter: {} }]} areAlgoRelays={areAlgoRelays} />
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import BookmarkList from '@/components/BookmarkList'
|
||||
import NoteList from '@/components/NoteList'
|
||||
import PostEditor from '@/components/PostEditor'
|
||||
import SaveRelayDropdownMenu from '@/components/SaveRelayDropdownMenu'
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -12,13 +11,15 @@ import { PencilLine } from 'lucide-react'
|
||||
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import FeedButton from './FeedButton'
|
||||
import FollowingFeed from './FollowingFeed'
|
||||
import RelaysFeed from './RelaysFeed'
|
||||
import SearchButton from './SearchButton'
|
||||
|
||||
const NoteListPage = forwardRef((_, ref) => {
|
||||
const { t } = useTranslation()
|
||||
const layoutRef = useRef<TPageRef>(null)
|
||||
const { pubkey, checkLogin } = useNostr()
|
||||
const { feedInfo, relayUrls, isReady, filter } = useFeed()
|
||||
const { feedInfo, relayUrls, isReady } = useFeed()
|
||||
useImperativeHandle(ref, () => layoutRef.current)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -27,8 +28,10 @@ const NoteListPage = forwardRef((_, ref) => {
|
||||
}
|
||||
}, [JSON.stringify(relayUrls), feedInfo])
|
||||
|
||||
let content = <div className="text-center text-sm text-muted-foreground">{t('loading...')}</div>
|
||||
if (feedInfo.feedType === 'following' && !pubkey) {
|
||||
let content: React.ReactNode = null
|
||||
if (!isReady) {
|
||||
content = <div className="text-center text-sm text-muted-foreground">{t('loading...')}</div>
|
||||
} else if (feedInfo.feedType === 'following' && !pubkey) {
|
||||
content = (
|
||||
<div className="flex justify-center w-full">
|
||||
<Button size="lg" onClick={() => checkLogin()}>
|
||||
@@ -48,16 +51,10 @@ const NoteListPage = forwardRef((_, ref) => {
|
||||
} else {
|
||||
content = <BookmarkList />
|
||||
}
|
||||
} else if (isReady) {
|
||||
content = (
|
||||
<NoteList
|
||||
relayUrls={relayUrls}
|
||||
filter={filter}
|
||||
needCheckAlgoRelay={feedInfo.feedType !== 'following'}
|
||||
isMainFeed
|
||||
skipTrustCheck={feedInfo.feedType === 'following'}
|
||||
/>
|
||||
)
|
||||
} else if (feedInfo.feedType === 'following') {
|
||||
content = <FollowingFeed />
|
||||
} else {
|
||||
content = <RelaysFeed />
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Favicon } from '@/components/Favicon'
|
||||
import NoteList from '@/components/NoteList'
|
||||
import NormalFeed from '@/components/NormalFeed'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { BIG_RELAY_URLS, SEARCHABLE_RELAY_URLS } from '@/constants'
|
||||
import SecondaryPageLayout from '@/layouts/SecondaryPageLayout'
|
||||
@@ -7,63 +7,67 @@ import { toProfileList } from '@/lib/link'
|
||||
import { fetchPubkeysFromDomain, getWellKnownNip05Url } from '@/lib/nip05'
|
||||
import { useSecondaryPage } from '@/PageManager'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import client from '@/services/client.service'
|
||||
import { TNormalFeedSubRequest } from '@/types'
|
||||
import { UserRound } from 'lucide-react'
|
||||
import { Filter } from 'nostr-tools'
|
||||
import React, { forwardRef, useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
const NoteListPage = forwardRef(({ index }: { index?: number }, ref) => {
|
||||
const { t } = useTranslation()
|
||||
const { push } = useSecondaryPage()
|
||||
const { relayList } = useNostr()
|
||||
const { relayList, pubkey } = useNostr()
|
||||
const [title, setTitle] = useState<React.ReactNode>(null)
|
||||
const [controls, setControls] = useState<React.ReactNode>(null)
|
||||
const [data, setData] = useState<
|
||||
| {
|
||||
type: 'hashtag' | 'search' | 'externalContent'
|
||||
filter: Filter
|
||||
urls: string[]
|
||||
}
|
||||
| {
|
||||
type: 'domain'
|
||||
filter: Filter
|
||||
domain: string
|
||||
urls?: string[]
|
||||
}
|
||||
| null
|
||||
>(null)
|
||||
const [subRequests, setSubRequests] = useState<TNormalFeedSubRequest[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
const searchParams = new URLSearchParams(window.location.search)
|
||||
const hashtag = searchParams.get('t')
|
||||
if (hashtag) {
|
||||
setData({
|
||||
type: 'hashtag',
|
||||
filter: { '#t': [hashtag] },
|
||||
urls: BIG_RELAY_URLS
|
||||
})
|
||||
setData({ type: 'hashtag' })
|
||||
setTitle(`# ${hashtag}`)
|
||||
setSubRequests([
|
||||
{
|
||||
filter: { '#t': [hashtag] },
|
||||
urls: BIG_RELAY_URLS
|
||||
}
|
||||
])
|
||||
return
|
||||
}
|
||||
const search = searchParams.get('s')
|
||||
if (search) {
|
||||
setData({
|
||||
type: 'search',
|
||||
filter: { search },
|
||||
urls: SEARCHABLE_RELAY_URLS
|
||||
})
|
||||
setData({ type: 'search' })
|
||||
setTitle(`${t('Search')}: ${search}`)
|
||||
setSubRequests([
|
||||
{
|
||||
filter: { search },
|
||||
urls: SEARCHABLE_RELAY_URLS
|
||||
}
|
||||
])
|
||||
return
|
||||
}
|
||||
const externalContentId = searchParams.get('i')
|
||||
if (externalContentId) {
|
||||
setData({
|
||||
type: 'externalContent',
|
||||
filter: { '#I': [externalContentId] },
|
||||
urls: BIG_RELAY_URLS.concat(relayList?.write || [])
|
||||
})
|
||||
setData({ type: 'externalContent' })
|
||||
setTitle(externalContentId)
|
||||
setSubRequests([
|
||||
{
|
||||
filter: { '#I': [externalContentId] },
|
||||
urls: BIG_RELAY_URLS.concat(relayList?.write || [])
|
||||
}
|
||||
])
|
||||
return
|
||||
}
|
||||
const domain = searchParams.get('d')
|
||||
@@ -75,13 +79,12 @@ const NoteListPage = forwardRef(({ index }: { index?: number }, ref) => {
|
||||
</div>
|
||||
)
|
||||
const pubkeys = await fetchPubkeysFromDomain(domain)
|
||||
console.log(domain, pubkeys)
|
||||
setData({
|
||||
type: 'domain',
|
||||
domain,
|
||||
filter: { authors: pubkeys }
|
||||
domain
|
||||
})
|
||||
if (pubkeys.length) {
|
||||
setSubRequests(await client.generateSubRequestsForPubkeys(pubkeys, pubkey))
|
||||
setControls(
|
||||
<Button
|
||||
variant="ghost"
|
||||
@@ -99,7 +102,7 @@ const NoteListPage = forwardRef(({ index }: { index?: number }, ref) => {
|
||||
}, [])
|
||||
|
||||
let content: React.ReactNode = null
|
||||
if (data?.type === 'domain' && data.filter?.authors?.length === 0) {
|
||||
if (data?.type === 'domain' && setSubRequests.length === 0) {
|
||||
content = (
|
||||
<div className="text-center w-full py-10">
|
||||
<span className="text-muted-foreground">
|
||||
@@ -108,7 +111,7 @@ const NoteListPage = forwardRef(({ index }: { index?: number }, ref) => {
|
||||
</div>
|
||||
)
|
||||
} else if (data) {
|
||||
content = <NoteList filter={data.filter} relayUrls={data.urls} />
|
||||
content = <NormalFeed subRequests={subRequests} />
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Collapsible from '@/components/Collapsible'
|
||||
import FollowButton from '@/components/FollowButton'
|
||||
import Nip05 from '@/components/Nip05'
|
||||
import NoteList from '@/components/NoteList'
|
||||
import Feed from '@/components/Feed'
|
||||
import ProfileAbout from '@/components/ProfileAbout'
|
||||
import ProfileBanner from '@/components/ProfileBanner'
|
||||
import ProfileOptions from '@/components/ProfileOptions'
|
||||
@@ -193,7 +193,7 @@ const ProfilePage = forwardRef(({ id, index }: { id?: string; index?: number },
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<NoteList
|
||||
<Feed
|
||||
author={pubkey}
|
||||
className="mt-2"
|
||||
filterMutedNotes={false}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import NoteList from '@/components/NoteList'
|
||||
import NormalFeed from '@/components/NormalFeed'
|
||||
import RelayInfo from '@/components/RelayInfo'
|
||||
import SaveRelayDropdownMenu from '@/components/SaveRelayDropdownMenu'
|
||||
import SearchInput from '@/components/SearchInput'
|
||||
@@ -52,10 +52,10 @@ const RelayPage = forwardRef(({ url, index }: { url?: string; index?: number },
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<NoteList
|
||||
relayUrls={[normalizedUrl]}
|
||||
needCheckAlgoRelay
|
||||
filter={debouncedInput ? { search: debouncedInput } : {}}
|
||||
<NormalFeed
|
||||
subRequests={[
|
||||
{ urls: [normalizedUrl], filter: debouncedInput ? { search: debouncedInput } : {} }
|
||||
]}
|
||||
/>
|
||||
</SecondaryPageLayout>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user