package tui import ( "cmp" "context" "slices" "albedo/cli/internal/daemon" tea "charm.land/bubbletea/v2" ) // termsLoadedMsg carries the daemon's sign-ins, for their terms warnings. type termsLoadedMsg struct { err error signIns []daemon.SignIn } // termsSeenMsg says the terms warning id was answered with a message sent. type termsSeenMsg struct{ ID string } // termsSavedMsg is the daemon's answer to saving a dismissed warning. type termsSavedMsg struct { prefs daemon.UIPreferences err error } func (m *Workspace) loadTermsCmd() tea.Cmd { conn := m.Conn if conn == nil { return nil } return func() tea.Msg { listed, err := daemon.SignInList(context.Background(), conn) return termsLoadedMsg{signIns: listed.Logins, err: err} } } func (m *Workspace) handleTerms(msg tea.Msg) (tea.Cmd, bool) { switch msg := msg.(type) { case termsLoadedMsg: // Without the list no warning shows until a login screen reads it. if msg.err == nil { m.signIns = msg.signIns } return nil, true case termsSeenMsg: if !slices.Contains(m.UI.DismissedNotices, msg.ID) { m.UI.DismissedNotices = append(slices.Clip(m.UI.DismissedNotices), msg.ID) } conn, id := m.Conn, msg.ID return func() tea.Msg { prefs, err := daemon.DismissNotice(context.Background(), conn, id) return termsSavedMsg{prefs: prefs, err: err} }, true case termsSavedMsg: if msg.err != nil { m.AddError("Could not save that the terms warning was seen: " + msg.err.Error()) return nil, true } m.ApplyUI(msg.prefs) return nil, true } return nil, false } // termsFor is the terms warning a message on profile still owes: the one its // sign-in carries, while the profile has no API key of its own and the // warning was never dismissed. func (m *Workspace) termsFor(profile string) *daemon.Terms { settings, known := m.Profiles.Providers[cmp.Or(profile, m.Profiles.Active)] if !known || settings.HasKey { return nil } i := slices.IndexFunc(m.signIns, func(s daemon.SignIn) bool { return s.Provider == settings.Extension }) if i < 0 { return nil } terms := m.signIns[i].Terms if terms == nil || slices.Contains(m.UI.DismissedNotices, terms.ID) { return nil } return terms }