|
| 1 | +import { ResourceChart } from "@/components/resource-chart"; |
| 2 | +import { getDatasetDetails } from "@/lib/ckan/dataset"; |
| 3 | +import { getResourceByName } from "@/lib/ckan/resource"; |
| 4 | +import { toPublicOrgSlug } from "@/lib/portal-name"; |
| 5 | +import type { ResourceChartConfig } from "@/components/resource-chart"; |
| 6 | +import HomeDataSignalCarouselClient, { |
| 7 | + type HomeDataSignalSlide, |
| 8 | +} from "./HomeDataSignalCarouselClient"; |
| 9 | + |
| 10 | +type SlideDefinition = HomeDataSignalSlide & { |
| 11 | + resourceName: string; |
| 12 | + config: ResourceChartConfig; |
| 13 | +}; |
| 14 | + |
| 15 | +type ResolvedSlide = SlideDefinition & { |
| 16 | + resourceId: string; |
| 17 | + resourceHref?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +const SLIDES: SlideDefinition[] = [ |
| 21 | + { |
| 22 | + id: "co2-concentration", |
| 23 | + resourceName: "co2-annmean-mlo", |
| 24 | + eyebrow: "Data signal", |
| 25 | + title: "Atmospheric CO2 concentration", |
| 26 | + description: |
| 27 | + "Annual mean carbon dioxide levels measured at Mauna Loa, showing the long-run trend in atmospheric concentration.", |
| 28 | + badges: ["CO2 concentration", "Annual series"], |
| 29 | + config: { |
| 30 | + type: "line", |
| 31 | + x: "Year", |
| 32 | + y: "Mean", |
| 33 | + aggregation: "avg", |
| 34 | + sort: "x-asc", |
| 35 | + limit: 90, |
| 36 | + xLabel: "Year", |
| 37 | + yLabel: "CO2 concentration (ppm)", |
| 38 | + height: 270, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: "temperature-anomaly", |
| 43 | + resourceName: "annual", |
| 44 | + eyebrow: "Climate record", |
| 45 | + title: "Global temperature anomaly", |
| 46 | + description: |
| 47 | + "Annual global surface temperature anomaly, useful for seeing how measured temperatures diverge from the historical baseline.", |
| 48 | + badges: ["Temperature anomaly", "Annual series"], |
| 49 | + config: { |
| 50 | + type: "line", |
| 51 | + x: "Year", |
| 52 | + y: "Mean", |
| 53 | + filters: { Source: "GCAG" }, |
| 54 | + aggregation: "avg", |
| 55 | + sort: "x-asc", |
| 56 | + limit: 180, |
| 57 | + xLabel: "Year", |
| 58 | + yLabel: "Temperature anomaly", |
| 59 | + height: 270, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + id: "fossil-co2-emitters", |
| 64 | + resourceName: "top-emitters", |
| 65 | + eyebrow: "Country comparison", |
| 66 | + title: "Largest fossil CO2 emitters in 2020", |
| 67 | + description: |
| 68 | + "A ranked snapshot of country-level fossil-fuel and cement-related CO2 emissions for the latest year in this resource.", |
| 69 | + badges: ["Million tonnes CO2", "Top countries"], |
| 70 | + config: { |
| 71 | + type: "bar-h", |
| 72 | + x: "country", |
| 73 | + y: "total_mt", |
| 74 | + filters: { year: 2020 }, |
| 75 | + aggregation: "sum", |
| 76 | + sort: "y-desc", |
| 77 | + limit: 8, |
| 78 | + xLabel: "Total emissions", |
| 79 | + yLabel: "Country", |
| 80 | + height: 330, |
| 81 | + }, |
| 82 | + }, |
| 83 | +]; |
| 84 | + |
| 85 | +function toClientSlide(slide: SlideDefinition): HomeDataSignalSlide { |
| 86 | + return { |
| 87 | + id: slide.id, |
| 88 | + eyebrow: slide.eyebrow, |
| 89 | + title: slide.title, |
| 90 | + description: slide.description, |
| 91 | + badges: slide.badges, |
| 92 | + resourceHref: slide.resourceHref, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export default async function HomeDataSignalCarousel() { |
| 97 | + const resources = await Promise.all( |
| 98 | + SLIDES.map((slide) => getResourceByName(slide.resourceName)), |
| 99 | + ); |
| 100 | + const datasets = await Promise.all( |
| 101 | + resources.map((resource) => |
| 102 | + resource?.package_id ? getDatasetDetails(resource.package_id).catch(() => null) : null, |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + const slides = SLIDES.reduce<ResolvedSlide[]>((resolvedSlides, slide, index) => { |
| 107 | + const resource = resources[index]; |
| 108 | + const dataset = datasets[index]; |
| 109 | + |
| 110 | + if (!resource?.id) return resolvedSlides; |
| 111 | + |
| 112 | + resolvedSlides.push({ |
| 113 | + ...slide, |
| 114 | + resourceId: resource.id, |
| 115 | + resourceHref: |
| 116 | + dataset?.name && dataset.organization?.name |
| 117 | + ? `/@${toPublicOrgSlug(dataset.organization.name)}/${dataset.name}` |
| 118 | + : undefined, |
| 119 | + }); |
| 120 | + |
| 121 | + return resolvedSlides; |
| 122 | + }, []); |
| 123 | + |
| 124 | + if (slides.length === 0) return null; |
| 125 | + |
| 126 | + return ( |
| 127 | + <HomeDataSignalCarouselClient |
| 128 | + slides={slides.map(toClientSlide)} |
| 129 | + > |
| 130 | + {slides.map((slide) => ( |
| 131 | + <ResourceChart |
| 132 | + key={slide.id} |
| 133 | + resourceId={slide.resourceId} |
| 134 | + config={slide.config} |
| 135 | + /> |
| 136 | + ))} |
| 137 | + </HomeDataSignalCarouselClient> |
| 138 | + ); |
| 139 | +} |
0 commit comments