Skip to Content
Octopus Energy Packages

Build a Shareable Overview

Store overview timeframe and preference state in URL params

The overview already works with localStorage. Build a shareable overview when the selected timeframe and card preferences should survive reloads, be bookmarkable, or be readable during server-side rendering.

This guide swaps the localStorage adapters for nuqs-backed URL storage. It follows the sandbox pattern: use unscoped preferences for zero or one card, and scope preferences by marketSupplyPointId when multiple cards can render.

What you'll build

By the end of this guide, ConsumptionOverview will store shared timeframe state and card preference state in URL params.

Create combined URL state

Scope preferences only when needed

When multiple market supply points can render, create scoped preference params. When zero or one can render, use unscoped preference params.

_components/consumption-overview.tsx
const preferenceScopes =
  marketSupplyPoints.length > 1
    ? marketSupplyPoints.map(({ marketSupplyPointId }) => marketSupplyPointId)
    : undefined;

Register timeframe params and preference params in one useQueryStates call.

_components/consumption-overview.tsx
import { useQueryStates } from "nuqs";
import {
  createNuqsAdapter,
  createPreferencesSearchParams,
  createTimeframeSearchParams,
} from "@octopus-energy/measurements/storage/nuqs/client";

const [searchParams, setSearchParams] = useQueryStates({
  ...createTimeframeSearchParams(),
  ...createPreferencesSearchParams({ scopes: preferenceScopes }),
});

Unlike the localStorage version, this guide does not need useStoredTimeframe. useQueryStates already gives you the parsed URL timeframe values, so read searchParams.date, searchParams.period, and searchParams.interval directly.

Resolve the anchor date

Use URL state before anchor-date defaults. If the URL already contains a date, do not resolve an anchor date. If the URL only contains a period or custom interval, pass those parsed values and the same IANA timezone used by useTimeframe to the anchor-date query.

_components/consumption-overview.tsx
const anchorDateQuery = useQuery(
  anchorDateQueryOptions(client, {
    enabled: !searchParams.date && marketSupplyPoints.length > 0,
    interval: searchParams.interval,
    marketSupplyPoints,
    period: searchParams.period,
    propertyId,
    timezone: "Europe/London",
  }),
);

Use URL state for the timeframe

Create the unscoped adapter for the shared timeframe. Only mount the component that calls useTimeframe after you know the initial date: use the URL date when present, otherwise wait for the anchor-date query. If neither provides a date, useTimeframe falls back to today.

_components/consumption-overview.tsx
const timeframeStorage = createNuqsAdapter(searchParams, setSearchParams);
const initialDate = searchParams.date ?? anchorDateQuery.data;

const [timeframe, timeframeActions] = useTimeframe({
  defaults: {
    date: initialDate,
  },
  storage: timeframeStorage,
  timezone: "Europe/London",
});

Fetch measurements for the URL timeframe

Fetch measurements from the component that owns useTimeframe. Because that component only mounts after the initial date is ready, the measurement query is keyed to the same timeframe the overview renders.

_components/consumption-overview.tsx
const measurementsQuery = useQuery(
  measurementsQueryOptions(client, {
    propertyId,
    marketSupplyPoints,
    timeframe,
  }),
);

Scope URL state for card preferences

Create the preference storage factory in the outer component that owns URL state. Pass a scope only when the parser config was created with scopes. This keeps each card's display state independent without passing searchParams and setSearchParams into the inner overview component.

_components/consumption-overview.tsx
function getPreferenceStorage(marketSupplyPointId: string): StorageAdapter {
  return createNuqsAdapter(
    searchParams,
    setSearchParams,
    preferenceScopes ? { scope: marketSupplyPointId } : undefined,
  );
}

Implementation checkpoint

At this checkpoint, the outer overview owns combined URL state, exposes that state through nuqs storage adapters, and keeps shared timeframe state plus scoped card preferences in the URL.

FAQ

On this page