-
Notifications
You must be signed in to change notification settings - Fork 0
feat(phase6): native responder app — Capacitor, telemetry, field workflows, roster ops #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 29 commits
0f461bf
e061bf4
df155c1
ef8467a
23ff79f
346aaab
a2c5254
7ba4e00
be61329
30bd1aa
0e82790
eaf0924
a890a4d
181cf62
7ba5d47
f76be20
ec6424c
f8461af
54846d5
fa98982
7f74e7f
d607b19
0c95fcb
5853522
b2f70f9
7913469
07591ab
736ab8a
5e909d5
6b02486
9a1b93c
9a9ffdb
23ca286
6906024
63787f3
e3b6ea1
d800155
e709118
5b92722
70350d9
62dae79
011b39f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,3 +57,5 @@ Thumbs.db | |
| # Worktrees | ||
| .worktrees/ | ||
| worktrees/ | ||
| !apps/responder-app/ios/ | ||
| !apps/responder-app/android/ | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import { useEffect, useState } from 'react' | ||||||||||||||||||||||||||||||||||||||||||
| import { collection, onSnapshot, query, where } from 'firebase/firestore' | ||||||||||||||||||||||||||||||||||||||||||
| import { db } from '../app/firebase' | ||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||
| agencyAssistanceRequestDocSchema, | ||||||||||||||||||||||||||||||||||||||||||
| logDimension, | ||||||||||||||||||||||||||||||||||||||||||
| type AgencyAssistanceRequestDoc, | ||||||||||||||||||||||||||||||||||||||||||
| } from '@bantayog/shared-validators' | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const log = logDimension('useAgencyAssistanceQueue') | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export interface BackupRequest { | ||||||||||||||||||||||||||||||||||||||||||
| id: string | ||||||||||||||||||||||||||||||||||||||||||
| reportId: string | ||||||||||||||||||||||||||||||||||||||||||
| municipalityId: string | ||||||||||||||||||||||||||||||||||||||||||
| reason: string | ||||||||||||||||||||||||||||||||||||||||||
| status: 'pending' | 'accepted' | 'declined' | 'fulfilled' | 'expired' | ||||||||||||||||||||||||||||||||||||||||||
| agencyId: string | ||||||||||||||||||||||||||||||||||||||||||
| createdAt: number | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| function parseBackupRequest(id: string, raw: Record<string, unknown>): BackupRequest | null { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| typeof raw.reportId !== 'string' || | ||||||||||||||||||||||||||||||||||||||||||
| typeof raw.reason !== 'string' || | ||||||||||||||||||||||||||||||||||||||||||
| typeof raw.agencyId !== 'string' || | ||||||||||||||||||||||||||||||||||||||||||
| typeof raw.createdAt !== 'number' | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| log({ | ||||||||||||||||||||||||||||||||||||||||||
| severity: 'WARNING', | ||||||||||||||||||||||||||||||||||||||||||
| code: 'backup_request.invalid', | ||||||||||||||||||||||||||||||||||||||||||
| message: `Invalid backup_request ${id}`, | ||||||||||||||||||||||||||||||||||||||||||
| data: {}, | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||
| reportId: raw.reportId, | ||||||||||||||||||||||||||||||||||||||||||
| municipalityId: typeof raw.municipalityId === 'string' ? raw.municipalityId : '', | ||||||||||||||||||||||||||||||||||||||||||
| reason: raw.reason, | ||||||||||||||||||||||||||||||||||||||||||
| status: typeof raw.status === 'string' ? (raw.status as BackupRequest['status']) : 'pending', | ||||||||||||||||||||||||||||||||||||||||||
| agencyId: raw.agencyId, | ||||||||||||||||||||||||||||||||||||||||||
| createdAt: raw.createdAt, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export function useAgencyAssistanceQueue(agencyId: string | undefined) { | ||||||||||||||||||||||||||||||||||||||||||
| const [requests, setRequests] = useState<(AgencyAssistanceRequestDoc & { id: string })[]>([]) | ||||||||||||||||||||||||||||||||||||||||||
| const [backupRequests, setBackupRequests] = useState<BackupRequest[]>([]) | ||||||||||||||||||||||||||||||||||||||||||
| const [loading, setLoading] = useState(true) | ||||||||||||||||||||||||||||||||||||||||||
| const [error, setError] = useState<string | null>(null) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||
| queueMicrotask(() => { | ||||||||||||||||||||||||||||||||||||||||||
| setError(null) | ||||||||||||||||||||||||||||||||||||||||||
| setRequests([]) | ||||||||||||||||||||||||||||||||||||||||||
| setBackupRequests([]) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!agencyId) { | ||||||||||||||||||||||||||||||||||||||||||
| queueMicrotask(() => { | ||||||||||||||||||||||||||||||||||||||||||
| setLoading(false) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| queueMicrotask(() => { | ||||||||||||||||||||||||||||||||||||||||||
| setLoading(true) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+77
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear the previous queue state when the agency context changes. If ♻️ Proposed fix if (!agencyId) {
- queueMicrotask(() => {
- setLoading(false)
- })
+ setRequests([])
+ setBackupRequests([])
+ setLoading(false)
return
}
- queueMicrotask(() => {
- setLoading(true)
- })
+ setRequests([])
+ setBackupRequests([])
+ setLoading(true)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Comment on lines
+68
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial State reset via queueMicrotask has subtle timing. The Consider synchronous state reset for clarity: ♻️ Simpler synchronous reset useEffect(() => {
- queueMicrotask(() => {
- setError(null)
- setRequests([])
- setBackupRequests([])
- })
+ setError(null)
+ setRequests([])
+ setBackupRequests([])
if (!agencyId) {
- queueMicrotask(() => {
- setLoading(false)
- })
+ setLoading(false)
return
}
- queueMicrotask(() => {
- setLoading(true)
- })
+ setLoading(true)🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const q = query( | ||||||||||||||||||||||||||||||||||||||||||
| collection(db, 'agency_assistance_requests'), | ||||||||||||||||||||||||||||||||||||||||||
| where('targetAgencyId', '==', agencyId), | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const unsubscribe = onSnapshot( | ||||||||||||||||||||||||||||||||||||||||||
| q, | ||||||||||||||||||||||||||||||||||||||||||
| (snapshot) => { | ||||||||||||||||||||||||||||||||||||||||||
| const parsed = snapshot.docs.flatMap((doc) => { | ||||||||||||||||||||||||||||||||||||||||||
| const result = agencyAssistanceRequestDocSchema.safeParse(doc.data()) | ||||||||||||||||||||||||||||||||||||||||||
| if (!result.success) { | ||||||||||||||||||||||||||||||||||||||||||
| log({ | ||||||||||||||||||||||||||||||||||||||||||
| severity: 'WARNING', | ||||||||||||||||||||||||||||||||||||||||||
| code: 'agency_assistance.invalid', | ||||||||||||||||||||||||||||||||||||||||||
| message: `Doc ${doc.id} failed schema`, | ||||||||||||||||||||||||||||||||||||||||||
| data: {}, | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return [{ ...result.data, id: doc.id }] | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| setRequests(parsed) | ||||||||||||||||||||||||||||||||||||||||||
| setLoading(false) | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| (err) => { | ||||||||||||||||||||||||||||||||||||||||||
| setError(err.message) | ||||||||||||||||||||||||||||||||||||||||||
| setLoading(false) | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const backupQ = query(collection(db, 'backup_requests'), where('agencyId', '==', agencyId)) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const unsubscribeBackup = onSnapshot( | ||||||||||||||||||||||||||||||||||||||||||
| backupQ, | ||||||||||||||||||||||||||||||||||||||||||
| (snapshot) => { | ||||||||||||||||||||||||||||||||||||||||||
| const parsed = snapshot.docs.flatMap((doc) => { | ||||||||||||||||||||||||||||||||||||||||||
| const req = parseBackupRequest(doc.id, doc.data() as Record<string, unknown>) | ||||||||||||||||||||||||||||||||||||||||||
| return req ? [req] : [] | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| setBackupRequests(parsed) | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| () => { | ||||||||||||||||||||||||||||||||||||||||||
| // Backup request errors are non-fatal | ||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return () => { | ||||||||||||||||||||||||||||||||||||||||||
| unsubscribe() | ||||||||||||||||||||||||||||||||||||||||||
| unsubscribeBackup() | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }, [agencyId]) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return { requests, backupRequests, loading, error } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { useEffect, useState } from 'react' | ||
| import { collection, onSnapshot, query, where } from 'firebase/firestore' | ||
| import { db } from '../app/firebase' | ||
| import { callables } from '../services/callables' | ||
|
|
||
| export interface RosterResponder { | ||
| uid: string | ||
| displayName: string | ||
| availabilityStatus: string | ||
| lastTelemetryAt: number | null | ||
| municipalityId: string | ||
| } | ||
|
|
||
| export function useRosterManagement(agencyId: string | undefined) { | ||
| const [responders, setResponders] = useState<RosterResponder[]>([]) | ||
| const [loading, setLoading] = useState(true) | ||
| const [error, setError] = useState<string | null>(null) | ||
|
|
||
| useEffect(() => { | ||
| queueMicrotask(() => { | ||
| setResponders([]) | ||
| setError(null) | ||
| }) | ||
|
|
||
| if (!agencyId) { | ||
| queueMicrotask(() => { | ||
| setLoading(false) | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| queueMicrotask(() => { | ||
| setLoading(true) | ||
| }) | ||
|
|
||
| const q = query(collection(db, 'responders'), where('agencyId', '==', agencyId)) | ||
|
|
||
| const unsubscribe = onSnapshot( | ||
| q, | ||
| (snapshot) => { | ||
| const docs: RosterResponder[] = snapshot.docs.map((d) => { | ||
| const data = d.data() | ||
| return { | ||
| uid: d.id, | ||
| displayName: String(data.displayName ?? d.id), | ||
| availabilityStatus: String(data.availabilityStatus ?? 'unknown'), | ||
| lastTelemetryAt: typeof data.lastTelemetryAt === 'number' ? data.lastTelemetryAt : null, | ||
| municipalityId: String(data.municipalityId ?? ''), | ||
| } | ||
| }) | ||
| setResponders(docs) | ||
| setLoading(false) | ||
| }, | ||
| (err) => { | ||
| setError(err.message) | ||
| setLoading(false) | ||
| }, | ||
| ) | ||
|
|
||
| return () => { | ||
| unsubscribe() | ||
| } | ||
| }, [agencyId]) | ||
|
|
||
| const suspendResponder = async (uid: string) => { | ||
| await callables.suspendResponder({ uid, idempotencyKey: crypto.randomUUID() }) | ||
| } | ||
|
|
||
| const revokeResponder = async (uid: string) => { | ||
| await callables.revokeResponder({ uid, idempotencyKey: crypto.randomUUID() }) | ||
| } | ||
|
|
||
| const bulkAvailabilityOverride = async ( | ||
| uids: string[], | ||
| status: 'available' | 'unavailable' | 'off_duty', | ||
| ) => { | ||
| await callables.bulkAvailabilityOverride({ | ||
| uids, | ||
| status, | ||
| idempotencyKey: crypto.randomUUID(), | ||
| }) | ||
| } | ||
|
|
||
| return { responders, loading, error, suspendResponder, revokeResponder, bulkAvailabilityOverride } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.