All blocks
Order tracker
DataFollow a food order from the kitchen to the door, with stages and an arrival estimate.
Use the arrow keys to pan, plus and minus to zoom, and Shift with the arrow keys to rotate.
Order #4821
Noodle Bar Nørrebro
18 min
- Order placed
- Being prepared
- On the way
- Delivered
Your order
- Spicy ramen
- Gyoza ×6
- Iced tea
Install
Adds the block to src/lib/components/order-tracker and the map if you do not have
it yet.
Code
<script lang="ts">
import BikeIcon from '@lucide/svelte/icons/bike';
import CheckIcon from '@lucide/svelte/icons/check';
import HouseIcon from '@lucide/svelte/icons/house';
import RotateCcwIcon from '@lucide/svelte/icons/rotate-ccw';
import UtensilsIcon from '@lucide/svelte/icons/utensils';
import { cn } from '$lib/utils.js';
import { Badge } from '$lib/components/ui/badge/index.js';
import { Button } from '$lib/components/ui/button/index.js';
import * as Card from '$lib/components/ui/card/index.js';
import {
Map,
MapControlContainer,
MapMarker,
MapMarkerContent,
MapMarkerTooltip,
MapRoute,
MapZoomControl,
splitLine,
type MapStyle
} from '$lib/components/ui/map/index.js';
import { MINUTES_PER_TICK, ORDER, STAGES, TICK_MS, VIEWPORT } from './data.js';
interface Props {
/** Basemap. Defaults to OpenFreeMap. */
mapStyle?: MapStyle;
class?: string;
}
let { mapStyle, class: className }: Props = $props();
type StepStatus = 'done' | 'current' | 'upcoming';
const RIDE_START = STAGES.find((stage) => stage.id === 'on-the-way')?.at ?? 0;
const DELIVERED_AT = STAGES[STAGES.length - 1].at;
const restaurant = ORDER.route[0];
const door = ORDER.route[ORDER.route.length - 1];
/** Simulated minutes since the order was placed. */
let minutes = $state(0);
const delivered = $derived(minutes >= DELIVERED_AT);
const rideMinutes = DELIVERED_AT - RIDE_START;
// If the data gives the ride no time, the courier jumps to the door once the ride starts.
const rideProgress = $derived(
rideMinutes > 0
? Math.min(1, Math.max(0, (minutes - RIDE_START) / rideMinutes))
: Number(minutes >= RIDE_START)
);
const courierAt = $derived(splitLine(ORDER.route, rideProgress).point);
const currentIndex = $derived(STAGES.findLastIndex((stage) => stage.at <= minutes));
const minutesLeft = $derived(Math.max(0, Math.ceil(DELIVERED_AT - minutes)));
function stepStatus(index: number): StepStatus {
if (index < currentIndex || (index === currentIndex && delivered)) return 'done';
return index === currentIndex ? 'current' : 'upcoming';
}
function tick() {
minutes = Math.min(DELIVERED_AT, minutes + MINUTES_PER_TICK);
}
// The order clock runs until the food arrives, and stops if the block goes away.
$effect(() => {
if (delivered) return;
const timer = setInterval(tick, TICK_MS);
return () => clearInterval(timer);
});
</script>
<div class={cn('grid min-h-[560px] gap-4 p-4 lg:grid-cols-[minmax(0,1fr)_20rem]', className)}>
<Map
class="min-h-[360px] rounded-lg border"
aria-label="Order map"
{mapStyle}
viewport={VIEWPORT}
>
<MapRoute
id="order-route"
positions={ORDER.route}
progress={rideProgress}
restDashArray={[1, 1.5]}
showHead={false}
/>
<MapMarker lngLat={restaurant} aria-label={ORDER.restaurant}>
<MapMarkerContent>
<span class="grid size-7 place-items-center rounded-full border bg-background shadow-md">
<UtensilsIcon class="size-3.5" />
</span>
</MapMarkerContent>
<MapMarkerTooltip>{ORDER.restaurant}</MapMarkerTooltip>
</MapMarker>
<MapMarker lngLat={door} aria-label="Delivery address">
<MapMarkerContent>
<span class="grid size-7 place-items-center rounded-full border bg-background shadow-md">
<HouseIcon class="size-3.5" />
</span>
</MapMarkerContent>
<MapMarkerTooltip>Your door</MapMarkerTooltip>
</MapMarker>
{#if !delivered}
<MapMarker lngLat={courierAt} aria-label="Courier {ORDER.courier}">
<MapMarkerContent>
<span
class="grid size-9 place-items-center rounded-full bg-primary text-primary-foreground shadow-lg ring-4 ring-primary/25"
data-testid="courier"
data-progress={rideProgress.toFixed(3)}
>
<BikeIcon class="size-4" />
</span>
</MapMarkerContent>
<MapMarkerTooltip>{ORDER.courier}</MapMarkerTooltip>
</MapMarker>
{/if}
<MapControlContainer position="top-right">
<MapZoomControl />
</MapControlContainer>
</Map>
<Card.Root class="min-h-0 gap-4 overflow-y-auto">
<Card.Header>
<Card.Title>Order {ORDER.id}</Card.Title>
<Card.Description>{ORDER.restaurant}</Card.Description>
<Card.Action>
<Badge variant={delivered ? 'secondary' : 'default'} data-testid="eta">
{delivered ? 'Delivered' : `${minutesLeft} min`}
</Badge>
</Card.Action>
</Card.Header>
<Card.Content class="flex flex-col gap-5">
<ol class="flex flex-col gap-3" aria-label="Order progress">
{#each STAGES as stage, index (stage.id)}
{@const status = stepStatus(index)}
<li
class="flex items-center gap-3 text-sm"
aria-current={status === 'current' ? 'step' : undefined}
data-status={status}
data-testid="stage"
>
<span
class={[
'grid size-6 shrink-0 place-items-center rounded-full border',
status === 'done' && 'border-primary bg-primary text-primary-foreground',
status === 'current' && 'border-primary text-primary',
status === 'upcoming' && 'text-muted-foreground'
]}
>
{#if status === 'done'}
<CheckIcon class="size-3.5" />
{:else}
<span
class={[
'size-2 rounded-full',
status === 'current' ? 'bg-primary motion-safe:animate-pulse' : 'bg-border'
]}
></span>
{/if}
</span>
<span class={status === 'upcoming' ? 'text-muted-foreground' : 'font-medium'}>
{stage.label}
</span>
</li>
{/each}
</ol>
<div class="flex flex-col gap-1 border-t pt-4 text-sm">
<p class="font-medium">Your order</p>
<ul class="text-muted-foreground">
{#each ORDER.items as item (item)}
<li>{item}</li>
{/each}
</ul>
</div>
<Button variant="outline" size="sm" class="self-start" onclick={() => (minutes = 0)}>
<RotateCcwIcon />
Replay
</Button>
</Card.Content>
</Card.Root>
</div>
import type { LngLatTuple, MapViewport } from '$lib/components/ui/map/index.js';
/** Inner Copenhagen. */
export const VIEWPORT: MapViewport = {
center: [12.5729, 55.6829],
zoom: 13,
bearing: 0,
pitch: 0
};
/** Milliseconds between clock updates. */
export const TICK_MS = 500;
/** Simulated minutes that pass per update. */
export const MINUTES_PER_TICK = 0.5;
export interface Stage {
id: 'placed' | 'preparing' | 'on-the-way' | 'delivered';
label: string;
/** Minute after the order was placed when this stage starts. */
at: number;
}
/** The courier rides between `on-the-way` and `delivered`. */
export const STAGES: readonly Stage[] = [
{ id: 'placed', label: 'Order placed', at: 0 },
{ id: 'preparing', label: 'Being prepared', at: 1 },
{ id: 'on-the-way', label: 'On the way', at: 6 },
{ id: 'delivered', label: 'Delivered', at: 18 }
];
export interface Order {
id: string;
restaurant: string;
items: string[];
courier: string;
/** From the restaurant to the door, in riding order. */
route: LngLatTuple[];
}
export const ORDER: Order = {
id: '#4821',
restaurant: 'Noodle Bar Nørrebro',
items: ['Spicy ramen', 'Gyoza ×6', 'Iced tea'],
courier: 'Freja',
route: [
[12.5536, 55.6922],
[12.5592, 55.6889],
[12.5651, 55.6857],
[12.5703, 55.6829],
[12.5741, 55.6802],
[12.5812, 55.6786],
[12.5869, 55.6762],
[12.5921, 55.6735]
]
};