From 99b6dbe697ffc96ab4d312ca6d67d030368d9a55 Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Sat, 21 Jun 2025 13:51:03 +0200 Subject: [PATCH] fix: add update listener --- internal/tui/components/chat/header/header.go | 10 ++++++ internal/tui/page/chat/chat.go | 35 ++++++++----------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/internal/tui/components/chat/header/header.go b/internal/tui/components/chat/header/header.go index 166ab69784be199412688569ac340b1b73cf64e5..78620161a75a3ade2e0e2416351c50699ac8bd4d 100644 --- a/internal/tui/components/chat/header/header.go +++ b/internal/tui/components/chat/header/header.go @@ -10,7 +10,9 @@ import ( "github.com/charmbracelet/crush/internal/llm/models" "github.com/charmbracelet/crush/internal/lsp" "github.com/charmbracelet/crush/internal/lsp/protocol" + "github.com/charmbracelet/crush/internal/pubsub" "github.com/charmbracelet/crush/internal/session" + "github.com/charmbracelet/crush/internal/tui/components/chat" "github.com/charmbracelet/crush/internal/tui/styles" "github.com/charmbracelet/crush/internal/tui/util" "github.com/charmbracelet/lipgloss/v2" @@ -44,6 +46,14 @@ func (p *header) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: p.width = msg.Width - 2 + case chat.SessionSelectedMsg: + p.session = msg + case pubsub.Event[session.Session]: + if msg.Type == pubsub.UpdatedEvent { + if p.session.ID == msg.Payload.ID { + p.session = msg.Payload + } + } } return p, nil } diff --git a/internal/tui/page/chat/chat.go b/internal/tui/page/chat/chat.go index e79eb35e27cb5805c000e774b209c66c96b01ebd..960c1317276fdd75994094a40740b36c9d49664b 100644 --- a/internal/tui/page/chat/chat.go +++ b/internal/tui/page/chat/chat.go @@ -64,10 +64,10 @@ type chatPage struct { } func (p *chatPage) Init() tea.Cmd { - cmd := p.layout.Init() return tea.Batch( - cmd, - p.layout.FocusPanel(layout.BottomPanel), // Focus on the bottom panel (editor) + p.layout.Init(), + p.compactSidebar.Init(), + p.layout.FocusPanel(layout.BottomPanel), // Focus on the bottom panel (editor), ) } @@ -88,9 +88,7 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { h, cmd := p.header.Update(msg) cmds = append(cmds, cmd) p.header = h.(header.Header) - if p.compactMode && p.showDetails { - cmds = append(cmds, p.compactSidebar.SetSize(msg.Width-4, 0)) - } + cmds = append(cmds, p.compactSidebar.SetSize(msg.Width-4, 0)) // the mode is only relevant when there is a session if p.session.ID != "" { // Only auto-switch to compact mode if not forced @@ -213,13 +211,7 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { p.showDetails = !p.showDetails p.header.SetDetailsOpen(p.showDetails) if p.showDetails { - p.compactSidebar = sidebarCmp(p.app, true) - c, cmd := p.compactSidebar.Update(chat.SessionSelectedMsg(p.session)) - p.compactSidebar = c.(layout.Container) - return p, tea.Batch( - cmd, - p.compactSidebar.SetSize(p.wWidth-4, 0), - ) + return p, tea.Batch() } return p, nil @@ -228,12 +220,12 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) { u, cmd := p.layout.Update(msg) cmds = append(cmds, cmd) p.layout = u.(layout.SplitPaneLayout) - - if p.compactMode && p.showDetails { - s, cmd := p.compactSidebar.Update(msg) - p.compactSidebar = s.(layout.Container) - cmds = append(cmds, cmd) - } + h, cmd := p.header.Update(msg) + p.header = h.(header.Header) + cmds = append(cmds, cmd) + s, cmd := p.compactSidebar.Update(msg) + p.compactSidebar = s.(layout.Container) + cmds = append(cmds, cmd) return p, tea.Batch(cmds...) } @@ -409,7 +401,8 @@ func NewChatPage(app *app.App) ChatPage { layout.WithFixedBottomHeight(5), layout.WithFixedRightWidth(31), ), - keyMap: DefaultKeyMap(), - header: header.New(app.LSPClients), + compactSidebar: sidebarCmp(app, true), + keyMap: DefaultKeyMap(), + header: header.New(app.LSPClients), } }