--- import BaseLayout from '../layouts/base.astro'; import core from '../core.json'; import EventItem from '../components/EventItem.astro'; import { isFuture } from 'date-fns'; import { types, countryNames } from '../lib/events.js'; import { isFutureDate } from '../lib/date'; const { type: selectedType } = Astro.props; const { country: selectedCountry } = Astro.props; const typeObj = selectedType ? types.find(t => t.id === selectedType) : null const countryObj = selectedCountry ? { name: countryNames[selectedCountry] } : null let title = 'Events'; if (selectedType) { title = typeObj.plural } if (selectedCountry) { title = 'Events in ' + countryObj.name } let events = core.events; if (selectedType) { events = core.events.filter(ev => ev.type === selectedType) } if (selectedCountry) { events = core.events.filter(ev => ev.country === selectedCountry) } function eventsFilter (year, future=true) { return function (x) { if (!x.date.match(new RegExp(`^${year}`))) { return false } const isDate = x.date.match(/^\d{4}-\d{2}-\d{2}$/) if (!isDate) { return false } return future ? isFuture(new Date(x.date)) : !isFuture(new Date(x.date)) } } const pastYears = [...new Set(events.map(e => e.date.match(/^(\d{4})/)[1]))]; let upcomingEvents = []; let pastEvents = []; for (const e of events) { if (isFutureDate(e.date)) { upcomingEvents.push(e) } else { pastEvents.push(e) } } const upcoming = events.filter(x => isFutureDate(x.date)) const past = {} let pastTotal = 0 for (const year of pastYears.reverse()) { past[year] = pastEvents.filter(eventsFilter(year, false)).reverse() pastTotal += past[year].length } let places = [{ id: '', country: 'All countries', num: core.events.length }]; for (const ev of core.events) { const found = places.find(p => p.country === ev.country) if (found) { found.num++; continue; } if (!ev.country) { continue; } places.push({ id: ev.country?.toLowerCase(), country: ev.country, num: 1, }) } places = places.sort((x, y) => x.num < y.num ? 1 : -1) ---
{upcoming.length > 0 &&

Upcoming {title} ({upcoming.length})

{upcoming.map((event) => ( ))}
} {pastTotal > 0 &&

Past {title} ({events.length-upcoming.length})

{pastYears.map((year) => ( past[year].length > 0 &&

{year} ({past[year].length})

{past[year]?.map((event) => ( ))}
))}
}

Source repository