AirTrail sync and export, GPS tracks, airport database with autocomplete

AirTrail (https://github.com/johanohly/AirTrail):
- File export (Flights → Export for AirTrail) in AirTrail's import
  format, validated against its schema; same-day repeat legs get leg
  numbers so AirTrail's de-duplication doesn't merge them.
- Automatic sync through AirTrail's REST API (Profile → AirTrail sync):
  per-user connection with an encrypted API key and scope check; flights
  are created/updated/deleted in background jobs; a full sync first
  links flights already in AirTrail (e.g. from a file import) instead of
  duplicating them; airports are resolved and cached, with manual
  mapping for unknown codes.
- GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's
  track format for both sync and export; the flight page shows a summary.
- Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type.

Airport database (OurAirports, the source AirTrail uses):
- Managed like AirTrail: downloaded in the background on first start,
  updated in place from Admin → Airports (adds, changes, removes; ids
  stable), with admin-managed custom airports that updates never touch.
- Each airport carries AirTrail's code, so logged codes translate to
  AirTrail's exactly (1B9 → K1B9, 4MB → K5M3).
- From/To autocomplete on new flights (airports this aircraft has used
  rank first); From is pre-filled with the last flight's destination;
  codes are normalized to uppercase.
- Flights → Standardize airport codes: rewrite logged codes to the
  standard code, and fix codes not in the database (typos such as 0R8 →
  08R are suggested).

Also: admin pages grouped under an Admin dropdown menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-05 19:40:20 -05:00
1 parent 0c4803d717
commit 5bf682502b
68 files changed
+2667 -10

No files matched your search

@@ -0,0 +1,130 @@
import { Controller } from "@hotwired/stimulus"
// Airport code field with suggestions from the airport database.
// <div data-controller="airport-autocomplete" data-airport-autocomplete-url-value="/airports/search?aircraft_id=1">
// <input data-airport-autocomplete-target="input" ...>
// <ul data-airport-autocomplete-target="list" hidden></ul>
// <p data-airport-autocomplete-target="hint"></p>
// </div>
export default class extends Controller {
static targets = ["input", "list", "hint"]
static values = { url: String }
connect() {
this.results = []
this.active = -1
this.inputTarget.setAttribute("autocomplete", "off")
this.inputTarget.setAttribute("role", "combobox")
this.inputTarget.setAttribute("aria-autocomplete", "list")
this.inputTarget.setAttribute("aria-expanded", "false")
this.listTarget.id ||= `${this.inputTarget.id}-suggestions`
this.inputTarget.setAttribute("aria-controls", this.listTarget.id)
this.describe() // e.g. From pre-filled from the last flight
}
disconnect() { clearTimeout(this.timer) }
search() {
clearTimeout(this.timer)
this.timer = setTimeout(() => this.fetch(), 150)
}
async fetch() {
const q = this.inputTarget.value.trim()
if (q.length < 2) { this.close(); this.setHint(""); return }
const url = new URL(this.urlValue, window.location.origin)
url.searchParams.set("q", q)
const response = await fetch(url, { headers: { Accept: "application/json" } })
if (!response.ok || q !== this.inputTarget.value.trim()) return
this.results = await response.json()
this.active = -1
this.render()
this.updateHint()
}
render() {
this.listTarget.replaceChildren(...this.results.map((airport, i) => {
const li = document.createElement("li")
li.id = `${this.listTarget.id}-${i}`
li.setAttribute("role", "option")
li.className = "cursor-pointer px-3 py-1.5 text-sm hover:bg-sky-50"
const code = document.createElement("span")
code.className = "font-mono font-semibold"
code.textContent = airport.code
const name = document.createElement("span")
name.className = "ml-2 text-slate-700"
name.textContent = airport.name
const place = document.createElement("span")
place.className = "ml-1 text-xs text-slate-500"
place.textContent = [airport.place, airport.aka?.length ? `also ${airport.aka.join(", ")}` : null, airport.kind].filter(Boolean).join(" · ")
li.append(code, name, place)
li.addEventListener("mousedown", (event) => { event.preventDefault(); this.choose(i) })
return li
}))
const open = this.results.length > 0
this.listTarget.hidden = !open
this.inputTarget.setAttribute("aria-expanded", String(open))
}
keydown(event) {
if (this.listTarget.hidden) return
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault()
const step = event.key === "ArrowDown" ? 1 : -1
this.active = (this.active + step + this.results.length) % this.results.length
this.highlight()
} else if (event.key === "Enter" && this.active >= 0) {
event.preventDefault()
this.choose(this.active)
} else if (event.key === "Escape") {
this.close()
}
}
highlight() {
Array.from(this.listTarget.children).forEach((li, i) => {
li.classList.toggle("bg-sky-100", i === this.active)
li.setAttribute("aria-selected", String(i === this.active))
})
this.inputTarget.setAttribute("aria-activedescendant", this.active >= 0 ? `${this.listTarget.id}-${this.active}` : "")
}
choose(index) {
const airport = this.results[index]
this.inputTarget.value = airport.code
this.setHint(this.describeAirport(airport))
this.close()
}
blur() { setTimeout(() => this.close(), 100) }
close() {
this.listTarget.hidden = true
this.inputTarget.setAttribute("aria-expanded", "false")
}
// Show the airport name for a typed code, or note that it isn't in the database.
updateHint() {
const code = this.inputTarget.value.trim().toUpperCase()
const match = this.results.find((a) => a.code === code || (a.aka || []).includes(code))
if (match) this.setHint(this.describeAirport(match))
else if (code.length >= 2 && code.length <= 4 && !code.includes(" ")) this.setHint("Not in the airport database", true)
else this.setHint("")
}
async describe() {
if (!this.inputTarget.value.trim()) return
await this.fetch()
this.close()
}
describeAirport(a) {
return [a.name, a.place].filter(Boolean).join(", ")
}
setHint(text, warn = false) {
if (!this.hasHintTarget) return
this.hintTarget.textContent = text
this.hintTarget.classList.toggle("text-amber-700", warn)
}
}
@@ -0,0 +1,12 @@
import { Controller } from "@hotwired/stimulus"
// Closes a <details> dropdown when clicking elsewhere or pressing Escape.
export default class extends Controller {
closeOutside(event) {
if (!this.element.contains(event.target)) this.close()
}
close() {
this.element.removeAttribute("open")
}
}