"use client"; import { X } from "lucide-react"; import { AnimatePresence, motion, useInView, useReducedMotion, useSpring, } from "motion/react"; import { type CSSProperties, type KeyboardEvent, type ReactNode, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, } from "react"; import { Tooltip } from "@/components/motion/tooltip"; import { EASE_OUT, SPRING_PANEL, SPRING_SWAP } from "@/lib/ease"; import { useControllable } from "@/lib/hooks/use-controllable"; import { useLocale, useMessages } from "@/lib/i18n"; import { useSound } from "@/lib/sound"; import { buzzSound } from "@/lib/sounds/buzz"; import { dismissSound } from "@/lib/sounds/dismiss"; import { popSound } from "@/lib/sounds/pop"; import { selectSound } from "@/lib/sounds/select"; import { cn } from "@/lib/utils"; export const flightSeatMapMessages = { map: "Seat map", row: "Row", aisleColumn: "Aisle", seat: "Seat {seat}", window: "Window", middle: "Middle", aisle: "Aisle", standard: "Standard", "extra-legroom": "Extra legroom", premium: "Premium", available: "Available", taken: "Taken", legend: "Seat types", selectedFor: "selected for {name}", included: "Included", passengers: "Passengers", noSeat: "No seat yet", total: "Total", selected: "Selected", clear: "Remove seat for {name}", announce: "Seat {seat} selected for {name}", announceCleared: "Seat removed for {name}", hint: "Pick a seat for {name}", }; export type FlightSeatMapMessages = typeof flightSeatMapMessages; /** How a seat is sold: a plain seat, one with more room in front, or a premium cabin seat. */ export type SeatType = "standard" | "extra-legroom" | "premium"; /** A run of rows that share a seat type and price. */ export interface SeatSection { /** First row number in the section. */ from: number; /** Last row number in the section, inclusive. */ to: number; /** Seat type for every seat in these rows. Default: "standard" */ type?: SeatType; /** Price of each seat in these rows, in the `currency` unit. Leave out or pass 0 for seats included in the fare. */ price?: number; } /** The cabin's shape: which seat letters sit where, and which rows exist. */ export interface SeatMapLayout { /** Seat letters from left to right; an empty string marks an aisle, e.g. ["A", "B", "C", "", "D", "E", "F"]. */ columns: string[]; /** Row ranges from front to back. Rows not covered by a section are not drawn. */ sections: SeatSection[]; /** Rows with an emergency exit beside them, marked in green at both edges of the cabin floor. */ exitRows?: number[]; /** Row numbers the airline skips, such as 13. */ skipRows?: number[]; } /** Someone travelling on the booking who needs a seat. */ export interface SeatPassenger { /** Stable id, used as the key in the selection map. */ id: string; /** Name shown in the switcher, the summary and seat labels. */ name: string; } /** Passenger id to seat id (such as "12A"), or null for a passenger without a seat. */ export type SeatSelection = Record; export interface FlightSeatMapProps { /** The cabin layout: seat columns, aisles, sections, exit rows and skipped rows. */ layout: SeatMapLayout; /** Everyone who needs a seat. One passenger makes it a single-seat picker. */ passengers: SeatPassenger[]; /** Seat ids that are already taken, such as ["3C", "12A"]. */ taken?: string[]; /** Selected seats, for controlled use. */ value?: SeatSelection; /** Selected seats at first, when uncontrolled. Default: {} */ defaultValue?: SeatSelection; /** Called with the whole selection whenever a passenger's seat changes. */ onChange?: (value: SeatSelection) => void; /** ISO 4217 currency code for seat prices. Default: "USD" */ currency?: string; /** How far the cabin floor tips away from the viewer, in degrees. 0 draws it flat from above. Default: 42 */ tilt?: number; /** Play a pop when a head lands in a seat, a flick when a seat is given up, a click when switching passengers and a buzz on a taken seat. Off by default; a `SoundProvider` can mute it app-wide. Default: false */ sound?: boolean; /** Override any built-in string. */ labels?: Partial; className?: string; } type Seat = { id: string; row: number; letter: string; rowIndex: number; col: number; type: SeatType; price: number; position: "window" | "middle" | "aisle"; }; type Row = { row: number; type: SeatType; exit: boolean; sectionStart: boolean; seats: (Seat | null)[]; }; function buildCabin(layout: SeatMapLayout) { const { columns } = layout; const skip = new Set(layout.skipRows); const exits = new Set(layout.exitRows); const seatCols = columns.flatMap((c, i) => (c ? [i] : [])); const first = seatCols[0]; const last = seatCols[seatCols.length - 1]; const rows: Row[] = []; const seats = new Map(); for (const section of layout.sections) { const type = section.type ?? "standard"; for (let n = section.from; n <= section.to; n++) { if (skip.has(n)) continue; const rowIndex = rows.length; const cells = columns.map((letter, i) => { if (!letter) return null; const position = i === first || i === last ? "window" : columns[i - 1] === "" || columns[i + 1] === "" ? "aisle" : "middle"; const seat: Seat = { id: `${n}${letter}`, row: n, letter, rowIndex, col: seatCols.indexOf(i), type, price: section.price ?? 0, position, }; seats.set(seat.id, seat); return seat; }); rows.push({ row: n, type, exit: exits.has(n), sectionStart: n === section.from && rowIndex > 0, seats: cells, }); } } const grid = rows.map((r) => r.seats.filter((s): s is Seat => s !== null)); return { rows, seats, grid }; } /* -------------------------------------------------------------------------- */ /* Geometry (px). The cabin is laid out flat, then tipped back in 3D. */ /* -------------------------------------------------------------------------- */ /** Seat footprint on the floor: width across the cabin, depth along it. */ const SEAT_W = 58; const SEAT_D = 52; /** Seatback height, and how far a head peeks over it. */ const BACK_H = 46; const PEEK = 19; const HEAD = 25; const AISLE_W = 42; /** Floor margin between the seats and the cabin's edge. */ const PAD = 30; /** How far the rounding at each end of the cabin reaches along it. */ const END = 110; /** Camera distance for the stage's perspective. */ const PERSPECTIVE = 2400; /** Space between rows: more in front of extra legroom rows, which is the point of them. */ const PITCH: Record = { standard: 10, "extra-legroom": 26, premium: 16 }; /** The cabin settling into its tilt on entry: a heavy, unhurried, critically * damped glide, slower than any UI spring because it moves the whole scene. */ const CABIN_SETTLE = { stiffness: 90, damping: 19, mass: 1 }; /** Hair colours for travellers already seated, picked per seat so the cabin looks lived in. */ const HAIR = ["#2f2a26", "#5b4030", "#a7804f", "#8f8f8f", "#1f1f1f", "#6e3b2a"]; function hash(value: string) { let h = 0; for (let i = 0; i < value.length; i++) h = (h * 31 + value.charCodeAt(i)) | 0; return Math.abs(h); } function initials(name: string) { const parts = name.trim().split(/\s+/); return ((parts[0]?.[0] ?? "") + (parts.length > 1 ? (parts[parts.length - 1]?.[0] ?? "") : "")).toUpperCase(); } /** Seat fabrics, as CSS colours built from the theme so both modes stay in key. */ const FABRIC: Record = { standard: "color-mix(in oklab, var(--color-foreground) 22%, var(--color-background))", "extra-legroom": "color-mix(in oklab, var(--color-success) 55%, var(--color-background))", premium: "color-mix(in oklab, var(--color-warning) 62%, var(--color-background))", taken: "color-mix(in oklab, var(--color-foreground) 11%, var(--color-background))", picked: "var(--color-primary)", }; const NAV_KEYS = new Set(["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End", "PageUp", "PageDown"]); /** The seat a navigation key lands on from `at`, or undefined at an edge. */ function stepSeat(grid: Seat[][], at: Seat, key: string, ctrlKey: boolean) { const row = grid[at.rowIndex]; // Up and down keep the seat letter when the next row has it, else the // nearest column, so cabins with narrower rows still step cleanly. const sameColumn = (r: Seat[] | undefined) => r?.find((s) => s.letter === at.letter) ?? r?.reduce( (best, s) => (!best || Math.abs(s.col - at.col) < Math.abs(best.col - at.col) ? s : best), undefined, ); switch (key) { case "ArrowLeft": return row[row.indexOf(at) - 1]; case "ArrowRight": return row[row.indexOf(at) + 1]; case "ArrowUp": return sameColumn(grid[at.rowIndex - 1]); case "ArrowDown": return sameColumn(grid[at.rowIndex + 1]); case "Home": return ctrlKey ? grid[0]?.[0] : row[0]; case "End": return ctrlKey ? grid[grid.length - 1]?.at(-1) : row.at(-1); case "PageUp": return sameColumn(grid[Math.max(at.rowIndex - 5, 0)]); case "PageDown": return sameColumn(grid[Math.min(at.rowIndex + 5, grid.length - 1)]); default: return undefined; } } /* -------------------------------------------------------------------------- */ /* Scenery */ /* -------------------------------------------------------------------------- */ /** An emergency exit beside a row, marked on the floor at the cabin's edge. */ function ExitMark({ side, offset }: { side: "left" | "right"; offset: number }) { return (