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
|
package tui
import (
"fmt"
"strconv"
"punchcard/internal/queries"
"github.com/charmbracelet/lipgloss/v2"
)
type ClientsProjectsModel struct {
clients []queries.Client
projects map[int64][]queries.Project
selectedClient int
selectedProject *int
}
// NewClientsProjectsModel creates a new clients/projects model
func NewClientsProjectsModel() ClientsProjectsModel {
return ClientsProjectsModel{}
}
// 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
absoluteRowIndex := 0
for i, client := range m.clients {
if i > 0 {
content += "\n"
}
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.selectedClient == i && m.selectedProject == nil {
clientStyle = clientStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
}
content += clientStyle.Render(clientLine) + "\n"
absoluteRowIndex++
clientProjects := m.projects[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)
}
projectStyle := lipgloss.NewStyle()
if m.selectedClient == i && m.selectedProject != nil && *m.selectedProject == j {
projectStyle = projectStyle.Background(lipgloss.Color("62")).Foreground(lipgloss.Color("230"))
}
content += projectStyle.Render(projectLine) + "\n"
}
}
}
return content
}
func (m *ClientsProjectsModel) changeSelection(forward bool) {
if forward {
m.changeSelectionForward()
} else {
m.changeSelectionBackward()
}
}
func (m *ClientsProjectsModel) changeSelectionForward() {
selectedClient := m.clients[m.selectedClient]
projects := m.projects[selectedClient.ID]
if m.selectedProject == nil {
// starting with a client selected
if len(projects) > 0 {
// can jump into the first project
var zero int = 0
m.selectedProject = &zero
return
}
// there is no next client - at the bottom, no-op
if m.selectedClient == len(m.clients)-1 {
return
}
// jump to next client
m.selectedClient++
return
}
if *m.selectedProject == len(projects)-1 {
// at last project
if m.selectedClient == len(m.clients)-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() {
selectedClient := m.clients[m.selectedClient]
projects := m.projects[selectedClient.ID]
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 = m.clients[m.selectedClient]
projects = m.projects[selectedClient.ID]
if len(projects) > 0 {
// previous client has projects, jump to last one
i := len(projects) - 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) {
client := m.clients[m.selectedClient]
clientID := strconv.FormatInt(client.ID, 10)
projectID := ""
if m.selectedProject != nil {
project := m.projects[client.ID][*m.selectedProject]
projectID = strconv.FormatInt(project.ID, 10)
}
return clientID, projectID, "", nil
}
|