diff --git a/src/components/InboxViewer.tsx b/src/components/InboxViewer.tsx index 6d7e1a6..471927c 100644 --- a/src/components/InboxViewer.tsx +++ b/src/components/InboxViewer.tsx @@ -48,14 +48,10 @@ function InboxViewer() { const [isDecryptingAll, setIsDecryptingAll] = useState(false); - // Initialize service when account changes - useEffect(() => { - if (account) { - giftWrapService.init(account.pubkey, account.signer ?? null); - } - }, [account]); + // Note: Gift wrap service is now initialized globally in useAccountSync + // This ensures DM subscriptions are active even when inbox viewer isn't open - // Update signer when it changes + // Update signer when it changes (in case user switches signers) useEffect(() => { if (account?.signer) { giftWrapService.setSigner(account.signer); diff --git a/src/hooks/useAccountSync.ts b/src/hooks/useAccountSync.ts index bc7f96f..6b9d86a 100644 --- a/src/hooks/useAccountSync.ts +++ b/src/hooks/useAccountSync.ts @@ -6,6 +6,7 @@ import { addressLoader } from "@/services/loaders"; import type { RelayInfo } from "@/types/app"; import { normalizeRelayURL } from "@/lib/relay-url"; import { getServersFromEvent } from "@/services/blossom"; +import giftWrapService from "@/services/gift-wrap"; /** * Hook that syncs active account with Grimoire state and fetches relay lists and blossom servers @@ -125,4 +126,43 @@ export function useAccountSync() { storeSubscription.unsubscribe(); }; }, [activeAccount?.pubkey, eventStore, setActiveAccountBlossomServers]); + + // Initialize gift wrap service for NIP-17 DM subscriptions when account changes + useEffect(() => { + if (!activeAccount?.pubkey) { + console.log( + "[useAccountSync] No active account, cleaning up gift wrap service", + ); + giftWrapService.cleanup(); + return; + } + + const pubkey = activeAccount.pubkey; + const signer = activeAccount.signer ?? null; + + console.log( + `[useAccountSync] Initializing gift wrap service for user ${pubkey.slice(0, 8)}`, + ); + + // Initialize the service (loads inbox relays, sets up subscriptions) + giftWrapService.init(pubkey, signer); + + // Auto-enable inbox sync if not already set + // This ensures users receive DMs without manually enabling inbox + const currentSettings = giftWrapService.settings$.value; + if (!currentSettings.enabled) { + console.log( + "[useAccountSync] Auto-enabling inbox sync for NIP-17 DM subscriptions", + ); + giftWrapService.updateSettings({ enabled: true }); + } + + // Cleanup on account change or logout + return () => { + console.log( + `[useAccountSync] Cleaning up gift wrap service for user ${pubkey.slice(0, 8)}`, + ); + giftWrapService.cleanup(); + }; + }, [activeAccount?.pubkey, activeAccount?.signer]); }