fix(tui): guard against panics in map member access

Christian Rocha created

Change summary

internal/tui/tui.go | 43 +++++++++++++++++++++++++++++++++++++------
1 file changed, 37 insertions(+), 6 deletions(-)

Detailed changes

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
 		}