Skip to Content
Octopus Energy Packages

Render the Summary Card

Map MeasurementsSummary to Coral and render localized summary text

After fetching and deriving a MeasurementsSummary, render it as one visual unit. EnergySummary owns loading and request-error presentation. Your app owns route params, fetching, localized text, Coral compat props, indicator text, and chart rendering.

What you'll build

By the end of this guide, your summary page will render a Coral EnergySummary with headline text, per-group indicators, and week-over-week charts.

This guide builds on Fetch Summary Data.

Format summary text

The primitive summary and role helpers return display values, measurement flow direction, and trend data, but they do not produce product text.

The examples below inline simple English strings to keep the tutorial focused. In a real app, replace those string literals with your i18n solution.

getSummaryComparison(summary) can return a total comparison, a per-entry breakdown, or null when no usable comparison exists. Indicators use the same metric and trend language. Keep those text formatters together so the rendering step stays focused on layout.

_components/energy-summary-card.tsx
import type { SummaryIndicatorProps } from "@octopus-energy/measurements/compat";
import type {
  SummaryComparison,
  SummaryComparisonEntry,
} from "@octopus-energy/measurements/summary";

const listFormat = new Intl.ListFormat("en-GB", { type: "conjunction" });

function formatGroupComparison(entry: SummaryComparisonEntry): string {
  const energy = entry.utility ?? "energy";

  if (entry.delta.trend === "stable") {
    return `${energy} stayed about the same`;
  }

  const direction = entry.delta.trend === "up" ? "more" : "less";
  return `${energy} was ${entry.delta.difference.label} ${direction}`;
}

function formatComparison(
  comparison: SummaryComparison | null,
  energyType: "electricity" | "gas" | "energy",
): string {
  if (!comparison) return "No summary data is available yet.";

  if (comparison.kind === "breakdown") {
    const parts = comparison.entries.map(formatGroupComparison);
    return `${listFormat.format(parts)} last week compared to the week before.`;
  }

  const verb = comparison.metric === "cost" ? "spent" : "used";

  if (comparison.delta.percentage.value === Infinity) {
    return `You ${verb} ${energyType} last week, but had none the week before.`;
  }

  if (Math.abs(comparison.delta.percentage.value) <= 1) {
    return `You ${verb} about the same on ${energyType} last week compared to the week before.`;
  }

  const direction = comparison.delta.percentage.value < 0 ? "less" : "more";
  return `You ${verb} ${comparison.delta.percentage.label} ${direction} on ${energyType} last week compared to the week before.`;
}

function formatIndicator(indicator: SummaryIndicatorProps): string {
  const energy = indicator.utility ?? "energy";

  if (indicator.delta.trend === "stable") {
    return `About the same on ${energy}`;
  }

  const direction = indicator.delta.trend === "up" ? "more" : "less";
  return `${indicator.delta.difference.label} ${direction} on ${energy}`;
}

Glossary

The summary metric is "energy" | "cost". The per-group direction is the measurement flow ("consumption" | "generation"), while delta.trend is the change direction ("up" | "down" | "stable").

Select render-role data

Derive the energy type from market supply points and use role helpers to select the data needed by each visual role. Role helpers are safe to call without checking summary.status first.

_components/energy-summary-card.tsx
import {
  getMarketSupplyPointUtilities,
  type MarketSupplyPoint,
} from "@octopus-energy/measurements/market-supply-points";
import {
  getSummaryChartBars,
  getSummaryChartLabels,
  getSummaryComparison,
  getSummaryIndicators,
} from "@octopus-energy/measurements/summary";

function getEnergyType(marketSupplyPoints: MarketSupplyPoint[]) {
  const utilities = getMarketSupplyPointUtilities(marketSupplyPoints);

  if (utilities.length === 1 && utilities[0]) return utilities[0];
  return "energy";
}

const energyType = getEnergyType(marketSupplyPointsQuery.data ?? []);
const comparison = getSummaryComparison(summary);
const indicators = getSummaryIndicators(summary);
const chartBars = getSummaryChartBars(summary);
const chartLabels = getSummaryChartLabels(summary);

Each role helper has its own default metric preference. A helper can fall back from energy to cost, or from totals to a per-entry breakdown, without changing the primitive summary model.

Render indicators and charts

Keep the loading, errorMessage, and error values from the previous guide, and adapt role-helper outputs to Coral props at the render points.

_components/energy-summary-card.tsx
import {
  getSummaryChartBarProps,
  getSummaryChartLabelProps,
  getSummaryIndicatorsProps,
} from "@octopus-energy/measurements/compat";

return (
  <EnergySummary
    error={error}
    errorMessage={errorMessage ?? "Unable to load your energy summary"}
    loading={loading}
  >
    <EnergySummarySummary>
      <Typography component="h4" textStyle="h4">
        At a glance
      </Typography>

      <Typography textStyle="body2">{formatComparison(comparison, energyType)}</Typography>

      {getSummaryIndicatorsProps(indicators).map((indicator) => (
        <EnergySummaryIndicators key={indicator.key}>
          <EnergySummaryIndicator direction={indicator.direction} />
          <Typography textStyle="body2">{formatIndicator(indicator)}</Typography>
        </EnergySummaryIndicators>
      ))}
    </EnergySummarySummary>

    <EnergySummaryCharts>
      <EnergySummaryChartContainer>
        <EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.weekBefore)} />
        <EnergySummaryChartGroup>
          {getSummaryChartBarProps(chartBars.weekBefore).map(({ key, ...chart }) => (
            <EnergySummaryChart key={key} {...chart} />
          ))}
        </EnergySummaryChartGroup>
        <EnergySummaryChartLabel value="Week before" />
      </EnergySummaryChartContainer>

      <EnergySummaryChartContainer>
        <EnergySummaryChartLabel {...getSummaryChartLabelProps(chartLabels.lastWeek)} />
        <EnergySummaryChartGroup>
          {getSummaryChartBarProps(chartBars.lastWeek).map(({ key, ...chart }) => (
            <EnergySummaryChart key={key} {...chart} />
          ))}
        </EnergySummaryChartGroup>
        <EnergySummaryChartLabel value="Last week" />
      </EnergySummaryChartContainer>
    </EnergySummaryCharts>
  </EnergySummary>
);

Implementation checkpoint

At this checkpoint, the summary route fetches data client-side, derives a MeasurementsSummary, selects render-role data, and renders the Coral summary with app-owned text.

FAQ

On this page