summaryrefslogtreecommitdiff
path: root/internal/ui/editor_handlers.go
blob: 8f8142034acd32a455cad14e05184f6c1767bf68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ui

import (
	"fmt"
	"os"
	"strings"
	"time"

	tea "charm.land/bubbletea/v2"

	"codeberg.org/snonux/tasksamurai/internal/task"
)

// handleEditDone handles completion of external editor
func (m *Model) handleEditDone(msg editDoneMsg) (tea.Model, tea.Cmd) {
	if msg.err != nil {
		m.showError(fmt.Errorf("editor: %w", msg.err))
	}
	if m.showUltra {
		m.ultraFocusedID = m.editID
	}
	if !m.reloadAndReport() {
		m.editID = 0
		return m, nil
	}
	cmd := m.startBlink(m.editID, false)
	m.editID = 0
	return m, cmd
}

// handleDescEditDone handles the completion of description editing
func (m *Model) handleDescEditDone(msg descEditDoneMsg) (tea.Model, tea.Cmd) {
	m.detailDescEditing = false
	if msg.tempFile != "" {
		defer func() { _ = os.Remove(msg.tempFile) }()
	}

	if msg.err != nil {
		m.statusMsg = fmt.Sprintf("Edit error: %v", msg.err)
		cmd := tea.Tick(2*time.Second, func(time.Time) tea.Msg {
			return struct{ clearStatus bool }{true}
		})
		return m, cmd
	}

	// Read the edited content
	content, err := os.ReadFile(msg.tempFile)
	if err != nil {
		m.statusMsg = fmt.Sprintf("Error reading file: %v", err)
		cmd := tea.Tick(2*time.Second, func(time.Time) tea.Msg {
			return struct{ clearStatus bool }{true}
		})
		return m, cmd
	}

	// Update the description
	newDesc := strings.TrimSpace(string(content))
	if m.currentTaskDetail != nil {
		err = task.SetDescription(m.currentTaskDetail.ID, newDesc)
		if err != nil {
			m.statusMsg = fmt.Sprintf("Error updating description: %v", err)
			cmd := tea.Tick(2*time.Second, func(time.Time) tea.Msg {
				return struct{ clearStatus bool }{true}
			})
			return m, cmd
		}

		// Reload and start blinking
		if !m.reloadAndReport() {
			return m, nil
		}
		return m, m.startDetailBlink(m.detailDescriptionFieldIndex())
	}

	return m, nil
}