Guard optional event fields before calling trim in live-log paths.

Legacy decrypted events may omit mgk or wind fields; optional chaining prevents runtime crashes in course prefill and stats aggregation.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-31 21:29:42 +02:00
co-authored by Cursor
parent aa03573e1f
commit 0b2c1c22c6
2 changed files with 7 additions and 5 deletions
+4 -2
View File
@@ -81,14 +81,16 @@ function hapticPulse() {
function lastCourseFromEvents(events: LogEventPayload[]): string { function lastCourseFromEvents(events: LogEventPayload[]): string {
for (let i = events.length - 1; i >= 0; i--) { for (let i = events.length - 1; i >= 0; i--) {
if (events[i].mgk.trim()) return events[i].mgk const mgk = events[i].mgk?.trim()
if (mgk) return mgk
} }
return '' return ''
} }
function lastWindDirectionFromEvents(events: LogEventPayload[]): string { function lastWindDirectionFromEvents(events: LogEventPayload[]): string {
for (let i = events.length - 1; i >= 0; i--) { for (let i = events.length - 1; i >= 0; i--) {
if (events[i].windDirection.trim()) return events[i].windDirection const direction = events[i].windDirection?.trim()
if (direction) return direction
} }
return '' return ''
} }
@@ -71,21 +71,21 @@ export async function loadLogbookEventSeries(logbookId: string): Promise<EventSe
time: event.time time: event.time
} }
if (event.windPressure.trim()) { if (event.windPressure?.trim()) {
pressure.push({ pressure.push({
...base, ...base,
summary: `${event.windPressure} hPa` summary: `${event.windPressure} hPa`
}) })
} }
if (event.windDirection.trim() || event.windStrength.trim()) { if (event.windDirection?.trim() || event.windStrength?.trim()) {
wind.push({ wind.push({
...base, ...base,
summary: [event.windDirection, event.windStrength].filter(Boolean).join(' ') summary: [event.windDirection, event.windStrength].filter(Boolean).join(' ')
}) })
} }
const code = event.remarks.trim() const code = event.remarks?.trim() ?? ''
if ( if (
code === LIVE_EVENT_CODES.MOTOR_START || code === LIVE_EVENT_CODES.MOTOR_START ||
code === LIVE_EVENT_CODES.MOTOR_STOP code === LIVE_EVENT_CODES.MOTOR_STOP