summaryrefslogtreecommitdiff
path: root/internal/tui/projects_box.go
blob: cd50d5e949043e9e847fd03634d349d862f64cb1 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package tui

import (
	"fmt"
	"strconv"

	"git.tjp.lol/punchcard/internal/queries"

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

type ClientsProjectsModel struct {
	clients         []queries.Client
	projects        map[int64][]queries.Project
	selectedClient  int
	selectedProject *int
	showArchived    bool
}

// NewClientsProjectsModel creates a new clients/projects model
func NewClientsProjectsModel() ClientsProjectsModel {
	return ClientsProjectsModel{}
}

// visibleClients returns the list of clients that should be displayed
func (m ClientsProjectsModel) visibleClients() []queries.Client {
	if m.showArchived {
		return m.clients
	}

	visible := make([]queries.Client, 0, len(m.clients))
	for _, client := range m.clients {
		if client.Archived == 0 {
			visible = append(visible, client)
		}
	}
	return visible
}

// visibleProjects returns the list of projects for a client that should be displayed
func (m ClientsProjectsModel) visibleProjects(clientID int64) []queries.Project {
	allProjects := m.projects[clientID]
	if m.showArchived {
		return allProjects
	}

	visible := make([]queries.Project, 0, len(allProjects))
	for _, project := range allProjects {
		if project.Archived == 0 {
			visible = append(visible, project)
		}
	}
	return visible
}

// View renders the clients/projects box
func (m ClientsProjectsModel) View(width, height int, isSelected bool) string {
	var content string

	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		if len(m.clients) == 0 {
			content = inactiveTimerStyle.Render("No clients found\n\nUse 'punch add client' to\nadd your first client.")
		} else {
			content = inactiveTimerStyle.Render("All clients archived\n\nPress '.' to show archived")
		}
	} else {
		content = m.renderClientsAndProjects()
	}

	// Apply box styling
	style := unselectedBoxStyle
	if isSelected {
		style = selectedBoxStyle
	}

	title := titleStyle.Render("👥 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
	visibleClients := m.visibleClients()

	for i, client := range visibleClients {
		if i > 0 {
			content += "\n"
		}

		// Build client name and rate
		clientText := client.Name
		if client.BillableRate.Valid {
			rateInDollars := float64(client.BillableRate.Int64) / 100.0
			clientText += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
		}

		// Style for client text
		clientStyle := lipgloss.NewStyle().Bold(true)
		if m.selectedClient == i && m.selectedProject == nil {
			clientStyle = clientStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
		} else if client.Archived != 0 {
			// Gray out archived clients
			clientStyle = clientStyle.Foreground(lipgloss.Color("246"))
		}

		content += "• " + clientStyle.Render(clientText) + "\n"

		visibleProjects := m.visibleProjects(client.ID)
		if len(visibleProjects) == 0 {
			content += "  └── (no projects)\n"
		} else {
			for j, project := range visibleProjects {
				prefix := "├──"
				if j == len(visibleProjects)-1 {
					prefix = "└──"
				}

				// Build project name and rate
				projectText := project.Name
				if project.BillableRate.Valid {
					rateInDollars := float64(project.BillableRate.Int64) / 100.0
					projectText += fmt.Sprintf(" ($%.2f/hr)", rateInDollars)
				}

				// Style for project text
				projectStyle := lipgloss.NewStyle()
				if m.selectedClient == i && m.selectedProject != nil && *m.selectedProject == j {
					projectStyle = projectStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
				} else if project.Archived != 0 {
					// Gray out archived projects
					projectStyle = projectStyle.Foreground(lipgloss.Color("246"))
				}

				content += fmt.Sprintf("  %s ", prefix) + projectStyle.Render(projectText) + "\n"
			}
		}
	}

	return content
}

func (m *ClientsProjectsModel) changeSelection(forward bool) {
	if forward {
		m.changeSelectionForward()
	} else {
		m.changeSelectionBackward()
	}
}

func (m *ClientsProjectsModel) changeSelectionForward() {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		return
	}

	selectedClient := visibleClients[m.selectedClient]
	visibleProjects := m.visibleProjects(selectedClient.ID)

	if m.selectedProject == nil {
		// starting with a client selected
		if len(visibleProjects) > 0 {
			// can jump into the first project
			zero := 0
			m.selectedProject = &zero
			return
		}

		// there is no next client - at the bottom, no-op
		if m.selectedClient == len(visibleClients)-1 {
			return
		}

		// jump to next client
		m.selectedClient++
		return
	}

	if *m.selectedProject == len(visibleProjects)-1 {
		// at last project

		if m.selectedClient == len(visibleClients)-1 {
			// also at last client - at the bottom, no-op
			return
		}

		// jump to next client, no project
		m.selectedClient++
		m.selectedProject = nil
		return
	}

	// jump to next project
	*m.selectedProject++
}

func (m *ClientsProjectsModel) changeSelectionBackward() {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		return
	}

	selectedClient := visibleClients[m.selectedClient]

	if m.selectedProject == nil {
		// starting with a client selected
		if m.selectedClient == 0 {
			// at first client - at the start, no-op
			return
		}

		m.selectedClient--
		selectedClient = visibleClients[m.selectedClient]
		visibleProjects := m.visibleProjects(selectedClient.ID)

		if len(visibleProjects) > 0 {
			// previous client has projects, jump to last one
			i := len(visibleProjects) - 1
			m.selectedProject = &i
		}

		// otherwise selectedProject is already nil
		return
	}

	if *m.selectedProject == 0 {
		// selected first project - jump up to client
		m.selectedProject = nil
		return
	}

	// otherwise, jump to previous project in same client
	*m.selectedProject--
}

func (m ClientsProjectsModel) selection() (string, string, string, *float64) {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		return "", "", "", nil
	}

	client := visibleClients[m.selectedClient]
	clientID := strconv.FormatInt(client.ID, 10)

	projectID := ""
	if m.selectedProject != nil {
		visibleProjects := m.visibleProjects(client.ID)
		project := visibleProjects[*m.selectedProject]
		projectID = strconv.FormatInt(project.ID, 10)
	}

	return clientID, projectID, "", nil
}

// resetSelection clamps the selection to valid bounds after visibility changes
func (m *ClientsProjectsModel) resetSelection() {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		m.selectedClient = 0
		m.selectedProject = nil
		return
	}

	// Clamp client selection
	if m.selectedClient >= len(visibleClients) {
		m.selectedClient = len(visibleClients) - 1
	}

	// Clamp project selection
	if m.selectedProject != nil {
		client := visibleClients[m.selectedClient]
		visibleProjects := m.visibleProjects(client.ID)
		if len(visibleProjects) == 0 {
			m.selectedProject = nil
		} else if *m.selectedProject >= len(visibleProjects) {
			i := len(visibleProjects) - 1
			m.selectedProject = &i
		}
	}
}

// getSelectedIDs returns the currently selected client ID and optional project ID
func (m ClientsProjectsModel) getSelectedIDs() (clientID int64, projectID *int64) {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		return 0, nil
	}
	if m.selectedClient >= len(visibleClients) {
		return 0, nil
	}

	client := visibleClients[m.selectedClient]
	clientID = client.ID

	if m.selectedProject != nil {
		visibleProjects := m.visibleProjects(client.ID)
		if *m.selectedProject >= len(visibleProjects) {
			return clientID, nil
		}
		project := visibleProjects[*m.selectedProject]
		projectID = &project.ID
	}

	return clientID, projectID
}

// restoreSelection tries to restore selection to the given IDs, or resets to top if not found
func (m *ClientsProjectsModel) restoreSelection(clientID int64, projectID *int64) {
	visibleClients := m.visibleClients()
	if len(visibleClients) == 0 {
		m.selectedClient = 0
		m.selectedProject = nil
		return
	}

	// Try to find the client
	clientFound := false
	for i, client := range visibleClients {
		if client.ID == clientID {
			m.selectedClient = i
			clientFound = true

			// Try to find the project if one was selected
			if projectID != nil {
				visibleProjects := m.visibleProjects(client.ID)
				for j, project := range visibleProjects {
					if project.ID == *projectID {
						m.selectedProject = &j
						return
					}
				}
				// Project not found, select client only
				m.selectedProject = nil
				return
			}

			// No project was selected, just client
			m.selectedProject = nil
			return
		}
	}

	// Client not found, reset to top
	if !clientFound {
		m.selectedClient = 0
		m.selectedProject = nil
	}
}