Collection
Tour Operator / Tour Operations · v0.2.2Validated
See the upcoming departures, local times, guides and booked seats.
Upcoming Tour Departures · Preview
Loading widget…
Preparing source code…
Idle
Tour Operator / Tour Operations · v0.2.2Validated
See the upcoming departures, local times, guides and booked seats.
Loading widget…
Preparing source code…
Import @lumo/widgets/widget-utilities.css in your Tailwind stylesheet, alongside the Lumo theme.
import "@lumo/tokens/tokens.css";
import { NextDepartureWidget } from "@lumo/widgets/catalog/tour-ops/next-departure";
export default function Example() {
return <NextDepartureWidget />;
}Props for integrating the React component in your app.
snapshotNextDepartureSnapshot | null (optional)simulateboolean (optional){
"snapshot": {
"asOf": 1795853220000,
"timeZone": "Europe/Paris",
"departures": [
{
"id": "morning",
"title": "Morning Harbour Walk",
"meetingPoint": "Quai des Pêcheurs",
"departsAt": 1795854300000,
"estimatedDepartsAt": 1795854900000,
"bookedSeats": 12,
"capacity": 15,
"status": "scheduled"
}
]
}
}import type { NextDepartureSnapshot, UpcomingDeparture } from "@lumo/widgets/catalog/tour-ops/next-departure";
type Tour = Omit<UpcomingDeparture, "departsAt" | "bookedSeats"> & { departureEpochSeconds: number; partySizes: readonly number[] };
export function toSnapshot(asOf: number, timeZone: string, tours: readonly Tour[]): NextDepartureSnapshot {
return { asOf, timeZone, departures: tours.map(({ departureEpochSeconds, partySizes, ...tour }) => ({ ...tour,
departsAt: departureEpochSeconds * 1000, bookedSeats: partySizes.reduce((total, seats) => total + seats, 0) })) };
}"use client";
import { BusFront } from "lucide-react";
import { useMemo, useState } from "react";
import { useReducedMotion } from "motion/react";
import { useAnimationInterval, useHorizontalSwipe } from "@lumo/hooks";
import { AnimatedNumber, Avatar, Badge, Progress, WidgetCard, WidgetPagination } from "@lumo/ui";
import { nextDepartureDemo } from "./next-departure.constants";
import type { NextDepartureSnapshot, UpcomingDeparture } from "./next-departure.types";
import shared from "../../shared/operations-list.module.css";
import styles from "../../shared/upcoming-booking.module.css";
export type { NextDepartureSnapshot, UpcomingDeparture } from "./next-departure.types";
function effectiveDeparture(item: UpcomingDeparture) {
const estimate = item.estimatedDepartsAt;
return typeof estimate === "number" && Number.isFinite(new Date(estimate).getTime()) ? estimate : item.departsAt;
}
function localTime(timestamp: number, asOf: number | null, timeZone: string) {
let day: Intl.DateTimeFormat;
try {
day = new Intl.DateTimeFormat("en-GB", { timeZone, day: "numeric", month: "short", year: "numeric" });
} catch {
timeZone = "UTC";
day = new Intl.DateTimeFormat("en-GB", { timeZone, day: "numeric", month: "short", year: "numeric" });
}
const time = new Intl.DateTimeFormat("en-GB", { timeZone, hour: "2-digit", minute: "2-digit", timeZoneName: "short" }).format(timestamp);
const date = day.format(timestamp);
if (asOf !== null && date === day.format(asOf)) return time;
const year = (value: number) => day.formatToParts(value).find(part => part.type === "year")?.value;
const dateLabel = asOf !== null && year(asOf) === year(timestamp)
? new Intl.DateTimeFormat("en-GB", { timeZone, day: "numeric", month: "short" }).format(timestamp) : date;
return `${dateLabel} · ${time}`;
}
export function NextDepartureWidget({ snapshot, simulate = true }: {
index?: number; snapshot?: NextDepartureSnapshot | null; simulate?: boolean;
} = {}) {
const [selected, setSelected] = useState<string>();
const [ticks, setTicks] = useState(0);
const reduced = useReducedMotion();
const data = snapshot === undefined ? nextDepartureDemo : snapshot;
const asOf = data && Number.isFinite(new Date(data.asOf).getTime()) ? data.asOf : null;
const departures = useMemo(() => {
if (!data) return [];
const seen = new Set<string>();
return data.departures.filter(item => {
if (!item.id || seen.has(item.id) || !Number.isFinite(new Date(item.departsAt).getTime()) ||
item.status === "departed" || item.status === "cancelled") return false;
if (item.status !== "boarding" && asOf !== null && effectiveDeparture(item) < asOf) return false;
seen.add(item.id);
return true;
}).sort((a, b) => effectiveDeparture(a) - effectiveDeparture(b));
}, [data, asOf]);
const page = Math.max(0, departures.findIndex(item => item.id === selected));
const item = departures[page];
const select = (index: number) => { const next = departures[index]; if (next) setSelected(next.id); };
const swipe = useHorizontalSwipe(direction => select(page + (direction === "left" ? 1 : -1)), departures.length > 1);
useAnimationInterval(() => setTicks(ticks => ticks + 1), simulate && snapshot === undefined && !reduced && ticks < 60 ? 1000 : null);
const departureTime = item ? effectiveDeparture(item) : null;
const remaining = departureTime !== null && asOf !== null
? Math.max(0, Math.ceil((departureTime - asOf - (snapshot === undefined ? ticks * 1000 : 0)) / 60000)) : null;
const countdown = remaining === null ? null : remaining >= 1440 ? { amount: remaining / 1440, unit: "d" }
: remaining >= 120 ? { amount: remaining / 60, unit: "h" } : { amount: remaining, unit: "min" };
const capacityKnown = item && Number.isSafeInteger(item.capacity) && item.capacity > 0 &&
Number.isSafeInteger(item.bookedSeats) && item.bookedSeats >= 0;
const over = capacityKnown && item.bookedSeats > item.capacity;
return <WidgetCard label="Next departure" title="Next Departure" description="Tour operations" size="small" icon={<BusFront />} className={shared.smallCard}>
<div {...swipe} data-studio-swipe-ignore className={styles.pages}>
<div className={styles.body}>
{item && data && departureTime !== null ? <>
<div className={styles.identity}><strong>{item.title}</strong><span>{item.meetingPoint}</span></div>
<div className={styles.reading}>
<div>
{item.status === "boarding" ? <Badge size="sm" tone="success">Boarding</Badge> : countdown === null ?
<span className={shared.muted}>Countdown unavailable</span> : remaining === 0 ?
<Badge size="sm" tone="info">Due now</Badge> : <strong className={styles.hero}><AnimatedNumber value={countdown.amount}
decimals={countdown.unit === "min" ? 0 : 1} /> <small>{countdown.unit}</small></strong>}
<time dateTime={new Date(departureTime).toISOString()}>
{departureTime > item.departsAt ? "Delayed · " : departureTime < item.departsAt ? "Earlier · " : ""}
{localTime(departureTime, asOf, data.timeZone)}
</time>
</div>
{item.guide && <Avatar size="sm" name={item.guide.name} src={item.guide.avatarUrl} title={item.guide.name} />}
</div>
{capacityKnown ? <div className={styles.capacity}>
<Progress value={Math.min(item.bookedSeats, item.capacity)} max={item.capacity} animateOnMount
className={shared.patternRail} aria-label="Seats booked" />
<span>{item.bookedSeats} / {item.capacity} seats{over ? " · Overbooked" : ""}</span>
</div> : <span className={shared.muted}>Capacity unavailable</span>}
</> : <p className={shared.muted}>{data ? "No upcoming departures" : "Departures unavailable"}</p>}
</div>
<div className={styles.pagination}><WidgetPagination page={page} count={departures.length} onChange={select}
pageLabel="Departure" pageLabels={departures.map(departure => departure.title)} label="Upcoming departures" /></div>
</div>
</WidgetCard>;
}
lucide-reactmotionreactreact-dom