diff options
Diffstat (limited to 'internal/tui/app.go')
-rw-r--r-- | internal/tui/app.go | 65 |
1 files changed, 56 insertions, 9 deletions
diff --git a/internal/tui/app.go b/internal/tui/app.go index 6765cd1..34310bd 100644 --- a/internal/tui/app.go +++ b/internal/tui/app.go @@ -192,6 +192,13 @@ func (m AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case openCreateProjectModal: m.modalBox.activateCreateProjectModal(m) + case openHistoryFilterModal: + m.openHistoryFilterModal() + + case updateHistoryFilter: + m.historyBox.filter = HistoryFilter(msg) + cmds = append(cmds, m.refreshCmd) + } return m, tea.Batch(cmds...) @@ -199,33 +206,73 @@ func (m AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *AppModel) openEntryEditor() { m.modalBox.activate(ModalTypeEntry, m.historyBox.selectedEntry().ID) - f := m.modalBox.form - f.fields[0].Focus() + m.modalBox.form.fields[0].Focus() entry := m.historyBox.selectedEntry() - f.fields[0].SetValue(entry.StartTime.Local().Format(time.DateTime)) + m.modalBox.form.fields[0].SetValue(entry.StartTime.Local().Format(time.DateTime)) if entry.EndTime.Valid { - f.fields[1].SetValue(entry.EndTime.Time.Local().Format(time.DateTime)) + m.modalBox.form.fields[1].SetValue(entry.EndTime.Time.Local().Format(time.DateTime)) } for _, client := range m.projectsBox.clients { if client.ID == entry.ClientID { - f.fields[2].SetValue(client.Name) + m.modalBox.form.fields[2].SetValue(client.Name) break } } if entry.ProjectID.Valid { for _, project := range m.projectsBox.projects[entry.ClientID] { if project.ID == entry.ProjectID.Int64 { - f.fields[3].SetValue(project.Name) + m.modalBox.form.fields[3].SetValue(project.Name) break } } } if entry.Description.Valid { - f.fields[4].SetValue(entry.Description.String) + m.modalBox.form.fields[4].SetValue(entry.Description.String) } if entry.BillableRate.Valid { - f.fields[5].SetValue(fmt.Sprintf("%.2f", float64(entry.BillableRate.Int64)/100)) + m.modalBox.form.fields[5].SetValue(fmt.Sprintf("%.2f", float64(entry.BillableRate.Int64)/100)) + } +} + +func (m *AppModel) openHistoryFilterModal() { + m.modalBox.activate(ModalTypeHistoryFilter, 0) + m.modalBox.form.fields[0].Focus() + + // Pre-populate form with current filter values + filter := m.historyBox.filter + + // Set date range based on current filter + var dateRangeStr string + if filter.EndDate == nil { + // Use "since <date>" format for open-ended ranges + dateRangeStr = fmt.Sprintf("since %s", filter.StartDate.Format("2006-01-02")) + } else { + // Use "YYYY-MM-DD to YYYY-MM-DD" format for bounded ranges + dateRangeStr = fmt.Sprintf("%s to %s", filter.StartDate.Format("2006-01-02"), filter.EndDate.Format("2006-01-02")) + } + m.modalBox.form.fields[0].SetValue(dateRangeStr) + + // Set client filter if present + if filter.ClientID != nil { + for _, client := range m.projectsBox.clients { + if client.ID == *filter.ClientID { + m.modalBox.form.fields[1].SetValue(client.Name) + break + } + } + } + + // Set project filter if present + if filter.ProjectID != nil { + for _, clientProjects := range m.projectsBox.projects { + for _, project := range clientProjects { + if project.ID == *filter.ProjectID { + m.modalBox.form.fields[2].SetValue(project.Name) + break + } + } + } } } @@ -286,7 +333,7 @@ type dataUpdatedMsg struct { // refreshCmd is a command to update all app data func (m AppModel) refreshCmd() tea.Msg { - timerInfo, stats, clients, projects, entries, err := getAppData(m.ctx, m.queries) + timerInfo, stats, clients, projects, entries, err := getAppData(m.ctx, m.queries, m.historyBox.filter) if err != nil { msg := dataUpdatedMsg{} msg.err = err |