From 1bc41c8c387a289e96a54755dd760cb4eabf6b5e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 14 Aug 2025 10:52:56 -0400 Subject: [PATCH] fix(tui): guard against panics in map member access --- internal/tui/tui.go | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 64aa86ea9633954a858fbd908cf8ea7fc0dce603..828f6a0f20cccd3b255a949ca366015ee161de0b 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -209,8 +209,13 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { }) // Permissions case pubsub.Event[permission.PermissionNotification]: + item, ok := a.pages[a.currentPage] + if !ok { + return a, nil + } + // forward to page - updated, cmd := a.pages[a.currentPage].Update(msg) + updated, cmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) return a, cmd case pubsub.Event[permission.PermissionRequest]: @@ -258,8 +263,13 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return a, tea.Batch(cmds...) case splash.OnboardingCompleteMsg: + item, ok := a.pages[a.currentPage] + if !ok { + return a, nil + } + a.isConfigured = config.HasInitialDataConfig() - updated, pageCmd := a.pages[a.currentPage].Update(msg) + updated, pageCmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) cmds = append(cmds, pageCmd) return a, tea.Batch(cmds...) @@ -273,7 +283,12 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { a.dialog = u.(dialogs.DialogCmp) cmds = append(cmds, dialogCmd) } else { - updated, pageCmd := a.pages[a.currentPage].Update(msg) + item, ok := a.pages[a.currentPage] + if !ok { + return a, nil + } + + updated, pageCmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) cmds = append(cmds, pageCmd) } @@ -284,7 +299,12 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { a.dialog = u.(dialogs.DialogCmp) cmds = append(cmds, dialogCmd) } else { - updated, pageCmd := a.pages[a.currentPage].Update(msg) + item, ok := a.pages[a.currentPage] + if !ok { + return a, nil + } + + updated, pageCmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) cmds = append(cmds, pageCmd) } @@ -292,8 +312,14 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } s, _ := a.status.Update(msg) a.status = s.(status.StatusCmp) - updated, cmd := a.pages[a.currentPage].Update(msg) + + item, ok := a.pages[a.currentPage] + if !ok { + return a, nil + } + updated, cmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) + if a.dialog.HasDialogs() { u, dialogCmd := a.dialog.Update(msg) a.dialog = u.(dialogs.DialogCmp) @@ -411,7 +437,12 @@ func (a *appModel) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd { a.dialog = u.(dialogs.DialogCmp) return dialogCmd } else { - updated, cmd := a.pages[a.currentPage].Update(msg) + item, ok := a.pages[a.currentPage] + if !ok { + return nil + } + + updated, cmd := item.Update(msg) a.pages[a.currentPage] = updated.(util.Model) return cmd }