From 7bf05bc5ecd6975ed08670132d79f7250925e79f Mon Sep 17 00:00:00 2001 From: padreug Date: Tue, 5 Aug 2025 01:00:06 +0200 Subject: [PATCH] refactor: Enhance stall and product loading logic in useMarket composable - Update the loadStalls and loadProducts functions to filter events based on the active market's merchants instead of fetching from all authors. - Improve logging to provide clearer insights into the loading process and the merchants involved. - Add checks to handle cases where no active market or merchants are found, preventing unnecessary API calls. --- src/composables/useMarket.ts | 66 ++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/composables/useMarket.ts b/src/composables/useMarket.ts index e494f0e..021c192 100644 --- a/src/composables/useMarket.ts +++ b/src/composables/useMarket.ts @@ -69,7 +69,7 @@ export function useMarket() { // Load stalls for this market console.log('Loading stalls...') - await loadStalls(marketData.pubkey) + await loadStalls() console.log('Stalls loaded') // Load products for all stalls @@ -166,16 +166,29 @@ export function useMarket() { } } - const loadStalls = async (marketPubkey: string) => { + const loadStalls = async () => { try { const client = nostrStore.getClient() - console.log('Loading all stalls from all authors') + // Get the active market to filter by its merchants + const activeMarket = marketStore.activeMarket + if (!activeMarket) { + console.warn('No active market found') + return + } - // Fetch all stall events from all authors - // We'll filter by market membership in the application logic + const merchants = [...(activeMarket.opts.merchants || [])] + console.log('Loading stalls from market merchants:', merchants) + + if (merchants.length === 0) { + console.log('No merchants in market, skipping stall loading') + return + } + + // Fetch stall events from market merchants only const events = await client.fetchEvents({ - kinds: [MARKET_EVENT_KINDS.STALL] + kinds: [MARKET_EVENT_KINDS.STALL], + authors: merchants }) console.log('Found stall events:', events.length) @@ -235,13 +248,28 @@ export function useMarket() { try { const client = nostrStore.getClient() - // Fetch all product events from all authors - // We'll filter by stall membership in the application logic + // Get the active market to filter by its merchants + const activeMarket = marketStore.activeMarket + if (!activeMarket) { + console.warn('No active market found') + return + } + + const merchants = [...(activeMarket.opts.merchants || [])] + console.log('Loading products from market merchants:', merchants) + + if (merchants.length === 0) { + console.log('No merchants in market, skipping product loading') + return + } + + // Fetch product events from market merchants only const events = await client.fetchEvents({ - kinds: [MARKET_EVENT_KINDS.PRODUCT] + kinds: [MARKET_EVENT_KINDS.PRODUCT], + authors: merchants }) - console.log('Found product events from all authors:', events.length) + console.log('Found product events from market merchants:', events.length) // Group events by product ID and keep only the most recent version const productGroups = new Map() @@ -370,10 +398,26 @@ export function useMarket() { try { const client = nostrStore.getClient() - // Subscribe to real-time market updates + // Get the active market to filter by its merchants + const activeMarket = marketStore.activeMarket + if (!activeMarket) { + console.warn('No active market found for subscription') + return () => {} + } + + const merchants = [...(activeMarket.opts.merchants || [])] + if (merchants.length === 0) { + console.log('No merchants in market, skipping subscription') + return () => {} + } + + console.log('Subscribing to updates from market merchants:', merchants) + + // Subscribe to real-time market updates from market merchants only const filters = [ { kinds: [MARKET_EVENT_KINDS.STALL, MARKET_EVENT_KINDS.PRODUCT], + authors: merchants, since: Math.floor(Date.now() / 1000) } ]