import { useEffect, useState } from 'react' import { useItems } from '../Map/hooks/useItems' import { Tag } from '../../types' import { useTags } from '../Map/hooks/useTags' import { getValue } from '../../Utils/GetValue' import { MapOverlayPage } from './MapOverlayPage' import { TagView } from './TagView' import { useNavigate } from 'react-router-dom' function groupAndCount (arr) { const grouped = arr.reduce((acc, obj) => { const found = acc.find(item => JSON.stringify(item.object) === JSON.stringify(obj)) if (found) { found.count += 1 } else { acc.push({ object: obj, count: 1 }) } return acc }, []) return grouped.sort((a, b) => b.count - a.count) } export const MarketView = () => { const [offers, setOffers] = useState>([]) const [needs, setNeeds] = useState>([]) const navigate = useNavigate() const items = useItems() const tags = useTags() useEffect(() => { setOffers([]) setNeeds([]) items.map(i => { i?.layer?.itemOffersField && getValue(i, i.layer.itemOffersField)?.map(o => { const tag = tags.find(t => t.id === o.tags_id) tag && setOffers(current => [...current, tag]) }) i?.layer?.itemNeedsField && getValue(i, i.layer.itemNeedsField)?.map(n => { const tag = tags.find(t => t.id === n.tags_id) tag && setNeeds(current => [...current, tag]) }) }) console.log(offers) // eslint-disable-next-line react-hooks/exhaustive-deps }, [items]) return (

Offers

{groupAndCount(offers).map(o => ( navigate(`/?tags=${o.object.name}`)} key={o.object.id} tag={o.object} count={o.count}> ))}

Needs

{groupAndCount(needs).map(o => ( navigate(`/?tags=${o.object.name}`)} key={o.object.id} tag={o.object} count={o.count}> ))}
) }