wip: refactoring tui

This commit is contained in:
adamdottv 2025-05-29 06:45:34 -05:00
parent 9f1a75e938
commit d70201cd93
No known key found for this signature in database
GPG key ID: 9CB48779AF150E75

View file

@ -270,27 +270,19 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case *client.EventStorageWrite: case *client.EventStorageWrite:
slog.Debug("Received SSE event", "key", msg.Key) 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, "/") splits := strings.Split(msg.Key, "/")
current := newState current := a.app.State
for i, part := range splits { for i, part := range splits {
if i == len(splits)-1 { if i == len(splits)-1 {
// Last part - set the value
current[part] = msg.Content current[part] = msg.Content
} else { } else {
// Intermediate parts - ensure map exists
if _, exists := current[part]; !exists { if _, exists := current[part]; !exists {
current[part] = make(map[string]any) current[part] = make(map[string]any)
} }
// Navigate to the next level
nextLevel, ok := current[part].(map[string]any) nextLevel, ok := current[part].(map[string]any)
if !ok { if !ok {
// If it's not a map, replace it with a new map
current[part] = make(map[string]any) current[part] = make(map[string]any)
nextLevel = current[part].(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 // 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: case dialog.CloseQuitMsg:
a.showQuit = false a.showQuit = false
@ -948,27 +937,6 @@ func (a appModel) View() string {
return appView 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 { func New(app *app.App) tea.Model {
startPage := page.ChatPage startPage := page.ChatPage
model := &appModel{ model := &appModel{