Skip to Content
Octopus Energy Packages

Timeframe

useTimeframe hook, resolveTimeframe, and related types

useTimeframe

Manages timeframe state: the selected period, custom interval, anchor date, and computed startAt/endAt range. Optionally connects to a storage adapter.

Storage adapters such as localStorage and nuqs control the hook from their current snapshot during render. When no adapter is provided, the hook keeps local React state that resets on unmount.

import { useTimeframe } from "@octopus-energy/measurements/timeframe";
useTimeframe

Prop

Type

import { useTimeframe } from "@octopus-energy/measurements/timeframe";
import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";

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

The hook accepts the following options:

UseTimeframeOptions

Prop

Type


Overrides

Use overrides when app data needs to constrain the user's timeframe. The function must be pure: return the original timeframe when no constraint applies, or return a new Timeframe object with the enforced values.

The hook returns the overridden timeframe during render to avoid UI flicker, then commits the canonical inputs (date, period, and interval) back to React state/storage after render. Derived fields such as range, startAt, and endAt are recomputed from those canonical inputs.

const [timeframe, timeframeActions] = useTimeframe({
  timezone: "Europe/London",
  storage,
  overrides: (timeframe) => {
    const startOfLastYear = new Date(new Date().getFullYear() - 1, 0, 1);
    if (timeframe.date < startOfLastYear) {
      return { ...timeframe, period: "year" };
    }
    return timeframe;
  },
});

Custom interval persistence

The stored timeframe keeps the last selected interval even when the current period is not custom. This means switching from custom to another period and back restores the user's previous custom interval. For URL state, nuqs omits the interval when it matches the parser default, usually "week".


useStoredTimeframe

Reads persisted timeframe state from a storage adapter without applying useTimeframe fallback defaults such as today or "week".

Use this before client-side anchor-date resolution when you need to know whether storage already has a date, or which stored period/interval should be passed to anchorDateQueryOptions.

import { useStoredTimeframe } from "@octopus-energy/measurements/timeframe";
useStoredTimeframe

Prop

Type

const storedTimeframe = useStoredTimeframe(storage);

const anchorDateQuery = useQuery(
  anchorDateQueryOptions(client, {
    enabled: !storedTimeframe.date,
    interval: storedTimeframe.interval,
    marketSupplyPoints,
    period: storedTimeframe.period,
    propertyId,
    timezone: "Europe/London",
  }),
);

When storage has no valid timeframe value, the hook returns an empty object.

Stored timeframe state has the following shape:

StoredTimeframe

Prop

Type


resolveTimeframe

A pure function that computes a Timeframe object from a period, date, timezone, and optional interval. Used internally by useTimeframe but useful for server-side rendering or testing.

import { resolveTimeframe } from "@octopus-energy/measurements/timeframe";
resolveTimeframe

Prop

Type

import { resolveTimeframe } from "@octopus-energy/measurements/timeframe";

const timeframe = resolveTimeframe({
  date,
  period,
  interval,
  timezone: "Europe/London",
});

Applies the same defaults as useTimeframe: period = "week", date = today, interval = "week".

resolveTimeframe accepts the following options:

ResolveTimeframeOptions

Prop

Type


Serialization

Converts a Timeframe to and from a JSON-safe representation. Timeframe contains Date instances and optional undefined values which are not JSON-serializable. SerializedTimeframe.date is a YYYY-MM-DD calendar value, while its range bounds remain ISO timestamps. Use these utilities when passing a resolved timeframe through a serialization boundary (e.g. getServerSideProps return value in the Pages Router).

import { serializeTimeframe, deserializeTimeframe } from "@octopus-energy/measurements/timeframe";
serializeTimeframe

Prop

Type

deserializeTimeframe

Prop

Type

SerializedTimeframe

Prop

Type

import { resolveTimeframe, serializeTimeframe } from "@octopus-energy/measurements/timeframe";

// Server: resolve and serialize
const timeframe = resolveTimeframe({
  date,
  period,
  interval,
  timezone: "Europe/London",
});
const serialized = serializeTimeframe(timeframe);

// Client: deserialize back to a full Timeframe
import { deserializeTimeframe } from "@octopus-energy/measurements/timeframe";

const timeframe = deserializeTimeframe(serialized);

Calendar date parsing

Use parseCalendarDate to convert a YYYY-MM-DD calendar value into a local calendar Date. Do not use new Date("YYYY-MM-DD"), which interprets the value as midnight UTC.

import { parseCalendarDate } from "@octopus-energy/measurements/timeframe";

const date = parseCalendarDate("2026-05-27");

createTimeframeActions

Creates a TimeframeActions object that writes normalized timeframe inputs to a StorageAdapter. Useful for controlled integrations where the current Timeframe is resolved elsewhere, such as nuqs URL params parsed on the server.

import { createTimeframeActions } from "@octopus-energy/measurements/timeframe";
createTimeframeActions

Prop

Type

import { createTimeframeActions, resolveTimeframe } from "@octopus-energy/measurements/timeframe";

const timeframe = resolveTimeframe({ period, date: anchorDate, timezone });
const actions = createTimeframeActions(timeframe, storage);

Types

Timeframe

Prop

Type

TimeframeActions

Prop

Type

TypeValues
TimeframeRange"day" | "week" | "month" | "year"
TimeframePeriod"day" | "week" | "month" | "year" | "custom"
TimeframeInterval"day" | "week" | "month"

On this page