feat: add mailbox configuration

This commit is contained in:
codytseng
2025-01-13 16:53:07 +08:00
parent 9de3d4ed5b
commit 0f8a5403cd
20 changed files with 525 additions and 62 deletions

View File

@@ -1,12 +1,11 @@
import { BIG_RELAY_URLS } from '@/constants'
import { isWebsocketUrl, normalizeUrl } from '@/lib/url'
import client from '@/services/client.service'
import storage from '@/services/storage.service'
import { TFeedType } from '@/types'
import { Filter } from 'nostr-tools'
import { createContext, useContext, useEffect, useState } from 'react'
import { useNostr } from './NostrProvider'
import { useRelaySets } from './RelaySetsProvider'
import { BIG_RELAY_URLS } from '@/constants'
type TFeedContext = {
feedType: TFeedType
@@ -29,7 +28,7 @@ export const useFeed = () => {
}
export function FeedProvider({ children }: { children: React.ReactNode }) {
const { pubkey } = useNostr()
const { pubkey, getRelayList, getFollowings } = useNostr()
const { relaySets } = useRelaySets()
const [feedType, setFeedType] = useState<TFeedType>(storage.getFeedType())
const [relayUrls, setRelayUrls] = useState<string[]>([])
@@ -46,11 +45,8 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
const searchParams = new URLSearchParams(window.location.search)
const temporaryRelayUrls = searchParams
.getAll('r')
.map((url) =>
!url.startsWith('ws://') && !url.startsWith('wss://') ? `wss://${url}` : url
)
.filter((url) => isWebsocketUrl(url))
.map((url) => normalizeUrl(url))
.filter((url) => isWebsocketUrl(url))
if (temporaryRelayUrls.length) {
return await switchFeed('temporary', { temporaryRelayUrls })
}
@@ -65,10 +61,10 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
}, [])
useEffect(() => {
if (feedType !== 'following') return
if (!isReady || feedType !== 'following') return
switchFeed('following')
}, [pubkey])
}, [pubkey, feedType, isReady])
useEffect(() => {
if (feedType !== 'relays') return
@@ -86,7 +82,9 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
setIsReady(false)
if (feedType === 'relays') {
const relaySetId = options.activeRelaySetId ?? (relaySets.length > 0 ? relaySets[0].id : null)
if (!relaySetId) return
if (!relaySetId) {
return setIsReady(true)
}
const relaySet =
relaySets.find((set) => set.id === options.activeRelaySetId) ??
@@ -96,37 +94,35 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
setRelayUrls(relaySet.relayUrls)
setActiveRelaySetId(relaySet.id)
setFilter({})
setIsReady(true)
storage.setActiveRelaySetId(relaySet.id)
storage.setFeedType(feedType)
}
return
return setIsReady(true)
}
if (feedType === 'following') {
if (!pubkey) return
if (!pubkey) {
return setIsReady(true)
}
setFeedType(feedType)
setActiveRelaySetId(null)
const [relayList, followings] = await Promise.all([
client.fetchRelayList(pubkey),
client.fetchFollowings(pubkey)
])
const [relayList, followings] = await Promise.all([getRelayList(), getFollowings()])
setRelayUrls(relayList.read.concat(BIG_RELAY_URLS).slice(0, 4))
setFilter({ authors: followings.includes(pubkey) ? followings : [...followings, pubkey] })
setIsReady(true)
storage.setFeedType(feedType)
return
return setIsReady(true)
}
if (feedType === 'temporary') {
const urls = options.temporaryRelayUrls ?? temporaryRelayUrls
if (!urls.length) return
if (!urls.length) {
return setIsReady(true)
}
setFeedType(feedType)
setTemporaryRelayUrls(urls)
setRelayUrls(urls)
setActiveRelaySetId(null)
setFilter({})
setIsReady(true)
return
return setIsReady(true)
}
setIsReady(true)
}

View File

@@ -25,7 +25,7 @@ export const useFollowList = () => {
}
export function FollowListProvider({ children }: { children: React.ReactNode }) {
const { pubkey: accountPubkey, publish } = useNostr()
const { pubkey: accountPubkey, publish, updateFollowings } = useNostr()
const [followListEvent, setFollowListEvent] = useState<Event | undefined>(undefined)
const [isFetching, setIsFetching] = useState(true)
const followings = useMemo(() => {
@@ -65,6 +65,7 @@ export function FollowListProvider({ children }: { children: React.ReactNode })
}
const newFollowListEvent = await publish(newFollowListDraftEvent)
client.updateFollowListCache(accountPubkey, newFollowListEvent)
updateFollowings([...followings, pubkey])
setFollowListEvent(newFollowListEvent)
}
@@ -81,6 +82,7 @@ export function FollowListProvider({ children }: { children: React.ReactNode })
}
const newFollowListEvent = await publish(newFollowListDraftEvent)
client.updateFollowListCache(accountPubkey, newFollowListEvent)
updateFollowings(followings.filter((followPubkey) => followPubkey !== pubkey))
setFollowListEvent(newFollowListEvent)
}

View File

@@ -1,9 +1,9 @@
import LoginDialog from '@/components/LoginDialog'
import { useFetchFollowings, useToast } from '@/hooks'
import { useFetchRelayList } from '@/hooks/useFetchRelayList'
import { BIG_RELAY_URLS } from '@/constants'
import { useToast } from '@/hooks'
import client from '@/services/client.service'
import storage from '@/services/storage.service'
import { ISigner, TAccount, TAccountPointer, TDraftEvent, TRelayList } from '@/types'
import { ISigner, TAccount, TAccountPointer, TDraftEvent, TProfile, TRelayList } from '@/types'
import dayjs from 'dayjs'
import { Event, kinds } from 'nostr-tools'
import { createContext, useContext, useEffect, useState } from 'react'
@@ -13,6 +13,7 @@ import { NsecSigner } from './nsec.signer'
type TNostrContext = {
pubkey: string | null
profile: TProfile | null
relayList: TRelayList | null
followings: string[] | null
account: TAccountPointer | null
@@ -29,6 +30,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
}
const NostrContext = createContext<TNostrContext | undefined>(undefined)
@@ -46,8 +51,9 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
const [account, setAccount] = useState<TAccountPointer | null>(null)
const [signer, setSigner] = useState<ISigner | null>(null)
const [openLoginDialog, setOpenLoginDialog] = useState(false)
const { relayList, isFetching: isFetchingRelayList } = useFetchRelayList(account?.pubkey)
const { followings, isFetching: isFetchingFollowings } = useFetchFollowings(account?.pubkey)
const [profile, setProfile] = useState<TProfile | null>(null)
const [relayList, setRelayList] = useState<TRelayList | null>(null)
const [followings, setFollowings] = useState<string[] | null>(null)
useEffect(() => {
const init = async () => {
@@ -60,6 +66,40 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
init()
}, [])
useEffect(() => {
if (!account) {
setRelayList(null)
return
}
const storedRelayList = storage.getAccountRelayList(account.pubkey)
if (storedRelayList) {
setRelayList(storedRelayList)
}
const followings = storage.getAccountFollowings(account.pubkey)
if (followings) {
setFollowings(followings)
}
const profile = storage.getAccountProfile(account.pubkey)
if (profile) {
setProfile(profile)
}
client.fetchRelayList(account.pubkey).then((relayList) => {
setRelayList(relayList)
storage.setAccountRelayList(account.pubkey, relayList)
})
client.fetchFollowings(account.pubkey).then((followings) => {
setFollowings(followings)
storage.setAccountFollowings(account.pubkey, followings)
})
client.fetchProfile(account.pubkey).then((profile) => {
if (profile) {
setProfile(profile)
storage.setAccountProfile(account.pubkey, profile)
}
})
}, [account])
const login = (signer: ISigner, act: TAccount) => {
storage.addAccount(act)
storage.switchAccount(act)
@@ -176,7 +216,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
const publish = async (draftEvent: TDraftEvent, additionalRelayUrls: string[] = []) => {
const event = await signEvent(draftEvent)
await client.publishEvent(relayList.write.concat(additionalRelayUrls), event)
await client.publishEvent((relayList?.write ?? []).concat(additionalRelayUrls), event)
return event
}
@@ -200,12 +240,53 @@ 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 storedRelayList = storage.getAccountRelayList(account.pubkey)
if (storedRelayList) {
return storedRelayList
}
return await client.fetchRelayList(account.pubkey)
}
const updateRelayList = (relayList: TRelayList) => {
if (!account) {
return
}
setRelayList(relayList)
storage.setAccountRelayList(account.pubkey, relayList)
}
const getFollowings = async () => {
if (!account) {
return []
}
const followings = storage.getAccountFollowings(account.pubkey)
if (followings) {
return followings
}
return await client.fetchFollowings(account.pubkey)
}
const updateFollowings = (followings: string[]) => {
if (!account) {
return
}
setFollowings(followings)
storage.setAccountFollowings(account.pubkey, followings)
}
return (
<NostrContext.Provider
value={{
pubkey: account?.pubkey ?? null,
relayList: isFetchingRelayList ? null : relayList,
followings: isFetchingFollowings ? null : followings,
profile,
relayList,
followings,
account,
accounts: storage
.getAccounts()
@@ -218,7 +299,11 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
publish,
signHttpAuth,
checkLogin,
signEvent
signEvent,
getRelayList,
updateRelayList,
getFollowings,
updateFollowings
}}
>
{children}