fix: 🐛
This commit is contained in:
@@ -14,7 +14,10 @@ type TFeedContext = {
|
||||
filter: Filter
|
||||
isReady: boolean
|
||||
activeRelaySetId: string | null
|
||||
switchFeed: (feedType: TFeedType, options?: { activeRelaySetId?: string }) => Promise<void>
|
||||
switchFeed: (
|
||||
feedType: TFeedType,
|
||||
options?: { activeRelaySetId?: string; pubkey?: string }
|
||||
) => Promise<void>
|
||||
}
|
||||
|
||||
const FeedContext = createContext<TFeedContext | undefined>(undefined)
|
||||
@@ -52,32 +55,28 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
if (feedType === 'following') {
|
||||
return await switchFeed('following')
|
||||
return await switchFeed('following', { pubkey })
|
||||
} else {
|
||||
await switchFeed('relays', { activeRelaySetId })
|
||||
}
|
||||
await switchFeed('relays', { activeRelaySetId })
|
||||
}
|
||||
|
||||
init()
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isReady || feedType !== 'following') return
|
||||
|
||||
switchFeed('following')
|
||||
}, [pubkey, feedType, isReady])
|
||||
|
||||
useEffect(() => {
|
||||
if (feedType !== 'relays') return
|
||||
|
||||
const relaySet = relaySets.find((set) => set.id === activeRelaySetId)
|
||||
if (!relaySet) return
|
||||
|
||||
setRelayUrls(relaySet.relayUrls)
|
||||
}, [relaySets])
|
||||
if (pubkey && feedType === 'following') {
|
||||
switchFeed('following', { pubkey })
|
||||
}
|
||||
}, [feedType, pubkey])
|
||||
|
||||
const switchFeed = async (
|
||||
feedType: TFeedType,
|
||||
options: { activeRelaySetId?: string | null; temporaryRelayUrls?: string[] | null } = {}
|
||||
options: {
|
||||
activeRelaySetId?: string | null
|
||||
temporaryRelayUrls?: string[] | null
|
||||
pubkey?: string | null
|
||||
} = {}
|
||||
) => {
|
||||
setIsReady(false)
|
||||
if (feedType === 'relays') {
|
||||
@@ -100,14 +99,19 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
|
||||
return setIsReady(true)
|
||||
}
|
||||
if (feedType === 'following') {
|
||||
if (!pubkey) {
|
||||
if (!options.pubkey) {
|
||||
return setIsReady(true)
|
||||
}
|
||||
setFeedType(feedType)
|
||||
setActiveRelaySetId(null)
|
||||
const [relayList, followings] = await Promise.all([getRelayList(), getFollowings()])
|
||||
const [relayList, followings] = await Promise.all([
|
||||
getRelayList(options.pubkey),
|
||||
getFollowings(options.pubkey)
|
||||
])
|
||||
setRelayUrls(relayList.read.concat(BIG_RELAY_URLS).slice(0, 4))
|
||||
setFilter({ authors: followings.includes(pubkey) ? followings : [...followings, pubkey] })
|
||||
setFilter({
|
||||
authors: followings.includes(options.pubkey) ? followings : [...followings, options.pubkey]
|
||||
})
|
||||
storage.setFeedType(feedType)
|
||||
return setIsReady(true)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { TDraftEvent } from '@/types'
|
||||
import { tagNameEquals } from '@/lib/tag'
|
||||
import client from '@/services/client.service'
|
||||
import { TDraftEvent } from '@/types'
|
||||
import dayjs from 'dayjs'
|
||||
import { Event, kinds } from 'nostr-tools'
|
||||
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
|
||||
@@ -25,7 +25,7 @@ export const useFollowList = () => {
|
||||
}
|
||||
|
||||
export function FollowListProvider({ children }: { children: React.ReactNode }) {
|
||||
const { pubkey: accountPubkey, publish, updateFollowings } = useNostr()
|
||||
const { pubkey: accountPubkey, publish, updateFollowListEvent } = useNostr()
|
||||
const [followListEvent, setFollowListEvent] = useState<Event | undefined>(undefined)
|
||||
const [isFetching, setIsFetching] = useState(true)
|
||||
const followings = useMemo(() => {
|
||||
@@ -65,7 +65,7 @@ export function FollowListProvider({ children }: { children: React.ReactNode })
|
||||
}
|
||||
const newFollowListEvent = await publish(newFollowListDraftEvent)
|
||||
client.updateFollowListCache(accountPubkey, newFollowListEvent)
|
||||
updateFollowings([...followings, pubkey])
|
||||
updateFollowListEvent(newFollowListEvent)
|
||||
setFollowListEvent(newFollowListEvent)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ export function FollowListProvider({ children }: { children: React.ReactNode })
|
||||
}
|
||||
const newFollowListEvent = await publish(newFollowListDraftEvent)
|
||||
client.updateFollowListCache(accountPubkey, newFollowListEvent)
|
||||
updateFollowings(followings.filter((followPubkey) => followPubkey !== pubkey))
|
||||
updateFollowListEvent(newFollowListEvent)
|
||||
setFollowListEvent(newFollowListEvent)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import LoginDialog from '@/components/LoginDialog'
|
||||
import { BIG_RELAY_URLS } from '@/constants'
|
||||
import { useToast } from '@/hooks'
|
||||
import {
|
||||
getFollowingsFromFollowListEvent,
|
||||
getProfileFromProfileEvent,
|
||||
getRelayListFromRelayListEvent
|
||||
} from '@/lib/event'
|
||||
import client from '@/services/client.service'
|
||||
import storage from '@/services/storage.service'
|
||||
import { ISigner, TAccount, TAccountPointer, TDraftEvent, TProfile, TRelayList } from '@/types'
|
||||
@@ -30,10 +34,10 @@ type TNostrContext = {
|
||||
signHttpAuth: (url: string, method: string) => Promise<string>
|
||||
signEvent: (draftEvent: TDraftEvent) => Promise<Event>
|
||||
checkLogin: <T>(cb?: () => T) => Promise<T | void>
|
||||
getRelayList: () => Promise<TRelayList>
|
||||
updateRelayList: (relayList: TRelayList) => void
|
||||
getFollowings: () => Promise<string[]>
|
||||
updateFollowings: (followings: string[]) => void
|
||||
getRelayList: (pubkey: string) => Promise<TRelayList>
|
||||
updateRelayListEvent: (relayListEvent: Event) => void
|
||||
getFollowings: (pubkey: string) => Promise<string[]>
|
||||
updateFollowListEvent: (followListEvent: Event) => void
|
||||
}
|
||||
|
||||
const NostrContext = createContext<TNostrContext | undefined>(undefined)
|
||||
@@ -69,34 +73,42 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
useEffect(() => {
|
||||
if (!account) {
|
||||
setRelayList(null)
|
||||
setFollowings(null)
|
||||
setProfile(null)
|
||||
return
|
||||
}
|
||||
|
||||
const storedRelayList = storage.getAccountRelayList(account.pubkey)
|
||||
if (storedRelayList) {
|
||||
setRelayList(storedRelayList)
|
||||
const storedRelayListEvent = storage.getAccountRelayListEvent(account.pubkey)
|
||||
if (storedRelayListEvent) {
|
||||
setRelayList(
|
||||
storedRelayListEvent ? getRelayListFromRelayListEvent(storedRelayListEvent) : null
|
||||
)
|
||||
}
|
||||
const followings = storage.getAccountFollowings(account.pubkey)
|
||||
if (followings) {
|
||||
setFollowings(followings)
|
||||
const followListEvent = storage.getAccountFollowListEvent(account.pubkey)
|
||||
if (followListEvent) {
|
||||
setFollowings(getFollowingsFromFollowListEvent(followListEvent))
|
||||
}
|
||||
const profile = storage.getAccountProfile(account.pubkey)
|
||||
if (profile) {
|
||||
setProfile(profile)
|
||||
const profileEvent = storage.getAccountProfileEvent(account.pubkey)
|
||||
if (profileEvent) {
|
||||
setProfile(getProfileFromProfileEvent(profileEvent))
|
||||
}
|
||||
client.fetchRelayList(account.pubkey).then((relayList) => {
|
||||
setRelayList(relayList)
|
||||
storage.setAccountRelayList(account.pubkey, relayList)
|
||||
client.fetchRelayListEvent(account.pubkey).then((relayListEvent) => {
|
||||
if (!relayListEvent) return
|
||||
const isNew = storage.setAccountRelayListEvent(relayListEvent)
|
||||
if (!isNew) return
|
||||
setRelayList(getRelayListFromRelayListEvent(relayListEvent))
|
||||
})
|
||||
client.fetchFollowings(account.pubkey).then((followings) => {
|
||||
setFollowings(followings)
|
||||
storage.setAccountFollowings(account.pubkey, followings)
|
||||
client.fetchFollowListEvent(account.pubkey).then((followListEvent) => {
|
||||
if (!followListEvent) return
|
||||
const isNew = storage.setAccountFollowListEvent(followListEvent)
|
||||
if (!isNew) return
|
||||
setFollowings(getFollowingsFromFollowListEvent(followListEvent))
|
||||
})
|
||||
client.fetchProfile(account.pubkey).then((profile) => {
|
||||
if (profile) {
|
||||
setProfile(profile)
|
||||
storage.setAccountProfile(account.pubkey, profile)
|
||||
}
|
||||
client.fetchProfileEvent(account.pubkey).then((profileEvent) => {
|
||||
if (!profileEvent) return
|
||||
const isNew = storage.setAccountProfileEvent(profileEvent)
|
||||
if (!isNew) return
|
||||
setProfile(getProfileFromProfileEvent(profileEvent))
|
||||
})
|
||||
}, [account])
|
||||
|
||||
@@ -240,44 +252,32 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
return setOpenLoginDialog(true)
|
||||
}
|
||||
|
||||
const getRelayList = async () => {
|
||||
if (!account) {
|
||||
return { write: BIG_RELAY_URLS, read: BIG_RELAY_URLS }
|
||||
const getRelayList = async (pubkey: string) => {
|
||||
const storedRelayListEvent = storage.getAccountRelayListEvent(pubkey)
|
||||
if (storedRelayListEvent) {
|
||||
return getRelayListFromRelayListEvent(storedRelayListEvent)
|
||||
}
|
||||
|
||||
const storedRelayList = storage.getAccountRelayList(account.pubkey)
|
||||
if (storedRelayList) {
|
||||
return storedRelayList
|
||||
}
|
||||
return await client.fetchRelayList(account.pubkey)
|
||||
return await client.fetchRelayList(pubkey)
|
||||
}
|
||||
|
||||
const updateRelayList = (relayList: TRelayList) => {
|
||||
if (!account) {
|
||||
return
|
||||
}
|
||||
setRelayList(relayList)
|
||||
storage.setAccountRelayList(account.pubkey, relayList)
|
||||
const updateRelayListEvent = (relayListEvent: Event) => {
|
||||
const isNew = storage.setAccountRelayListEvent(relayListEvent)
|
||||
if (!isNew) return
|
||||
setRelayList(getRelayListFromRelayListEvent(relayListEvent))
|
||||
}
|
||||
|
||||
const getFollowings = async () => {
|
||||
if (!account) {
|
||||
return []
|
||||
const getFollowings = async (pubkey: string) => {
|
||||
const followListEvent = storage.getAccountFollowListEvent(pubkey)
|
||||
if (followListEvent) {
|
||||
return getFollowingsFromFollowListEvent(followListEvent)
|
||||
}
|
||||
|
||||
const followings = storage.getAccountFollowings(account.pubkey)
|
||||
if (followings) {
|
||||
return followings
|
||||
}
|
||||
return await client.fetchFollowings(account.pubkey)
|
||||
return await client.fetchFollowings(pubkey)
|
||||
}
|
||||
|
||||
const updateFollowings = (followings: string[]) => {
|
||||
if (!account) {
|
||||
return
|
||||
}
|
||||
setFollowings(followings)
|
||||
storage.setAccountFollowings(account.pubkey, followings)
|
||||
const updateFollowListEvent = (followListEvent: Event) => {
|
||||
const isNew = storage.setAccountFollowListEvent(followListEvent)
|
||||
if (!isNew) return
|
||||
setFollowings(getFollowingsFromFollowListEvent(followListEvent))
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -301,9 +301,9 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
||||
checkLogin,
|
||||
signEvent,
|
||||
getRelayList,
|
||||
updateRelayList,
|
||||
updateRelayListEvent,
|
||||
getFollowings,
|
||||
updateFollowings
|
||||
updateFollowListEvent
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user