fix: bring the login page back

drew created

Change summary

main.go         | 30 +++++++++++++++++++++++++++---
tui/login.go    | 22 ++++++++++++++++------
tui/messages.go |  2 ++
3 files changed, 45 insertions(+), 9 deletions(-)

Detailed changes

main.go 🔗

@@ -25,6 +25,12 @@ type mainModel struct {
 
 // newInitialModel now returns a pointer, which is crucial for state management.
 func newInitialModel(cfg *config.Config) *mainModel {
+	// If config is nil, it means we're starting from the login screen.
+	if cfg == nil {
+		return &mainModel{
+			current: tui.NewLogin(),
+		}
+	}
 	return &mainModel{
 		current: tui.NewChoice(),
 		config:  cfg,
@@ -61,6 +67,22 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		}
 
 	// --- Custom Messages for switching views ---
+	case tui.Credentials: // This is a struct, not a pointer
+		cfg := &config.Config{
+			ServiceProvider: msg.Provider,
+			Name:            msg.Name,
+			Email:           msg.Email,
+			Password:        msg.Password,
+		}
+		if err := config.SaveConfig(cfg); err != nil {
+			// TODO: Show an error message to the user
+			log.Printf("could not save config: %v", err)
+			return m, tea.Quit
+		}
+		m.config = cfg
+		m.current = tui.NewChoice()
+		cmds = append(cmds, m.current.Init())
+
 	case tui.GoToInboxMsg:
 		m.current = tui.NewStatus("Fetching emails...")
 		return m, tea.Batch(m.current.Init(), fetchEmails(m.config))
@@ -127,13 +149,15 @@ func fetchEmails(cfg *config.Config) tea.Cmd {
 
 func main() {
 	cfg, err := config.LoadConfig()
+	var initialModel *mainModel
 	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)
+		initialModel = newInitialModel(nil)
+	} else {
+		initialModel = newInitialModel(cfg)
 	}
 
-	p := tea.NewProgram(newInitialModel(cfg), tea.WithAltScreen())
+	p := tea.NewProgram(initialModel, tea.WithAltScreen())
 
 	if _, err := p.Run(); err != nil {
 		fmt.Printf("Alas, there's been an error: %v", err)

tui/login.go 🔗

@@ -12,10 +12,10 @@ type Login struct {
 	inputs     []textinput.Model
 }
 
-// NewLogin creates a new login model. This function was missing.
+// NewLogin creates a new login model.
 func NewLogin() tea.Model {
 	m := Login{
-		inputs: make([]textinput.Model, 2),
+		inputs: make([]textinput.Model, 4), // Increased to 4 for provider, name, email, and password
 	}
 
 	var t textinput.Model
@@ -26,10 +26,16 @@ func NewLogin() tea.Model {
 
 		switch i {
 		case 0:
-			t.Placeholder = "Email"
+			t.Placeholder = "Provider (gmail or icloud)"
 			t.Focus()
-			t.Prompt = "✉️ > "
+			t.Prompt = "☁️ > "
 		case 1:
+			t.Placeholder = "Name"
+			t.Prompt = "👤 > "
+		case 2:
+			t.Placeholder = "Email"
+			t.Prompt = "✉️ > "
+		case 3:
 			t.Placeholder = "Password"
 			t.EchoMode = textinput.EchoPassword
 			t.Prompt = "🔑 > "
@@ -55,8 +61,10 @@ func (m Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			if m.focusIndex == len(m.inputs)-1 {
 				return m, func() tea.Msg {
 					return Credentials{
-						Email:    m.inputs[0].Value(),
-						Password: m.inputs[1].Value(),
+						Provider: m.inputs[0].Value(),
+						Name:     m.inputs[1].Value(),
+						Email:    m.inputs[2].Value(),
+						Password: m.inputs[3].Value(),
 					}
 				}
 			}
@@ -103,6 +111,8 @@ func (m Login) View() string {
 		"Please enter your credentials.",
 		m.inputs[0].View(),
 		m.inputs[1].View(),
+		m.inputs[2].View(),
+		m.inputs[3].View(),
 		helpStyle.Render("\nenter: submit • tab: next field • esc: quit"),
 	)
 }

tui/messages.go 🔗

@@ -16,6 +16,8 @@ type SendEmailMsg struct {
 
 // A message to indicate that the user has entered their credentials.
 type Credentials struct {
+	Provider string
+	Name     string
 	Email    string
 	Password string
 }