Files
simple-aircraft-manager-rails/app/javascript/controllers/push_controller.js
T
rickyandClaude Opus 5.5 8d5f57da81 Installed app: offline squawks and pre-flight pages, push notifications, shortcuts
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>
2026-11-13 22:39:42 -05:00

64 lines
2.6 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { t } from "i18n"
// Profile → This device: notifications on or off for this browser or app.
export default class extends Controller {
static targets = [ "status", "on", "off", "test" ]
static values = { key: String, url: String, testUrl: String }
async connect() {
if (!("serviceWorker" in navigator) || !("PushManager" in window) || !("Notification" in window)) {
const ios = /iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1)
return this.show(t(ios ? "push.ios_install_first" : "push.unsupported"), null)
}
this.registration = await navigator.serviceWorker.ready
const subscription = await this.registration.pushManager.getSubscription()
if (Notification.permission === "denied") return this.show(t("push.blocked"), null)
this.show(t(subscription ? "push.on" : "push.off"), !!subscription)
}
async turnOn() {
if (await Notification.requestPermission() !== "granted") return this.show(t("push.blocked"), null)
try {
const subscription = await this.registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: this.keyBytes() })
const response = await this.request("POST", this.urlValue, { subscription: subscription.toJSON() })
if (!response.ok) throw new Error(response.status)
this.show(t("push.on"), true)
} catch (_) {
this.show(t("push.failed"), false)
}
}
async turnOff() {
const subscription = await this.registration.pushManager.getSubscription()
if (subscription) {
await this.request("DELETE", this.urlValue, { endpoint: subscription.endpoint })
await subscription.unsubscribe()
}
this.show(t("push.off"), false)
}
async sendTest() {
await this.request("POST", this.testUrlValue, {})
this.statusTarget.textContent = t("push.test_sent")
}
// on: true / false, or null when it can't be changed here.
show(text, on) {
this.statusTarget.textContent = text
this.onTarget.hidden = on !== false
this.offTarget.hidden = on !== true
this.testTarget.hidden = on !== true
}
request(method, url, body) {
return fetch(url, { method, credentials: "same-origin", body: JSON.stringify(body),
headers: { "Content-Type": "application/json", "X-CSRF-Token": document.querySelector("meta[name=csrf-token]")?.content || "" } })
}
keyBytes() {
const base64 = (this.keyValue + "=".repeat((4 - this.keyValue.length % 4) % 4)).replace(/-/g, "+").replace(/_/g, "/")
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
}
}