mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
What people can do on an aircraft now comes from their role there, and administrators set what each role allows (Admin → Roles). - Roles are installation-wide sets of permissions (31, grouped as flying, squawks and notes, maintenance records, imports and exports, the aircraft, notifications). Seeing an aircraft's records needs none. - Built in: Owner (always every permission, so an aircraft can always be managed), Mechanic (shop maintenance: logbook, signing, components, inspections, ADs, squawks, reminders), Pilot (what pilots could do before) and Viewer (view only). Administrators can change all but Owner's, and add their own; a role can be deleted once nobody has it. - Existing owners and pilots keep exactly what they could do. - Admin → Users → a user lists their role on every aircraft, to change, remove or add. On the aircraft's Access page, anyone who manages people hands out roles, but only an Owner can give or take away Owner, nobody changes their own (except administrators), and an aircraft always keeps an Owner (AircraftAccess). - Controllers declare the permission each action needs; a request that changes something with none declared is refused (and raises in development and tests), and a test walks every route to check. - Weekly reminders and overdue alerts go to roles that get reminders; the DRS reminder also needs Manage ADs. The API follows the person's role; its owners_only error is now not_permitted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
3.9 KiB
JavaScript
81 lines
3.9 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { currentUser } from "controllers/offline_queue"
|
|
|
|
// Saves, ahead of time, what's needed to work without signal: the Fleet page
|
|
// and, for each of your aircraft, its Overview, W&B, Log Flight and Report
|
|
// Squawk pages (fetching them through the service worker keeps a copy, see
|
|
// service-worker.js) and its photo (kept in sam-images, shown when offline). Done in the background at most once an hour, or when
|
|
// your aircraft change. Signing out clears it.
|
|
const EVERY = 60 * 60 * 1000
|
|
|
|
// How far along it is, for the ring in the top bar (connection_controller.js).
|
|
const progress = (done, total) => document.dispatchEvent(new CustomEvent("sam:progress", { detail: { source: "prep", done, total } }))
|
|
|
|
export default class extends Controller {
|
|
static values = { aircraft: Array }
|
|
|
|
connect() {
|
|
if (!navigator.onLine || !("serviceWorker" in navigator)) return
|
|
navigator.serviceWorker.ready.then(() => this.prepare()).catch(() => {})
|
|
}
|
|
|
|
async prepare() {
|
|
const signature = `${currentUser()}:${JSON.stringify(this.aircraftValue)}`
|
|
let last = {}
|
|
try { last = JSON.parse(localStorage.getItem("sam-offline-prep") || "{}") } catch (_) { /* storage off */ }
|
|
if (last.signature === signature && Date.now() - last.at < EVERY) return
|
|
if (!navigator.serviceWorker.controller) return // first visit: the worker takes over on the next page
|
|
|
|
const forms = {}
|
|
const paths = [ "/" ]
|
|
for (const a of this.aircraftValue) {
|
|
paths.push(...a.pages)
|
|
if (a.flight) { paths.push(a.flight); forms[a.flight] = { tail: a.tail, kind: "flight" } }
|
|
if (a.squawk) { paths.push(a.squawk); forms[a.squawk] = { tail: a.tail, kind: "squawk" } }
|
|
}
|
|
const pictures = this.aircraftValue.flatMap((a) => a.pictures || [])
|
|
const total = paths.length + 1
|
|
try {
|
|
for (const [ index, path ] of paths.entries()) {
|
|
progress(index, total)
|
|
if (!navigator.onLine) return
|
|
// One at a time, after the page has settled, so it never competes with what you're doing.
|
|
await new Promise((resolve) => (window.requestIdleCallback || setTimeout)(resolve, { timeout: 2000 }))
|
|
try { await fetch(path, { credentials: "same-origin", headers: { Accept: "text/html" } }) } catch (_) {
|
|
document.dispatchEvent(new CustomEvent("sam:unreachable"))
|
|
return
|
|
}
|
|
}
|
|
progress(paths.length, total)
|
|
await this.savePictures(pictures)
|
|
} finally { progress(total, total) }
|
|
try {
|
|
localStorage.setItem("sam-offline-forms", JSON.stringify({ ...JSON.parse(localStorage.getItem("sam-offline-forms") || "{}"), ...forms }))
|
|
localStorage.setItem("sam-offline-prep", JSON.stringify({ signature, at: Date.now() }))
|
|
} catch (_) { /* storage off */ }
|
|
}
|
|
|
|
// The aircraft photos: refreshed, and those of aircraft you no longer have dropped.
|
|
async savePictures(urls) {
|
|
try {
|
|
const cache = await caches.open("sam-images")
|
|
const wanted = urls.map((url) => [ url, imageKey(url) ]).filter(([ , key ]) => key)
|
|
for (const request of await cache.keys()) if (!wanted.some(([ , key ]) => key === request.url)) await cache.delete(request)
|
|
for (const [ url, key ] of wanted) {
|
|
const response = await fetch(url, { credentials: "same-origin" }).catch(() => null)
|
|
if (response?.ok) await cache.put(key, response)
|
|
}
|
|
} catch (_) { /* no Cache API */ }
|
|
}
|
|
}
|
|
|
|
// Photo links carry an expiry, so they change with every page; the saved copy
|
|
// is filed under the upload's id (and size) instead (same rule in service-worker.js).
|
|
function imageKey(url) {
|
|
try {
|
|
const [ , signed, size ] = new URL(url, location.origin).pathname.match(/\/(?:blobs|representations)\/redirect\/([^/]+?)--[^/]+\/(?:([^/]+)\/)?/)
|
|
const id = JSON.parse(atob(decodeURIComponent(signed)))._rails.data
|
|
return `${location.origin}/offline-image/${id}${size ? `/${size}` : ""}`
|
|
} catch (_) { return null }
|
|
}
|