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.
This commit is contained in:
parent
ca3930296b
commit
7bf05bc5ec
1 changed files with 55 additions and 11 deletions
|
|
@ -69,7 +69,7 @@ export function useMarket() {
|
||||||
|
|
||||||
// Load stalls for this market
|
// Load stalls for this market
|
||||||
console.log('Loading stalls...')
|
console.log('Loading stalls...')
|
||||||
await loadStalls(marketData.pubkey)
|
await loadStalls()
|
||||||
console.log('Stalls loaded')
|
console.log('Stalls loaded')
|
||||||
|
|
||||||
// Load products for all stalls
|
// Load products for all stalls
|
||||||
|
|
@ -166,16 +166,29 @@ export function useMarket() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadStalls = async (marketPubkey: string) => {
|
const loadStalls = async () => {
|
||||||
try {
|
try {
|
||||||
const client = nostrStore.getClient()
|
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
|
const merchants = [...(activeMarket.opts.merchants || [])]
|
||||||
// We'll filter by market membership in the application logic
|
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({
|
const events = await client.fetchEvents({
|
||||||
kinds: [MARKET_EVENT_KINDS.STALL]
|
kinds: [MARKET_EVENT_KINDS.STALL],
|
||||||
|
authors: merchants
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('Found stall events:', events.length)
|
console.log('Found stall events:', events.length)
|
||||||
|
|
@ -235,13 +248,28 @@ export function useMarket() {
|
||||||
try {
|
try {
|
||||||
const client = nostrStore.getClient()
|
const client = nostrStore.getClient()
|
||||||
|
|
||||||
// Fetch all product events from all authors
|
// Get the active market to filter by its merchants
|
||||||
// We'll filter by stall membership in the application logic
|
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({
|
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
|
// Group events by product ID and keep only the most recent version
|
||||||
const productGroups = new Map<string, any[]>()
|
const productGroups = new Map<string, any[]>()
|
||||||
|
|
@ -370,10 +398,26 @@ export function useMarket() {
|
||||||
try {
|
try {
|
||||||
const client = nostrStore.getClient()
|
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 = [
|
const filters = [
|
||||||
{
|
{
|
||||||
kinds: [MARKET_EVENT_KINDS.STALL, MARKET_EVENT_KINDS.PRODUCT],
|
kinds: [MARKET_EVENT_KINDS.STALL, MARKET_EVENT_KINDS.PRODUCT],
|
||||||
|
authors: merchants,
|
||||||
since: Math.floor(Date.now() / 1000)
|
since: Math.floor(Date.now() / 1000)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue