Browse Source

wip: refactoring tui

adamdottv 9 months ago
parent
commit
d70201cd93
1 changed files with 2 additions and 34 deletions
  1. 2 34
      internal/tui/tui.go

+ 2 - 34
internal/tui/tui.go

@@ -270,27 +270,19 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	case *client.EventStorageWrite:
 		slog.Debug("Received SSE event", "key", msg.Key)
 
-		// Create a deep copy of the state to avoid mutation issues
-		newState := deepCopyState(a.app.State)
-
-		// Split the key and traverse/create the nested structure
 		splits := strings.Split(msg.Key, "/")
-		current := newState
+		current := a.app.State
 
 		for i, part := range splits {
 			if i == len(splits)-1 {
-				// Last part - set the value
 				current[part] = msg.Content
 			} else {
-				// Intermediate parts - ensure map exists
 				if _, exists := current[part]; !exists {
 					current[part] = make(map[string]any)
 				}
 
-				// Navigate to the next level
 				nextLevel, ok := current[part].(map[string]any)
 				if !ok {
-					// If it's not a map, replace it with a new map
 					current[part] = make(map[string]any)
 					nextLevel = current[part].(map[string]any)
 				}
@@ -298,11 +290,8 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			}
 		}
 
-		// Update the app state
-		a.app.State = newState
-
 		// Trigger UI update by updating all pages with the new state
-		return a.updateAllPages(state.StateUpdatedMsg{State: newState})
+		return a.updateAllPages(state.StateUpdatedMsg{State: a.app.State})
 
 	case dialog.CloseQuitMsg:
 		a.showQuit = false
@@ -948,27 +937,6 @@ func (a appModel) View() string {
 	return appView
 }
 
-// deepCopyState creates a deep copy of a map[string]any
-func deepCopyState(src map[string]any) map[string]any {
-	if src == nil {
-		return nil
-	}
-
-	dst := make(map[string]any, len(src))
-	for k, v := range src {
-		switch val := v.(type) {
-		case map[string]any:
-			// Recursively copy nested maps
-			dst[k] = deepCopyState(val)
-		default:
-			// For other types, just copy the value
-			// Note: This is still a shallow copy for slices/arrays
-			dst[k] = v
-		}
-	}
-	return dst
-}
-
 func New(app *app.App) tea.Model {
 	startPage := page.ChatPage
 	model := &appModel{