summaryrefslogtreecommitdiff
path: root/internal/tui/clients_projects_box.go
blob: c52e96477aabc94d5b8c355eead8c777da60c70a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package tui

import (
	"fmt"

	"punchcard/internal/queries"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss/v2"
)

// NewClientsProjectsModel creates a new clients/projects model
func NewClientsProjectsModel() ClientsProjectsModel {
	return ClientsProjectsModel{
		selectedIndex:   0,
		selectedIsClient: true,
	}
}

// Update handles messages for the clients/projects box
func (m ClientsProjectsModel) Update(msg tea.Msg) (ClientsProjectsModel, tea.Cmd) {
	return m, nil
}

// View renders the clients/projects box
func (m ClientsProjectsModel) View(width, height int, isSelected bool) string {
	var content string
	
	if len(m.clients) == 0 {
		content = "No clients found\n\nUse 'punch add client' to\nadd your first client."
	} else {
		content = m.renderClientsAndProjects()
	}
	
	// Apply box styling
	style := unselectedBoxStyle
	if isSelected {
		style = selectedBoxStyle
	}
	
	title := "👥 Clients & Projects"
	
	return style.Width(width).Height(height).Render(
		fmt.Sprintf("%s\n\n%s", title, content),
	)
}

// renderClientsAndProjects renders the clients and their projects
func (m ClientsProjectsModel) renderClientsAndProjects() string {
	var content string
	
	// Group projects by client
	projectsByClient := make(map[int64][]queries.ListAllProjectsRow)
	for _, project := range m.projects {
		projectsByClient[project.ClientID] = append(projectsByClient[project.ClientID], project)
	}
	
	// Track the absolute row index for selection highlighting
	absoluteRowIndex := 0
	
	for i, client := range m.clients {
		if i > 0 {
			content += "\n"
		}
		
		// Client name with rate if available
		clientLine := fmt.Sprintf("• %s", client.Name)
		if client.BillableRate.Valid {
			rateInDollars := float64(client.BillableRate.Int64) / 100.0
			clientLine += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
		}
		
		// Highlight if this client is selected
		clientStyle := lipgloss.NewStyle().Bold(true)
		if m.selectedIsClient && m.selectedIndex == i {
			clientStyle = clientStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
		}
		content += clientStyle.Render(clientLine) + "\n"
		absoluteRowIndex++
		
		// Projects for this client
		clientProjects := projectsByClient[client.ID]
		if len(clientProjects) == 0 {
			content += "  └── (no projects)\n"
		} else {
			for j, project := range clientProjects {
				prefix := "├──"
				if j == len(clientProjects)-1 {
					prefix = "└──"
				}
				
				projectLine := fmt.Sprintf("  %s %s", prefix, project.Name)
				if project.BillableRate.Valid {
					rateInDollars := float64(project.BillableRate.Int64) / 100.0
					projectLine += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
				}
				
				// Highlight if this project is selected
				// We need to check against the absolute project index in m.projects
				projectStyle := lipgloss.NewStyle()
				if !m.selectedIsClient {
					// Find this project's index in the m.projects slice
					for k, p := range m.projects {
						if p.ID == project.ID && m.selectedIndex == k {
							projectStyle = projectStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
							break
						}
					}
				}
				content += projectStyle.Render(projectLine) + "\n"
			}
		}
	}
	
	return content
}

// UpdateData updates the clients and projects data
func (m ClientsProjectsModel) UpdateData(clients []queries.Client, projects []queries.ListAllProjectsRow) ClientsProjectsModel {
	m.clients = clients
	m.projects = projects
	// Reset selection if we have new data
	if len(clients) > 0 {
		m.selectedIndex = 0
		m.selectedIsClient = true
	}
	return m
}

// NextSelection moves to the next selectable row
func (m ClientsProjectsModel) NextSelection() ClientsProjectsModel {
	totalRows := m.getTotalSelectableRows()
	if totalRows == 0 {
		return m
	}
	
	currentIndex := m.getCurrentRowIndex()
	if currentIndex < totalRows-1 {
		m.setRowIndex(currentIndex + 1)
	}
	return m
}

// PrevSelection moves to the previous selectable row
func (m ClientsProjectsModel) PrevSelection() ClientsProjectsModel {
	totalRows := m.getTotalSelectableRows()
	if totalRows == 0 {
		return m
	}
	
	currentIndex := m.getCurrentRowIndex()
	if currentIndex > 0 {
		m.setRowIndex(currentIndex - 1)
	}
	return m
}

// getDisplayOrder returns items in the order they are displayed (tree structure)
func (m ClientsProjectsModel) getDisplayOrder() []ProjectsDisplayItem {
	var items []ProjectsDisplayItem
	
	// Group projects by client
	projectsByClient := make(map[int64][]queries.ListAllProjectsRow)
	projectIndexByID := make(map[int64]int)
	for i, project := range m.projects {
		projectsByClient[project.ClientID] = append(projectsByClient[project.ClientID], project)
		projectIndexByID[project.ID] = i
	}
	
	// Build display order: client followed by its projects
	for i, client := range m.clients {
		// Add client
		items = append(items, ProjectsDisplayItem{
			IsClient:    true,
			ClientIndex: i,
			Client:      &client,
		})
		
		// Add projects for this client
		clientProjects := projectsByClient[client.ID]
		for _, project := range clientProjects {
			projectCopy := project // Copy to avoid reference issues
			items = append(items, ProjectsDisplayItem{
				IsClient:     false,
				ClientIndex:  i,
				ProjectIndex: projectIndexByID[project.ID],
				Project:      &projectCopy,
			})
		}
	}
	
	return items
}

// getTotalSelectableRows counts total items in display order
func (m ClientsProjectsModel) getTotalSelectableRows() int {
	return len(m.getDisplayOrder())
}

// getCurrentRowIndex gets the current absolute row index in display order
func (m ClientsProjectsModel) getCurrentRowIndex() int {
	displayOrder := m.getDisplayOrder()
	
	for i, item := range displayOrder {
		if item.IsClient && m.selectedIsClient && item.ClientIndex == m.selectedIndex {
			return i
		}
		if !item.IsClient && !m.selectedIsClient && item.ProjectIndex == m.selectedIndex {
			return i
		}
	}
	
	return 0 // Default to first item if not found
}

// setRowIndex sets the selection to the given absolute row index in display order
func (m *ClientsProjectsModel) setRowIndex(index int) {
	displayOrder := m.getDisplayOrder()
	if index < 0 || index >= len(displayOrder) {
		return
	}
	
	item := displayOrder[index]
	if item.IsClient {
		m.selectedIndex = item.ClientIndex
		m.selectedIsClient = true
	} else {
		m.selectedIndex = item.ProjectIndex
		m.selectedIsClient = false
	}
}

// GetSelectedClient returns the selected client if one is selected
func (m ClientsProjectsModel) GetSelectedClient() *queries.Client {
	if m.selectedIsClient && m.selectedIndex < len(m.clients) {
		return &m.clients[m.selectedIndex]
	}
	return nil
}

// GetSelectedProject returns the selected project if one is selected
func (m ClientsProjectsModel) GetSelectedProject() *queries.ListAllProjectsRow {
	if !m.selectedIsClient && m.selectedIndex < len(m.projects) {
		return &m.projects[m.selectedIndex]
	}
	return nil
}