9 changed files with 309 additions and 210 deletions
@ -0,0 +1,99 @@ |
|||||||
|
import NoteList, { TNoteListRef } from '@/components/NoteList' |
||||||
|
import Tabs from '@/components/Tabs' |
||||||
|
import { BIG_RELAY_URLS } from '@/constants' |
||||||
|
import { useNostr } from '@/providers/NostrProvider' |
||||||
|
import client from '@/services/client.service' |
||||||
|
import storage from '@/services/local-storage.service' |
||||||
|
import { TFeedSubRequest, TNoteListMode } from '@/types' |
||||||
|
import { useEffect, useMemo, useRef, useState } from 'react' |
||||||
|
|
||||||
|
export default function ProfileFeed({ |
||||||
|
pubkey, |
||||||
|
topSpace = 0 |
||||||
|
}: { |
||||||
|
pubkey: string |
||||||
|
topSpace?: number |
||||||
|
}) { |
||||||
|
const { pubkey: myPubkey } = useNostr() |
||||||
|
const [listMode, setListMode] = useState<TNoteListMode>(() => storage.getNoteListMode()) |
||||||
|
const noteListRef = useRef<TNoteListRef>(null) |
||||||
|
const [subRequests, setSubRequests] = useState<TFeedSubRequest[]>([]) |
||||||
|
const tabs = useMemo(() => { |
||||||
|
const _tabs = [ |
||||||
|
{ value: 'posts', label: 'Notes' }, |
||||||
|
{ value: 'postsAndReplies', label: 'Replies' } |
||||||
|
] |
||||||
|
|
||||||
|
if (myPubkey && myPubkey !== pubkey) { |
||||||
|
_tabs.push({ value: 'you', label: 'YouTabName' }) |
||||||
|
} |
||||||
|
|
||||||
|
return _tabs |
||||||
|
}, [myPubkey, pubkey]) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
const init = async () => { |
||||||
|
if (listMode === 'you') { |
||||||
|
if (!myPubkey) { |
||||||
|
setSubRequests([]) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
const [relayList, myRelayList] = await Promise.all([ |
||||||
|
client.fetchRelayList(pubkey), |
||||||
|
client.fetchRelayList(myPubkey) |
||||||
|
]) |
||||||
|
|
||||||
|
setSubRequests([ |
||||||
|
{ |
||||||
|
urls: myRelayList.write.concat(BIG_RELAY_URLS).slice(0, 5), |
||||||
|
filter: { |
||||||
|
authors: [myPubkey], |
||||||
|
'#p': [pubkey] |
||||||
|
} |
||||||
|
}, |
||||||
|
{ |
||||||
|
urls: relayList.write.concat(BIG_RELAY_URLS).slice(0, 5), |
||||||
|
filter: { |
||||||
|
authors: [pubkey], |
||||||
|
'#p': [myPubkey] |
||||||
|
} |
||||||
|
} |
||||||
|
]) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
const relayList = await client.fetchRelayList(pubkey) |
||||||
|
setSubRequests([ |
||||||
|
{ |
||||||
|
urls: relayList.write.concat(BIG_RELAY_URLS).slice(0, 8), |
||||||
|
filter: { |
||||||
|
authors: [pubkey] |
||||||
|
} |
||||||
|
} |
||||||
|
]) |
||||||
|
} |
||||||
|
init() |
||||||
|
}, [pubkey, listMode]) |
||||||
|
|
||||||
|
const handleListModeChange = (mode: TNoteListMode) => { |
||||||
|
setListMode(mode) |
||||||
|
setTimeout(() => { |
||||||
|
noteListRef.current?.scrollToTop() |
||||||
|
}, 0) |
||||||
|
} |
||||||
|
|
||||||
|
return ( |
||||||
|
<> |
||||||
|
<Tabs |
||||||
|
value={listMode} |
||||||
|
tabs={tabs} |
||||||
|
onTabChange={(listMode) => { |
||||||
|
handleListModeChange(listMode as TNoteListMode) |
||||||
|
}} |
||||||
|
threshold={Math.max(800, topSpace)} |
||||||
|
/> |
||||||
|
<NoteList ref={noteListRef} subRequests={subRequests} hideReplies={listMode === 'posts'} /> |
||||||
|
</> |
||||||
|
) |
||||||
|
} |
||||||
Loading…
Reference in new issue