mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Offline
- Flights and squawks saved offline are tagged with their user and only sent
while that user is signed in; storage is made persistent; Background Sync
sends them with the app closed (Chromium); unsent count on the app badge
- Report Squawk works offline (photos included, one-time key like flights)
- The Fleet page and, for each of your aircraft, the Overview, W&B, Log
Flight and Report Squawk pages and photo are saved ahead of time, so the
app opens and works without signal; copies say when they were saved
- Sign-out clears saved forms, pages and photos (asks first if items wait)
- Offline page rendered by Rails (localized, follows the theme, no inline
script); service worker version follows the build
Install and notify
- Web Push (web-push gem, VAPID keys from env or storage/vapid_keys.json):
grounding squawks, newly overdue items (daily, once each), signing
requests; per-device on/off and test in Profile -> This device; only
browser push service endpoints accepted
- Manifest shortcuts (Log Flight, Report Squawk via /go/...), lang and
localized description; install help on Profile; reload notice after a
release
Also
- Dockerfile: libssl-dev in the build stage (openssl gem); lockfile checksums
- Leftover English and %{_} interpolation names from the localization pass
- Guide and README updated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.9 KiB
JavaScript
94 lines
3.9 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { t } from "i18n"
|
|
import { queue } from "controllers/offline_queue"
|
|
|
|
// On every page: sends the flights and squawks this user saved offline once
|
|
// there's a connection, says what happened, and shows the count on the app
|
|
// icon. A rejected item stays until it's discarded.
|
|
export default class extends Controller {
|
|
static targets = [ "banner" ]
|
|
|
|
connect() {
|
|
this.sync = this.sync.bind(this)
|
|
window.addEventListener("online", this.sync)
|
|
document.addEventListener("offline-queue:changed", this.sync)
|
|
this.sync()
|
|
}
|
|
|
|
disconnect() {
|
|
window.removeEventListener("online", this.sync)
|
|
document.removeEventListener("offline-queue:changed", this.sync)
|
|
}
|
|
|
|
async sync() {
|
|
let items
|
|
try { items = await queue.mine() } catch (_) { return }
|
|
if (!items.length) { this.badge(0); return this.show("") }
|
|
if (!navigator.onLine || this.running) return this.status(items)
|
|
this.running = true
|
|
const sent = []
|
|
try {
|
|
for (const item of items.filter((i) => !i.error)) {
|
|
const result = await this.send(item)
|
|
if (result === "sent") { await queue.remove(item.id); sent.push(item) }
|
|
else if (result === "signin") { this.show(t("offline.sign_in")); return }
|
|
else if (result === "offline") break
|
|
else { item.error = result; await queue.put(item) }
|
|
}
|
|
} finally { this.running = false }
|
|
this.status(await queue.mine(), sent)
|
|
}
|
|
|
|
async send(item) {
|
|
const body = new FormData()
|
|
for (const [ name, value ] of item.fields) body.append(name, value)
|
|
const token = document.querySelector("meta[name=csrf-token]")?.content
|
|
if (token) body.append("authenticity_token", token)
|
|
let response
|
|
try {
|
|
response = await fetch(item.url, { method: "POST", body, credentials: "same-origin", headers: { Accept: "text/html" } })
|
|
} catch (_) { return "offline" }
|
|
if ([ 502, 503, 504 ].includes(response.status)) return "offline"
|
|
if (response.url.includes("/session/new")) return "signin"
|
|
if (response.ok) return "sent"
|
|
const html = await response.text()
|
|
const doc = new DOMParser().parseFromString(html, "text/html")
|
|
const message = [ ...doc.querySelectorAll("[role=alert], .field-error") ].map((e) => e.textContent.trim()).filter(Boolean).join(" ")
|
|
return message || t("offline.not_accepted", { status: response.status })
|
|
}
|
|
|
|
status(items, sent = []) {
|
|
const failed = items.filter((i) => i.error)
|
|
const waiting = items.filter((i) => !i.error)
|
|
const parts = []
|
|
for (const kind of [ "flight", "squawk" ]) {
|
|
const count = (list) => list.filter((i) => (i.kind || "flight") === kind).length
|
|
if (count(sent)) parts.push(t(`offline.${kind}.sent`, { count: count(sent) }))
|
|
if (count(waiting)) parts.push(t(`offline.${kind}.waiting`, { count: count(waiting) }))
|
|
}
|
|
this.show(parts.join(" "), failed)
|
|
this.badge(items.length)
|
|
}
|
|
|
|
// The number of unsent items on the installed app's icon.
|
|
badge(count) {
|
|
try { count ? navigator.setAppBadge?.(count) : navigator.clearAppBadge?.() } catch (_) { /* not supported */ }
|
|
}
|
|
|
|
show(text, failed = []) {
|
|
const banner = this.bannerTarget
|
|
banner.replaceChildren()
|
|
if (text) banner.append(Object.assign(document.createElement("p"), { textContent: text }))
|
|
for (const item of failed) {
|
|
const date = (item.fields.find(([ n ]) => n === "flight_log[date]") || [])[1] || ""
|
|
const row = document.createElement("p")
|
|
row.textContent = `${t(`offline.${item.kind || "flight"}.rejected`, { tail: item.tail, date, error: item.error })} `
|
|
const discard = Object.assign(document.createElement("button"), { type: "button", textContent: t("offline.discard"), className: "btn-secondary btn-sm ml-1" })
|
|
discard.addEventListener("click", async () => { await queue.remove(item.id); this.sync() })
|
|
row.append(discard)
|
|
banner.append(row)
|
|
}
|
|
banner.classList.toggle("hidden", !text && !failed.length)
|
|
}
|
|
}
|