feat(tui): set the textarea value back after closing the editor

Ayman Bagabas created

After closing the editor, the textarea value becomes the text
that was written in the editor. This allows users to edit the text
they wrote in the editor before sending it.

Change summary

internal/tui/components/chat/editor/editor.go | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

Detailed changes

internal/tui/components/chat/editor/editor.go 🔗

@@ -67,6 +67,10 @@ const (
 	maxAttachments = 5
 )
 
+type openEditorMsg struct {
+	Text string
+}
+
 func (m *editorCmp) openEditor(value string) tea.Cmd {
 	editor := os.Getenv("EDITOR")
 	if editor == "" {
@@ -102,11 +106,8 @@ func (m *editorCmp) openEditor(value string) tea.Cmd {
 			return util.ReportWarn("Message is empty")
 		}
 		os.Remove(tmpfile.Name())
-		attachments := m.attachments
-		m.attachments = nil
-		return chat.SendMsg{
-			Text:        string(content),
-			Attachments: attachments,
+		return openEditorMsg{
+			Text: strings.TrimSpace(string(content)),
 		}
 	})
 }
@@ -184,6 +185,9 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			m.completionsStartIndex = 0
 			return m, nil
 		}
+	case openEditorMsg:
+		m.textarea.SetValue(msg.Text)
+		return m, nil
 	case tea.KeyPressMsg:
 		switch {
 		// Completions