Collection
Business / E-commerce · v0.2.0Validated
Find peak order periods and inspect individual weekday and two-hour slots in local time.
Order Demand by Hour · Preview
Loading widget…
Preparing source code…
Idle
Business / E-commerce · v0.2.0Validated
Find peak order periods and inspect individual weekday and two-hour slots in local time.
Loading widget…
Preparing source code…
Load the Lumo theme once in your app entry point. The example uses the local Lumo workspace packages.
import "@lumo/tokens/tokens.css";
import { OrderHeatmapWidget } from "@lumo/widgets/catalog/ecommerce/order-heatmap";
export default function Example() {
return <OrderHeatmapWidget />;
}Props for integrating the React component in your app.
snapshotOrderHeatmapSnapshot | null (optional){
"snapshot": {
"period": "Week ending 29 Nov",
"timeZone": "Europe/Paris",
"counts": [
[
0,
null,
2,
5,
12,
21,
34,
24,
17,
26,
32,
6
],
[],
[],
[],
[],
[],
[]
]
}
}"use client";
import { CalendarDays } from "lucide-react";
import { useRef, useState, type KeyboardEvent } from "react";
import { AnimatedNumber, Button, WidgetCard } from "@lumo/ui";
import { nonnegativeInteger } from "../../shared/operation-snapshot";
import shared from "../../shared/operations-list.module.css";
import styles from "./order-heatmap.module.css";
import { ordersDemo, slotLabel, slots, weekdays } from "./order-heatmap.constants";
import type { OrderHeatmapSnapshot } from "./order-heatmap.types";
export type { OrderHeatmapSnapshot } from "./order-heatmap.types";
export function OrderHeatmapWidget({ snapshot = ordersDemo }: { index?: number; snapshot?: OrderHeatmapSnapshot | null } = {}) {
const [selection, setSelection] = useState<number>();
const [hover, setHover] = useState<number>();
const cells = useRef<Array<HTMLButtonElement | null>>([]);
const values = weekdays.flatMap((_, day) => Array.from({ length: slots }, (_, slot) => {
const value = snapshot?.counts[day]?.[slot];
return nonnegativeInteger(value) ? value : null;
}));
const known = values.flatMap(value => value === null ? [] : [value]);
const max = known.length ? Math.max(...known) : null;
const peak = max === null ? 0 : values.indexOf(max);
const picked = selection ?? peak;
const active = hover ?? picked;
const amount = values[active];
const average = known.length ? known.reduce((sum, value) => sum + value, 0) / known.length : null;
function navigate(event: KeyboardEvent<HTMLButtonElement>, index: number) {
const column = index % slots;
const rowStart = index - column;
const target = event.key === "ArrowRight" ? index + Number(column < slots - 1)
: event.key === "ArrowLeft" ? index - Number(column > 0)
: event.key === "ArrowDown" ? index + slots : event.key === "ArrowUp" ? index - slots
: event.key === "Home" ? rowStart : event.key === "End" ? rowStart + slots - 1 : null;
if (target === null) return;
event.preventDefault();
event.stopPropagation();
if (!cells.current[target]) return;
setHover(undefined);
cells.current[target]?.focus();
}
return <WidgetCard title="Order Heatmap" description={snapshot && `${snapshot.period} · ${snapshot.timeZone}` || undefined}
label="Order heatmap" size="medium" icon={<CalendarDays />} className={shared.mediumCard}>
{snapshot ? <div className={styles.body}>
<div className={styles.summary}>
<div><span>{max === null ? "No orders reported" : `${active === peak && max > 0 ? "Peak · " : ""}${slotLabel(active)}`}</span>
<strong>{amount === null ? "Unavailable" : <><AnimatedNumber value={amount} blur={false} /> <small>orders</small></>}</strong>
<span>{amount !== null && average !== null && average > 0 ? `${(amount / average).toFixed(1)}× the slot average`
: `${known.length} / ${values.length} slots reported`}</span></div>
<Button type="button" size="xs" variant="ghost" disabled={picked === peak && hover === undefined}
onClick={() => { setSelection(undefined); setHover(undefined); }}>Peak slot</Button>
</div>
<div className={styles.grid} role="group" aria-label="Orders by weekday and two-hour slot" onPointerLeave={() => setHover(undefined)}
data-swipe-ignore data-studio-swipe-ignore>
{weekdays.map((day, row) => <div className={styles.row} key={day}>
<span aria-hidden="true">{day}</span>
{values.slice(row * slots, (row + 1) * slots).map((count, column) => {
const index = row * slots + column;
const intensity = max && count !== null ? Math.round(12 + (count / max) * 88) : 12;
return <button key={column} type="button" ref={node => { cells.current[index] = node; }} tabIndex={index === picked ? 0 : -1}
aria-pressed={index === picked} aria-label={`${slotLabel(index)}, ${count === null ? "unavailable" : `${count} orders`}`}
data-active={index === active || undefined} data-missing={count === null || undefined}
onFocus={() => setSelection(index)} onClick={() => { setSelection(index); setHover(undefined); }}
onPointerEnter={event => { if (event.pointerType === "mouse") setHover(index); }}
onKeyDown={event => navigate(event, index)} style={{ animationDelay: `${(row + column) * 16}ms`,
background: count === null ? undefined : `color-mix(in srgb, var(--info) ${intensity}%, var(--surface-muted))` }} />;
})}
</div>)}
<div className={styles.times} aria-hidden="true"><span>00</span><span>06</span><span>12</span><span>18</span><span>24</span></div>
</div>
{known.length < values.length && <p className={shared.muted}>{known.length}/{values.length} slots reported · Hatched cells are missing.</p>}
</div> : <p className={shared.muted}>Order data unavailable</p>}
</WidgetCard>;
}
lucide-reactmotionreactreact-dom