fix: return to all emails (#6)

drew created

Change summary

main.go      | 18 +++++++++++++-----
tui/inbox.go | 18 ++++++++++--------
2 files changed, 23 insertions(+), 13 deletions(-)

Detailed changes

main.go 🔗

@@ -18,6 +18,7 @@ type mainModel struct {
 	current tea.Model
 	config  *config.Config
 	emails  []fetcher.Email
+	inbox   *tui.Inbox
 	width   int
 	height  int
 	err     error
@@ -58,9 +59,13 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		if msg.String() == "ctrl+c" {
 			return m, tea.Quit
 		}
-		// Allow ESC to go back to the main menu
+		// Allow ESC to go back
 		if msg.String() == "esc" {
-			if _, ok := m.current.(*tui.Choice); !ok {
+			switch m.current.(type) {
+			case *tui.EmailView:
+				m.current = m.inbox
+				return m, nil
+			case *tui.Inbox, *tui.Composer:
 				m.current = tui.NewChoice()
 				return m, m.current.Init()
 			}
@@ -89,18 +94,21 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 
 	case tui.EmailsFetchedMsg:
 		m.emails = msg.Emails
-		m.current = tui.NewInbox(m.emails)
+		m.inbox = tui.NewInbox(m.emails)
+		m.current = m.inbox
 		// 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())
 
 	case tui.GoToSendMsg:
-		m.current = tui.NewComposer(m.config.Email)
+		composer := tui.NewComposer(m.config.Email)
+		m.current = &composer
 		m.current, _ = m.current.Update(tea.WindowSizeMsg{Width: m.width, Height: m.height})
 		cmds = append(cmds, m.current.Init())
 
 	case tui.ViewEmailMsg:
-		m.current = tui.NewEmailView(m.emails[msg.Index], m.width, m.height)
+		emailView := tui.NewEmailView(m.emails[msg.Index], m.width, m.height)
+		m.current = emailView
 		cmds = append(cmds, m.current.Init())
 
 	case tui.SendEmailMsg:

tui/inbox.go 🔗

@@ -11,8 +11,8 @@ import (
 )
 
 var (
-	paginationStyle   = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
-	inboxHelpStyle    = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
+	paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
+	inboxHelpStyle  = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
 )
 
 type item struct {
@@ -50,7 +50,7 @@ type Inbox struct {
 	list list.Model
 }
 
-func NewInbox(emails []fetcher.Email) Inbox {
+func NewInbox(emails []fetcher.Email) *Inbox {
 	items := make([]list.Item, len(emails))
 	for i, email := range emails {
 		items[i] = item{
@@ -67,14 +67,14 @@ func NewInbox(emails []fetcher.Email) Inbox {
 	l.Styles.PaginationStyle = paginationStyle
 	l.Styles.HelpStyle = inboxHelpStyle
 
-	return Inbox{list: l}
+	return &Inbox{list: l}
 }
 
-func (m Inbox) Init() tea.Cmd {
+func (m *Inbox) Init() tea.Cmd {
 	return nil
 }
 
-func (m Inbox) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
+func (m *Inbox) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	switch msg := msg.(type) {
 	case tea.KeyMsg:
 		if msg.String() == "enter" {
@@ -92,10 +92,12 @@ func (m Inbox) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	}
 
 	var cmd tea.Cmd
-	m.list, cmd = m.list.Update(msg)
+	var newModel list.Model
+	newModel, cmd = m.list.Update(msg)
+	m.list = newModel
 	return m, cmd
 }
 
-func (m Inbox) View() string {
+func (m *Inbox) View() string {
 	return "\n" + m.list.View()
 }