fix: unable to publish to currently browsed relay

This commit is contained in:
codytseng
2025-08-29 22:35:54 +08:00
parent 1fb2c82bd5
commit 8c9416a6c8
4 changed files with 106 additions and 58 deletions

View File

@@ -2,6 +2,7 @@ import Sidebar from '@/components/Sidebar'
import { cn } from '@/lib/utils' import { cn } from '@/lib/utils'
import NoteListPage from '@/pages/primary/NoteListPage' import NoteListPage from '@/pages/primary/NoteListPage'
import HomePage from '@/pages/secondary/HomePage' import HomePage from '@/pages/secondary/HomePage'
import { CurrentRelaysProvider } from '@/providers/CurrentRelaysProvider'
import { TPageRef } from '@/types' import { TPageRef } from '@/types'
import { import {
cloneElement, cloneElement,
@@ -291,6 +292,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
: 0 : 0
}} }}
> >
<CurrentRelaysProvider>
<NotificationProvider> <NotificationProvider>
{!!secondaryStack.length && {!!secondaryStack.length &&
secondaryStack.map((item, index) => ( secondaryStack.map((item, index) => (
@@ -317,6 +319,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
<BottomNavigationBar /> <BottomNavigationBar />
<TooManyRelaysAlertDialog /> <TooManyRelaysAlertDialog />
</NotificationProvider> </NotificationProvider>
</CurrentRelaysProvider>
</SecondaryPageContext.Provider> </SecondaryPageContext.Provider>
</PrimaryPageContext.Provider> </PrimaryPageContext.Provider>
) )
@@ -337,6 +340,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
currentIndex: secondaryStack.length ? secondaryStack[secondaryStack.length - 1].index : 0 currentIndex: secondaryStack.length ? secondaryStack[secondaryStack.length - 1].index : 0
}} }}
> >
<CurrentRelaysProvider>
<NotificationProvider> <NotificationProvider>
<div className="flex h-screen overflow-hidden bg-surface-background"> <div className="flex h-screen overflow-hidden bg-surface-background">
<Sidebar /> <Sidebar />
@@ -376,6 +380,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
</div> </div>
<TooManyRelaysAlertDialog /> <TooManyRelaysAlertDialog />
</NotificationProvider> </NotificationProvider>
</CurrentRelaysProvider>
</SecondaryPageContext.Provider> </SecondaryPageContext.Provider>
</PrimaryPageContext.Provider> </PrimaryPageContext.Provider>
) )

View File

@@ -3,7 +3,7 @@ import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover
import { Switch } from '@/components/ui/switch' import { Switch } from '@/components/ui/switch'
import { isProtectedEvent } from '@/lib/event' import { isProtectedEvent } from '@/lib/event'
import { simplifyUrl } from '@/lib/url' import { simplifyUrl } from '@/lib/url'
import { useFeed } from '@/providers/FeedProvider' import { useCurrentRelays } from '@/providers/CurrentRelaysProvider'
import client from '@/services/client.service' import client from '@/services/client.service'
import { Info } from 'lucide-react' import { Info } from 'lucide-react'
import { Event } from 'nostr-tools' import { Event } from 'nostr-tools'
@@ -20,12 +20,12 @@ export default function SendOnlyToSwitch({
setSpecifiedRelayUrls: Dispatch<SetStateAction<string[] | undefined>> setSpecifiedRelayUrls: Dispatch<SetStateAction<string[] | undefined>>
}) { }) {
const { t } = useTranslation() const { t } = useTranslation()
const { relayUrls } = useFeed() const { currentRelayUrls } = useCurrentRelays()
const [urls, setUrls] = useState<string[]>([]) const [urls, setUrls] = useState<string[]>([])
useEffect(() => { useEffect(() => {
if (!parentEvent) { if (!parentEvent) {
setUrls(relayUrls) setUrls(currentRelayUrls)
return return
} }
const isProtected = isProtectedEvent(parentEvent) const isProtected = isProtectedEvent(parentEvent)
@@ -34,9 +34,9 @@ export default function SendOnlyToSwitch({
setSpecifiedRelayUrls(seenOn) setSpecifiedRelayUrls(seenOn)
setUrls(seenOn) setUrls(seenOn)
} else { } else {
setUrls(relayUrls) setUrls(currentRelayUrls)
} }
}, [parentEvent, relayUrls]) }, [parentEvent, currentRelayUrls])
if (!urls.length) return null if (!urls.length) return null

View File

@@ -2,12 +2,20 @@ import Relay from '@/components/Relay'
import RelayPageControls from '@/components/RelayPageControls' import RelayPageControls from '@/components/RelayPageControls'
import PrimaryPageLayout from '@/layouts/PrimaryPageLayout' import PrimaryPageLayout from '@/layouts/PrimaryPageLayout'
import { normalizeUrl, simplifyUrl } from '@/lib/url' import { normalizeUrl, simplifyUrl } from '@/lib/url'
import { useCurrentRelays } from '@/providers/CurrentRelaysProvider'
import { Server } from 'lucide-react' import { Server } from 'lucide-react'
import { forwardRef, useMemo } from 'react' import { forwardRef, useEffect, useMemo } from 'react'
const RelayPage = forwardRef(({ url }: { url?: string }, ref) => { const RelayPage = forwardRef(({ url }: { url?: string }, ref) => {
const { setTemporaryRelayUrls } = useCurrentRelays()
const normalizedUrl = useMemo(() => (url ? normalizeUrl(url) : undefined), [url]) const normalizedUrl = useMemo(() => (url ? normalizeUrl(url) : undefined), [url])
useEffect(() => {
if (normalizedUrl) {
setTemporaryRelayUrls([normalizedUrl])
}
}, [normalizedUrl])
return ( return (
<PrimaryPageLayout <PrimaryPageLayout
pageName="relay" pageName="relay"

View File

@@ -0,0 +1,35 @@
import { usePrimaryPage } from '@/PageManager'
import { createContext, useContext, useEffect, useState } from 'react'
import { useFeed } from './FeedProvider'
type TCurrentRelaysContext = {
currentRelayUrls: string[]
setTemporaryRelayUrls: (urls: string[]) => void
}
const CurrentRelaysContext = createContext<TCurrentRelaysContext | undefined>(undefined)
export const useCurrentRelays = () => {
const context = useContext(CurrentRelaysContext)
if (!context) {
throw new Error('useCurrentRelays must be used within a CurrentRelaysProvider')
}
return context
}
export function CurrentRelaysProvider({ children }: { children: React.ReactNode }) {
const { current } = usePrimaryPage()
const { relayUrls } = useFeed()
const [currentRelayUrls, setCurrentRelayUrls] = useState<string[]>([])
const [temporaryRelayUrls, setTemporaryRelayUrls] = useState<string[]>([])
useEffect(() => {
setCurrentRelayUrls(current === 'relay' ? temporaryRelayUrls : relayUrls)
}, [temporaryRelayUrls, current, relayUrls])
return (
<CurrentRelaysContext.Provider value={{ currentRelayUrls, setTemporaryRelayUrls }}>
{children}
</CurrentRelaysContext.Provider>
)
}