refactor: Enhance useMarket composable to group and process recent stall and product events

- Implement grouping of stall and product events by their unique IDs to retain only the most recent versions.
- Update event processing logic to sort events by creation timestamp, ensuring the latest data is used for stalls and products.
- Improve logging for better visibility into the processing of events and the creation of market entities.
This commit is contained in:
padreug 2025-08-02 18:11:10 +02:00
parent 9e02574c27
commit 1fcc3706be

View file

@ -175,10 +175,32 @@ export function useMarket() {
console.log('Found stall events:', events.length) console.log('Found stall events:', events.length)
// Group events by stall ID and keep only the most recent version
const stallGroups = new Map<string, any[]>()
events.forEach(event => { events.forEach(event => {
try { try {
console.log('Processing stall event:', event)
const stallData = JSON.parse(event.content) const stallData = JSON.parse(event.content)
const stallId = stallData.id
if (!stallGroups.has(stallId)) {
stallGroups.set(stallId, [])
}
stallGroups.get(stallId)!.push({ event, stallData })
} catch (err) {
console.warn('Failed to parse stall event:', err)
}
})
// Process each stall group, keeping only the most recent version
stallGroups.forEach((stallEvents, stallId) => {
// Sort by created_at timestamp (most recent first)
stallEvents.sort((a, b) => b.event.created_at - a.event.created_at)
// Take the most recent version
const { event, stallData } = stallEvents[0]
console.log('Processing most recent stall event:', event)
console.log('Parsed stall data:', stallData) console.log('Parsed stall data:', stallData)
const stall: Stall = { const stall: Stall = {
@ -191,11 +213,8 @@ export function useMarket() {
shipping: stallData.shipping shipping: stallData.shipping
} }
console.log('Created stall:', stall) console.log('Created stall (most recent version):', stall)
marketStore.addStall(stall) marketStore.addStall(stall)
} catch (err) {
console.warn('Failed to parse stall event:', err)
}
}) })
} catch (err) { } catch (err) {
@ -226,10 +245,32 @@ export function useMarket() {
console.log('Found product events:', events.length) console.log('Found product events:', events.length)
// Group events by product ID and keep only the most recent version
const productGroups = new Map<string, any[]>()
events.forEach(event => { events.forEach(event => {
try { try {
console.log('Processing product event:', event)
const productData = JSON.parse(event.content) const productData = JSON.parse(event.content)
const productId = productData.id
if (!productGroups.has(productId)) {
productGroups.set(productId, [])
}
productGroups.get(productId)!.push({ event, productData })
} catch (err) {
console.warn('Failed to parse product event:', err)
}
})
// Process each product group, keeping only the most recent version
productGroups.forEach((productEvents, productId) => {
// Sort by created_at timestamp (most recent first)
productEvents.sort((a, b) => b.event.created_at - a.event.created_at)
// Take the most recent version
const { event, productData } = productEvents[0]
console.log('Processing most recent product event:', event)
console.log('Parsed product data:', productData) console.log('Parsed product data:', productData)
const stall = marketStore.stalls.find(s => s.pubkey === event.pubkey) const stall = marketStore.stalls.find(s => s.pubkey === event.pubkey)
@ -251,14 +292,11 @@ export function useMarket() {
updatedAt: event.created_at updatedAt: event.created_at
} }
console.log('Created product:', product) console.log('Created product (most recent version):', product)
marketStore.addProduct(product) marketStore.addProduct(product)
} else { } else {
console.warn('No stall found for product pubkey:', event.pubkey) console.warn('No stall found for product pubkey:', event.pubkey)
} }
} catch (err) {
console.warn('Failed to parse product event:', err)
}
}) })
} catch (err) { } catch (err) {