refactor: improvements

Carlos Alexandro Becker created

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

internal/ui/dialog/commands.go | 29 +++++++++++------------------
internal/ui/model/ui.go        | 23 ++++++++++++-----------
2 files changed, 23 insertions(+), 29 deletions(-)

Detailed changes

internal/ui/dialog/commands.go 🔗

@@ -63,24 +63,22 @@ type Commands struct {
 
 	windowWidth int
 
-	customCommands []commands.CustomCommand
-	mcpPrompts     []commands.MCPPrompt
-	canFork        bool // whether a user message is selected for forking
-	canDelete      bool // whether a user message is selected for deletion
+	customCommands        []commands.CustomCommand
+	mcpPrompts            []commands.MCPPrompt
+	isUserMessageSelected bool // whether a user message is selected in chat
 }
 
 var _ Dialog = (*Commands)(nil)
 
 // NewCommands creates a new commands dialog.
-func NewCommands(com *common.Common, sessionID string, customCommands []commands.CustomCommand, mcpPrompts []commands.MCPPrompt, canFork, canDelete bool) (*Commands, error) {
+func NewCommands(com *common.Common, sessionID string, customCommands []commands.CustomCommand, mcpPrompts []commands.MCPPrompt, isUserMessageSelected bool) (*Commands, error) {
 	c := &Commands{
-		com:            com,
-		selected:       SystemCommands,
-		sessionID:      sessionID,
-		customCommands: customCommands,
-		mcpPrompts:     mcpPrompts,
-		canFork:        canFork,
-		canDelete:      canDelete,
+		com:                   com,
+		selected:              SystemCommands,
+		sessionID:             sessionID,
+		customCommands:        customCommands,
+		mcpPrompts:            mcpPrompts,
+		isUserMessageSelected: isUserMessageSelected,
 	}
 
 	help := help.New()
@@ -392,14 +390,9 @@ func (c *Commands) defaultCommands() []*CommandItem {
 		NewCommandItem(c.com.Styles, "new_session", "New Session", "ctrl+n", ActionNewSession{}),
 	}
 
-	if c.canFork {
+	if c.isUserMessageSelected {
 		commands = append(commands,
 			NewCommandItem(c.com.Styles, "fork_conversation", "Fork Conversation", "", ActionForkConversation{}),
-		)
-	}
-
-	if c.canDelete {
-		commands = append(commands,
 			NewCommandItem(c.com.Styles, "delete_messages", "Delete Messages (including this)", "", ActionDeleteMessages{}),
 		)
 	}

internal/ui/model/ui.go 🔗

@@ -322,6 +322,13 @@ func (m *UI) setState(state uiState, focus uiFocusState) {
 	m.updateLayoutAndSize()
 }
 
+// focusEditor focuses the editor and blurs the chat list.
+func (m *UI) focusEditor() tea.Cmd {
+	m.chat.list.Blur()
+	m.focus = uiFocusEditor
+	return m.textarea.Focus()
+}
+
 // loadCustomCommands loads the custom commands asynchronously.
 func (m *UI) loadCustomCommands() tea.Cmd {
 	return func() tea.Msg {
@@ -1193,9 +1200,7 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
 			}
 		})
 		m.dialog.CloseDialog(dialog.CommandsID)
-		m.chat.list.Blur()
-		m.focus = uiFocusEditor
-		cmds = append(cmds, m.textarea.Focus())
+		cmds = append(cmds, m.focusEditor())
 
 	case dialog.ActionDeleteMessages:
 		if m.isAgentBusy() {
@@ -1228,9 +1233,7 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
 			return m.loadSession(m.session.ID)()
 		})
 		m.dialog.CloseDialog(dialog.CommandsID)
-		m.chat.list.Blur()
-		m.focus = uiFocusEditor
-		cmds = append(cmds, m.textarea.Focus())
+		cmds = append(cmds, m.focusEditor())
 
 	case dialog.ActionSelectModel:
 		if m.isAgentBusy() {
@@ -2789,13 +2792,11 @@ func (m *UI) openModelsDialog() tea.Cmd {
 
 // openCommandsDialog opens the commands dialog.
 func (m *UI) openCommandsDialog() tea.Cmd {
-	canFork := false
-	canDelete := false
+	isUserMessageSelected := false
 	selectedItem := m.chat.SelectedItem()
 	if selectedItem != nil {
 		if _, ok := selectedItem.(*chat.UserMessageItem); ok {
-			canFork = true
-			canDelete = true
+			isUserMessageSelected = true
 		}
 	}
 
@@ -2809,7 +2810,7 @@ func (m *UI) openCommandsDialog() tea.Cmd {
 		sessionID = m.session.ID
 	}
 
-	commands, err := dialog.NewCommands(m.com, sessionID, m.customCommands, m.mcpPrompts, canFork, canDelete)
+	commands, err := dialog.NewCommands(m.com, sessionID, m.customCommands, m.mcpPrompts, isUserMessageSelected)
 	if err != nil {
 		return uiutil.ReportError(err)
 	}