refactor: 🏗️

This commit is contained in:
codytseng
2025-01-18 22:46:02 +08:00
parent fa455ba127
commit 34ff0cd314
5 changed files with 40 additions and 65 deletions

View File

@@ -1,11 +1,7 @@
import LoginDialog from '@/components/LoginDialog'
import { BIG_RELAY_URLS } from '@/constants'
import { useToast } from '@/hooks'
import {
getFollowingsFromFollowListEvent,
getProfileFromProfileEvent,
getRelayListFromRelayListEvent
} from '@/lib/event'
import { getProfileFromProfileEvent, getRelayListFromRelayListEvent } from '@/lib/event'
import { formatPubkey } from '@/lib/pubkey'
import client from '@/services/client.service'
import storage from '@/services/storage.service'
@@ -15,17 +11,16 @@ import { Event, kinds } from 'nostr-tools'
import * as nip19 from 'nostr-tools/nip19'
import * as nip49 from 'nostr-tools/nip49'
import { createContext, useContext, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { BunkerSigner } from './bunker.signer'
import { Nip07Signer } from './nip-07.signer'
import { NsecSigner } from './nsec.signer'
import { useTranslation } from 'react-i18next'
type TNostrContext = {
pubkey: string | null
profile: TProfile | null
profileEvent: Event | null
relayList: TRelayList | null
followings: string[] | null
account: TAccountPointer | null
accounts: TAccountPointer[]
nsec: string | null
@@ -45,8 +40,6 @@ type TNostrContext = {
checkLogin: <T>(cb?: () => T) => Promise<T | void>
getRelayList: (pubkey: string) => Promise<TRelayList>
updateRelayListEvent: (relayListEvent: Event) => void
getFollowings: (pubkey: string) => Promise<string[]>
updateFollowListEvent: (followListEvent: Event) => void
updateProfileEvent: (profileEvent: Event) => void
}
@@ -71,7 +64,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
const [profile, setProfile] = useState<TProfile | null>(null)
const [profileEvent, setProfileEvent] = useState<Event | null>(null)
const [relayList, setRelayList] = useState<TRelayList | null>(null)
const [followings, setFollowings] = useState<string[] | null>(null)
useEffect(() => {
const init = async () => {
@@ -86,7 +78,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
setRelayList(null)
setFollowings(null)
setProfile(null)
setProfileEvent(null)
setNsec(null)
@@ -112,10 +103,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
storedRelayListEvent ? getRelayListFromRelayListEvent(storedRelayListEvent) : null
)
}
const storedFollowListEvent = storage.getAccountFollowListEvent(account.pubkey)
if (storedFollowListEvent) {
setFollowings(getFollowingsFromFollowListEvent(storedFollowListEvent))
}
const storedProfileEvent = storage.getAccountProfileEvent(account.pubkey)
if (storedProfileEvent) {
setProfileEvent(storedProfileEvent)
@@ -132,17 +119,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
if (!isNew) return
setRelayList(getRelayListFromRelayListEvent(relayListEvent))
})
client.fetchFollowListEvent(account.pubkey).then(async (followListEvent) => {
if (!followListEvent) {
if (storedFollowListEvent) return
setFollowings([])
return
}
const isNew = storage.setAccountFollowListEvent(followListEvent)
if (!isNew) return
setFollowings(getFollowingsFromFollowListEvent(followListEvent))
})
client.fetchProfileEvent(account.pubkey).then(async (profileEvent) => {
if (!profileEvent) {
if (storedProfileEvent) return
@@ -344,20 +320,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
setRelayList(getRelayListFromRelayListEvent(relayListEvent))
}
const getFollowings = async (pubkey: string) => {
const followListEvent = storage.getAccountFollowListEvent(pubkey)
if (followListEvent) {
return getFollowingsFromFollowListEvent(followListEvent)
}
return await client.fetchFollowings(pubkey)
}
const updateFollowListEvent = (followListEvent: Event) => {
const isNew = storage.setAccountFollowListEvent(followListEvent)
if (!isNew) return
setFollowings(getFollowingsFromFollowListEvent(followListEvent))
}
const updateProfileEvent = (profileEvent: Event) => {
const isNew = storage.setAccountProfileEvent(profileEvent)
if (!isNew) return
@@ -373,7 +335,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
profile,
profileEvent,
relayList,
followings,
account,
accounts: storage
.getAccounts()
@@ -392,8 +353,6 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
signEvent,
getRelayList,
updateRelayListEvent,
getFollowings,
updateFollowListEvent,
updateProfileEvent
}}
>