diff --git a/main.go b/main.go index 8243d30ccb569fe447ec6aa1817d2ea376a5df0d..7f4ee3ea7d1f90d2575be5c1e9888dd53390a951 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "time" "github.com/andrinoff/email-cli/config" + "github.com/andrinoff/email-cli/fetcher" "github.com/andrinoff/email-cli/sender" "github.com/andrinoff/email-cli/tui" tea "github.com/charmbracelet/bubbletea" @@ -16,6 +17,7 @@ import ( type mainModel struct { current tea.Model config *config.Config + emails []fetcher.Email width int height int err error @@ -60,7 +62,12 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // --- Custom Messages for switching views --- case tui.GoToInboxMsg: - m.current = tui.NewInbox() + m.current = tui.NewStatus("Fetching emails...") + return m, tea.Batch(m.current.Init(), fetchEmails(m.config)) + + case tui.EmailsFetchedMsg: + m.emails = msg.Emails + m.current = tui.NewInbox(m.emails) // Manually set the size of the new view m.current, _ = m.current.Update(tea.WindowSizeMsg{Width: m.width, Height: m.height}) cmds = append(cmds, m.current.Init()) @@ -71,14 +78,14 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, m.current.Init()) case tui.ViewEmailMsg: - m.current = tui.NewEmailView(msg.Email, m.width, m.height) + m.current = tui.NewEmailView(m.emails[msg.Index], m.width, m.height) cmds = append(cmds, m.current.Init()) case tui.SendEmailMsg: m.current = tui.NewStatus("Sending email...") cmds = append(cmds, m.current.Init(), sendEmail(m.config, msg)) - case tui.EmailSentMsg: + case tui.EmailResultMsg: m.current = tui.NewChoice() cmds = append(cmds, m.current.Init()) } @@ -101,15 +108,28 @@ func sendEmail(cfg *config.Config, msg tui.SendEmailMsg) tea.Cmd { err := sender.SendEmail(cfg, recipients, msg.Subject, msg.Body) if err != nil { log.Printf("Failed to send email: %v", err) // Log error + return tui.EmailResultMsg{Err: err} } time.Sleep(1 * time.Second) // Give user time to see the "Sending" message - return tui.EmailSentMsg{} + return tui.EmailResultMsg{} + } +} + +func fetchEmails(cfg *config.Config) tea.Cmd { + return func() tea.Msg { + emails, err := fetcher.FetchEmails(cfg) + if err != nil { + return tui.FetchErr(err) + } + return tui.EmailsFetchedMsg{Emails: emails} } } func main() { cfg, err := config.LoadConfig() if err != nil { + // If the config doesn't exist, we can guide the user to create one. + // For now, we'll just log a fatal error. log.Fatalf("could not load config: %v", err) } diff --git a/tui/choice.go b/tui/choice.go index 9463323999e27a7abbb976ccf6fada374c1fac26..31197ef9e3470f2af9a1d9fa31f51b7224717e8b 100644 --- a/tui/choice.go +++ b/tui/choice.go @@ -44,6 +44,12 @@ func (m Choice) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.cursor < 1 { m.cursor++ } + case "enter": + if m.cursor == 0 { + return m, func() tea.Msg { return GoToInboxMsg{} } + } else if m.cursor == 1 { + return m, func() tea.Msg { return GoToSendMsg{} } + } } } return m, nil diff --git a/tui/inbox.go b/tui/inbox.go index cf46f70e05e9f70f4364c6ddc0f2f2e45f3af8b3..f6e9c2d73e47b2a1225a6accd22a81b57959ad31 100644 --- a/tui/inbox.go +++ b/tui/inbox.go @@ -14,7 +14,7 @@ var ( itemStyle = lipgloss.NewStyle().PaddingLeft(4) selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("205")) paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4) - inboxHelpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1) + inboxHelpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1) ) type item struct { @@ -38,7 +38,6 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list str := fmt.Sprintf("%d. %s", index+1, i.title) - // **FIX**: Corrected the function literal to accept a variadic string fn := itemStyle.Render if index == m.Index() { fn = func(s ...string) string { @@ -65,7 +64,7 @@ func NewInbox(emails []fetcher.Email) Inbox { l := list.New(items, itemDelegate{}, 20, 14) l.Title = "Inbox" l.SetShowStatusBar(false) - l.SetFilteringEnabled(false) + l.SetFilteringEnabled(true) l.Styles.Title = lipgloss.NewStyle().Foreground(lipgloss.Color("205")).Bold(true) l.Styles.PaginationStyle = paginationStyle l.Styles.HelpStyle = inboxHelpStyle diff --git a/tui/messages.go b/tui/messages.go index 9adac733b6fc910c24c1648f9f02d5921568bd43..71dd2d9ae6d7de1889deeabe24b791a064b71e7a 100644 --- a/tui/messages.go +++ b/tui/messages.go @@ -40,4 +40,10 @@ type EmailsFetchedMsg struct { } // A message to indicate that an error occurred while fetching emails. -type FetchErr error \ No newline at end of file +type FetchErr error + +// A message to navigate to the inbox view. +type GoToInboxMsg struct{} + +// A message to navigate to the composer view. +type GoToSendMsg struct{} \ No newline at end of file