Quick Start
Build a week-over-week EnergySummary comparison
This Quick Start builds a minimal client-rendered EnergySummary using
@octopus-energy/measurements, TanStack Query, and Coral components.
Use the full EnergySummary tutorial when you need a complete route-level implementation with clearer production boundaries.
Resolve the summary timeframe
EnergySummary compares the last complete ISO week with the week before it. Use
resolveSummaryTimeframe
to produce the fixed two-week measurement window.
import { resolveSummaryTimeframe } from "@octopus-energy/measurements/summary";
const timezone = "Europe/London";
const timeframe = resolveSummaryTimeframe({ timezone });Fetch supply points and measurements
Resolve market supply points first, then use them with the summary timeframe to fetch measurements. The measurements query waits until supply points are available.
import {
marketSupplyPointsQueryOptions,
measurementsQueryOptions,
} from "@octopus-energy/measurements/react-query";
import { useQuery } from "@tanstack/react-query";
const client = useGraphQLClient();
const marketSupplyPoints = useQuery(
marketSupplyPointsQueryOptions(client, {
accountNumber,
propertyId,
utilityMarkets,
}),
);
const measurements = useQuery(
measurementsQueryOptions(client, {
propertyId,
marketSupplyPoints: marketSupplyPoints.data,
timeframe,
}),
);Create the primitive summary
Group records with an ordered matching array, then pass those groups to
createMeasurementsSummary.
Passing the resolved market supply points groups records by supply point,
preserving their order for indicators and charts.
import { groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsSummary } from "@octopus-energy/measurements/summary";
const summaryGroups = groupMeasurements(measurements.data, {
matching: marketSupplyPoints.data ?? [],
});
const summary = createMeasurementsSummary(summaryGroups, {
currencyFallback: "GBP",
locale: "en-GB",
timeframe,
});createMeasurementsSummary always returns a primitive summary state.
status: "ready" means at least one entry has a comparable week-over-week
energy or cost delta. Entries that cannot be compared are still preserved in
summary.entries with status: "empty".
summary.status; // "ready" | "empty"
summary.entries; // one entry per input MeasurementGroup
summary.periods.weekBefore; // safe total for the earlier week
summary.periods.lastWeek; // safe total for the later week
summary.deltas; // safe total deltas by metricSelect render roles
Use role helpers to select the data needed by each part of the card. They are safe to call for both ready and empty summaries, and each helper owns its metric fallback for that render role.
import {
getSummaryChartBars,
getSummaryChartLabels,
getSummaryComparison,
getSummaryIndicators,
type SummaryComparison,
} from "@octopus-energy/measurements/summary";
const comparison = getSummaryComparison(summary);
const indicators = getSummaryIndicators(summary);
const chartBars = getSummaryChartBars(summary);
const chartLabels = getSummaryChartLabels(summary);
function formatComparison(comparison: SummaryComparison | null) {
if (!comparison) return "No summary data is available yet.";
if (comparison.kind === "breakdown") {
return `${comparison.entries.length} groups changed last week compared to the week before.`;
}
if (comparison.delta.trend === "stable") {
return "Last week was about the same as the week before.";
}
return `Last week was ${comparison.delta.percentage.label} ${
comparison.delta.trend === "up" ? "more" : "less"
} than the week before.`;
}Render with Coral compat props
Use the individual compat adapters at Coral render points. They are a temporary
bridge to Coral's current EnergySummary prop names; localized copy stays in
your app.
import {
getSummaryChartBarProps,
getSummaryChartLabelProps,
getSummaryIndicatorsProps,
} from "@octopus-energy/measurements/compat";
const loading = marketSupplyPoints.isPending || measurements.isPending;
const errorMessage = marketSupplyPoints.error?.message ?? measurements.error?.message;
const error = Boolean(errorMessage);
<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)}</Typography>
{getSummaryIndicatorsProps(indicators).map((indicator) => (
<EnergySummaryIndicators key={indicator.key}>
<EnergySummaryIndicator direction={indicator.direction} />
<Typography textStyle="body2">
{indicator.delta.trend === "stable"
? "About the same"
: `${indicator.delta.percentage.label} ${
indicator.delta.trend === "up" ? "more" : "less"
}`}
</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>;Next
- Tutorial: build a complete client-rendered week-over-week summary in focused steps.
- Next steps: move the fixed data fetching and summary derivation to the route.
- Summary API: full type reference for summary timeframe and transform helpers.
- Compat API: Coral bridge helpers.