diff --git a/src/components/ProfileViewer.tsx b/src/components/ProfileViewer.tsx index a5223f4..8ca1c36 100644 --- a/src/components/ProfileViewer.tsx +++ b/src/components/ProfileViewer.tsx @@ -32,6 +32,7 @@ import { useEffect, useState } from "react"; import type { Subscription } from "rxjs"; import { useGrimoire } from "@/core/state"; import { USER_SERVER_LIST_KIND, getServersFromEvent } from "@/services/blossom"; +import blossomServerCache from "@/services/blossom-server-cache"; export interface ProfileViewerProps { pubkey: string; @@ -128,40 +129,55 @@ export function ProfileViewer({ pubkey }: ProfileViewerProps) { // Fetch Blossom server list (kind 10063) useEffect(() => { - if (!resolvedPubkey) return; + if (!resolvedPubkey) { + setBlossomServers([]); + return; + } - let subscription: Subscription | null = null; + // First, check cache for instant display + blossomServerCache.getServers(resolvedPubkey).then((cachedServers) => { + if (cachedServers && cachedServers.length > 0) { + setBlossomServers(cachedServers); + } + }); - // Check if we already have the event in store + // Check if we already have the event in EventStore const existingEvent = eventStore.getReplaceable( USER_SERVER_LIST_KIND, resolvedPubkey, "", ); if (existingEvent) { - setBlossomServers(getServersFromEvent(existingEvent)); + const servers = getServersFromEvent(existingEvent); + setBlossomServers(servers); + // Also update cache + blossomServerCache.set(existingEvent); } - // Also fetch from network - subscription = addressLoader({ + // Subscribe to EventStore for reactive updates + const storeSubscription = eventStore + .replaceable(USER_SERVER_LIST_KIND, resolvedPubkey, "") + .subscribe((event) => { + if (event) { + const servers = getServersFromEvent(event); + setBlossomServers(servers); + // Also update cache + blossomServerCache.set(event); + } else { + setBlossomServers([]); + } + }); + + // Also fetch from network to get latest data + const networkSubscription = addressLoader({ kind: USER_SERVER_LIST_KIND, pubkey: resolvedPubkey, identifier: "", - }).subscribe({ - next: () => { - const event = eventStore.getReplaceable( - USER_SERVER_LIST_KIND, - resolvedPubkey, - "", - ); - if (event) { - setBlossomServers(getServersFromEvent(event)); - } - }, - }); + }).subscribe(); return () => { - subscription?.unsubscribe(); + storeSubscription.unsubscribe(); + networkSubscription.unsubscribe(); }; }, [resolvedPubkey, eventStore]);