Fix threads not loading when navigating directly to them

This commit is contained in:
hzrd149 2023-09-08 15:02:51 -05:00
parent 94cd15663b
commit 81e86c9550
4 changed files with 19 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"nostrudel": patch
---
Fix threads not loading when navigating directly to them

View File

@ -2,7 +2,7 @@ import { getReferences } from "../helpers/nostr/events";
import { NostrEvent } from "../types/nostr-event"; import { NostrEvent } from "../types/nostr-event";
import { NostrRequest } from "./nostr-request"; import { NostrRequest } from "./nostr-request";
import { NostrMultiSubscription } from "./nostr-multi-subscription"; import { NostrMultiSubscription } from "./nostr-multi-subscription";
import Subject, { PersistentSubject } from "./subject"; import { PersistentSubject } from "./subject";
export class ThreadLoader { export class ThreadLoader {
loading = new PersistentSubject(false); loading = new PersistentSubject(false);
@ -66,6 +66,12 @@ export class ThreadLoader {
} }
} }
setRelays(relays: string[]) {
this.relays = relays;
this.subscription.setRelays(relays);
this.loadEvent();
}
private updateSubscription() { private updateSubscription() {
if (this.rootId.value) { if (this.rootId.value) {
this.subscription.setQuery({ "#e": [this.rootId.value], kinds: [1] }); this.subscription.setQuery({ "#e": [this.rootId.value], kinds: [1] });

View File

@ -1,9 +1,11 @@
import { useEffect, useMemo, useRef } from "react"; import { useEffect, useMemo, useRef } from "react";
import { useUnmount } from "react-use"; import { useUnmount } from "react-use";
import { ThreadLoader } from "../classes/thread-loader"; import { ThreadLoader } from "../classes/thread-loader";
import { linkEvents } from "../helpers/thread"; import { linkEvents } from "../helpers/thread";
import { useReadRelayUrls } from "./use-client-relays"; import { useReadRelayUrls } from "./use-client-relays";
import useSubject from "./use-subject"; import useSubject from "./use-subject";
import useRelaysChanged from "./use-relays-changed";
type Options = { type Options = {
enabled?: boolean; enabled?: boolean;
@ -25,6 +27,10 @@ export function useThreadLoader(eventId: string, additionalRelays: string[] = []
else loader.close(); else loader.close();
}, [enabled]); }, [enabled]);
useRelaysChanged(relays, () => {
loader.setRelays(relays);
});
useUnmount(() => { useUnmount(() => {
loader.close(); loader.close();
}); });

View File

@ -12,7 +12,7 @@ class SingleEventService {
const event = this.eventCache.get(id); const event = this.eventCache.get(id);
if (event) return event; if (event) return event;
this.pending.set(id, this.pending.get(id)?.concat(relays) ?? safeRelayUrls(relays)); this.pending.set(id, this.pending.get(id)?.concat(safeRelayUrls(relays)) ?? safeRelayUrls(relays));
const deferred = createDefer<NostrEvent>(); const deferred = createDefer<NostrEvent>();
this.pendingPromises.set(id, deferred); this.pendingPromises.set(id, deferred);
return deferred; return deferred;