refactor: 💨
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { checkIfAlgoRelay } from '@renderer/lib/relay'
|
||||
import { checkAlgoRelay } from '@renderer/lib/relay'
|
||||
import client from '@renderer/services/client.service'
|
||||
import { TRelayInfo } from '@renderer/types'
|
||||
import { useEffect, useState } from 'react'
|
||||
@@ -17,7 +17,7 @@ export function useFetchRelayInfos(urls: string[]) {
|
||||
try {
|
||||
const relayInfos = await client.fetchRelayInfos(urls)
|
||||
setRelayInfos(relayInfos)
|
||||
setAreAlgoRelays(relayInfos.every((relayInfo) => checkIfAlgoRelay(relayInfo)))
|
||||
setAreAlgoRelays(relayInfos.every((relayInfo) => checkAlgoRelay(relayInfo)))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
} finally {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { TRelayInfo } from '@renderer/types'
|
||||
|
||||
export function checkIfAlgoRelay(relayInfo: TRelayInfo | undefined) {
|
||||
export function checkAlgoRelay(relayInfo: TRelayInfo | undefined) {
|
||||
return relayInfo?.software === 'https://github.com/bitvora/algo-relay' // hardcode for now
|
||||
}
|
||||
|
||||
export function checkSearchRelay(relayInfo: TRelayInfo | undefined) {
|
||||
return relayInfo?.supported_nips?.includes(50)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { TRelayGroup } from '@common/types'
|
||||
import { checkIfAlgoRelay } from '@renderer/lib/relay'
|
||||
import { checkAlgoRelay, checkSearchRelay } from '@renderer/lib/relay'
|
||||
import { isWebsocketUrl, normalizeUrl } from '@renderer/lib/url'
|
||||
import client from '@renderer/services/client.service'
|
||||
import storage from '@renderer/services/storage.service'
|
||||
@@ -59,20 +59,18 @@ export function RelaySettingsProvider({ children }: { children: React.ReactNode
|
||||
|
||||
useEffect(() => {
|
||||
const handler = async () => {
|
||||
const relayUrls = temporaryRelayUrls.length
|
||||
const newRelayUrls = temporaryRelayUrls.length
|
||||
? temporaryRelayUrls
|
||||
: (relayGroups.find((group) => group.isActive)?.relayUrls ?? [])
|
||||
|
||||
setSearchableRelayUrls([])
|
||||
setRelayUrls([])
|
||||
const relayInfos = await client.fetchRelayInfos(relayUrls)
|
||||
setSearchableRelayUrls(
|
||||
relayUrls.filter((_, index) => relayInfos[index]?.supported_nips?.includes(50))
|
||||
)
|
||||
const nonAlgoRelayUrls = relayUrls.filter((_, index) => !checkIfAlgoRelay(relayInfos[index]))
|
||||
setAreAlgoRelays(relayUrls.length > 0 && nonAlgoRelayUrls.length === 0)
|
||||
setRelayUrls(relayUrls)
|
||||
const relayInfos = await client.fetchRelayInfos(newRelayUrls)
|
||||
setSearchableRelayUrls(newRelayUrls.filter((_, index) => checkSearchRelay(relayInfos[index])))
|
||||
const nonAlgoRelayUrls = newRelayUrls.filter((_, index) => !checkAlgoRelay(relayInfos[index]))
|
||||
setAreAlgoRelays(newRelayUrls.length > 0 && nonAlgoRelayUrls.length === 0)
|
||||
client.setCurrentRelayUrls(nonAlgoRelayUrls)
|
||||
if (JSON.stringify(relayUrls) !== JSON.stringify(newRelayUrls)) {
|
||||
setRelayUrls(newRelayUrls)
|
||||
}
|
||||
}
|
||||
handler()
|
||||
}, [relayGroups, temporaryRelayUrls])
|
||||
|
||||
Reference in New Issue
Block a user