Browse Source

fix: 🐛

imwald
codytseng 12 months ago
parent
commit
b576afb971
  1. 3
      src/components/MailboxSetting/index.tsx
  2. 3
      src/components/RelaySetsSetting/RelayUrl.tsx
  3. 2
      src/components/SaveRelayDropdownMenu/index.tsx
  4. 1
      src/lib/event.ts
  5. 23
      src/lib/url.ts
  6. 2
      src/providers/FeedProvider.tsx

3
src/components/MailboxSetting/index.tsx

@ -47,6 +47,9 @@ export default function MailboxSetting() {
const saveNewMailboxRelay = (url: string) => { const saveNewMailboxRelay = (url: string) => {
if (url === '') return null if (url === '') return null
const normalizedUrl = normalizeUrl(url) const normalizedUrl = normalizeUrl(url)
if (!normalizedUrl) {
return t('Invalid relay URL')
}
if (relays.some((r) => r.url === normalizedUrl)) { if (relays.some((r) => r.url === normalizedUrl)) {
return t('Relay already exists') return t('Relay already exists')
} }

3
src/components/RelaySetsSetting/RelayUrl.tsx

@ -29,6 +29,9 @@ export default function RelayUrls({ relaySetId }: { relaySetId: string }) {
const saveNewRelayUrl = () => { const saveNewRelayUrl = () => {
if (newRelayUrl === '') return if (newRelayUrl === '') return
const normalizedUrl = normalizeUrl(newRelayUrl) const normalizedUrl = normalizeUrl(newRelayUrl)
if (!normalizedUrl) {
return setNewRelayUrlError(t('Invalid relay URL'))
}
if (relaySet.relayUrls.includes(normalizedUrl)) { if (relaySet.relayUrls.includes(normalizedUrl)) {
return setNewRelayUrlError(t('Relay already exists')) return setNewRelayUrlError(t('Relay already exists'))
} }

2
src/components/SaveRelayDropdownMenu/index.tsx

@ -23,7 +23,7 @@ export default function SaveRelayDropdownMenu({
}) { }) {
const { t } = useTranslation() const { t } = useTranslation()
const { relaySets } = useRelaySets() const { relaySets } = useRelaySets()
const normalizedUrls = useMemo(() => urls.map((url) => normalizeUrl(url)), [urls]) const normalizedUrls = useMemo(() => urls.map((url) => normalizeUrl(url)).filter(Boolean), [urls])
const alreadySaved = useMemo( const alreadySaved = useMemo(
() => relaySets.some((set) => normalizedUrls.every((url) => set.relayUrls.includes(url))), () => relaySets.some((set) => normalizedUrls.every((url) => set.relayUrls.includes(url))),
[relaySets, normalizedUrls] [relaySets, normalizedUrls]

1
src/lib/event.ts

@ -142,6 +142,7 @@ export function getRelayListFromRelayListEvent(event?: Event) {
if (!url || !isWebsocketUrl(url)) return if (!url || !isWebsocketUrl(url)) return
const normalizedUrl = normalizeUrl(url) const normalizedUrl = normalizeUrl(url)
if (!normalizedUrl) return
switch (type) { switch (type) {
case 'write': case 'write':
relayList.write.push(normalizedUrl) relayList.write.push(normalizedUrl)

23
src/lib/url.ts

@ -4,15 +4,20 @@ export function isWebsocketUrl(url: string): boolean {
// copy from nostr-tools/utils // copy from nostr-tools/utils
export function normalizeUrl(url: string): string { export function normalizeUrl(url: string): string {
if (url.indexOf('://') === -1) url = 'wss://' + url try {
const p = new URL(url) if (url.indexOf('://') === -1) url = 'wss://' + url
p.pathname = p.pathname.replace(/\/+/g, '/') const p = new URL(url)
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1) p.pathname = p.pathname.replace(/\/+/g, '/')
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
p.port = '' if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:'))
p.searchParams.sort() p.port = ''
p.hash = '' p.searchParams.sort()
return p.toString() p.hash = ''
return p.toString()
} catch {
console.error('Invalid URL:', url)
return ''
}
} }
export function normalizeHttpUrl(url: string): string { export function normalizeHttpUrl(url: string): string {

2
src/providers/FeedProvider.tsx

@ -56,7 +56,7 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
const temporaryRelayUrls = searchParams const temporaryRelayUrls = searchParams
.getAll('r') .getAll('r')
.map((url) => normalizeUrl(url)) .map((url) => normalizeUrl(url))
.filter((url) => isWebsocketUrl(url)) .filter((url) => url && isWebsocketUrl(url))
if (temporaryRelayUrls.length) { if (temporaryRelayUrls.length) {
return await switchFeed('temporary', { temporaryRelayUrls }) return await switchFeed('temporary', { temporaryRelayUrls })
} }

Loading…
Cancel
Save