on main help sign in
fix(sessions): select the current session in dialog
098bc8aKujtim Hoxha
internal/ui/dialog/sessions.go+18−7
@@ -16,11 +16,12 @@ const SessionsID = "session"
16
16
17// Session is a session selector dialog.
17// Session is a session selector dialog.
18type Session struct {
18type Session struct {
19− width, height int
19+ width, height int
20− com *common.Common
20+ com *common.Common
21− help help.Model
21+ help help.Model
22− list *list.FilterableList
22+ list *list.FilterableList
23− input textinput.Model
23+ input textinput.Model
24+ selectedSessionInx int
24
25
25 keyMap struct {
26 keyMap struct {
26 Select key.Binding
27 Select key.Binding
@@ -33,7 +34,7 @@ type Session struct {
33var _ Dialog = (*Session)(nil)
34var _ Dialog = (*Session)(nil)
34
35
35// NewSessions creates a new Session dialog.
36// NewSessions creates a new Session dialog.
36−func NewSessions(com *common.Common) (*Session, error) {
37+func NewSessions(com *common.Common, selectedSessionID string) (*Session, error) {
37 s := new(Session)
38 s := new(Session)
38 s.com = com
39 s.com = com
39 sessions, err := com.App.Sessions.List(context.TODO())
40 sessions, err := com.App.Sessions.List(context.TODO())
@@ -41,13 +42,19 @@ func NewSessions(com *common.Common) (*Session, error) {
41 return nil, err
42 return nil, err
42 }
43 }
43
44
45+ for i, sess := range sessions {
46+ if sess.ID == selectedSessionID {
47+ s.selectedSessionInx = i
48+ break
49+ }
50+ }
51+
44 help := help.New()
52 help := help.New()
45 help.Styles = com.Styles.DialogHelpStyles()
53 help.Styles = com.Styles.DialogHelpStyles()
46
54
47 s.help = help
55 s.help = help
48 s.list = list.NewFilterableList(sessionItems(com.Styles, sessions...)...)
56 s.list = list.NewFilterableList(sessionItems(com.Styles, sessions...)...)
49 s.list.Focus()
57 s.list.Focus()
50− s.list.SetSelected(0)
51
58
52 s.input = textinput.New()
59 s.input = textinput.New()
53 s.input.SetVirtualCursor(false)
60 s.input.SetVirtualCursor(false)
@@ -85,6 +92,10 @@ func (s *Session) SetSize(width, height int) {
85 s.input.SetWidth(innerWidth - t.Dialog.InputPrompt.GetHorizontalFrameSize() - 1) // (1) cursor padding
92 s.input.SetWidth(innerWidth - t.Dialog.InputPrompt.GetHorizontalFrameSize() - 1) // (1) cursor padding
86 s.list.SetSize(innerWidth, height-heightOffset)
93 s.list.SetSize(innerWidth, height-heightOffset)
87 s.help.SetWidth(width)
94 s.help.SetWidth(width)
95+
96+ // Now that we know the height we can select the selected session and scroll to it.
97+ s.list.SetSelected(s.selectedSessionInx)
98+ s.list.ScrollToSelected()
88}
99}
89
100
90// ID implements Dialog.
101// ID implements Dialog.
internal/ui/model/ui.go+6−1
@@ -1440,7 +1440,12 @@ func (m *UI) openSessionsDialog() tea.Cmd {
1440 return nil
1440 return nil
1441 }
1441 }
1442
1442
1443+ selectedSessionID := ""
1444+ if m.session != nil {
1445+ selectedSessionID = m.session.ID
1446+ }
1447+
1443− dialog, err := dialog.NewSessions(m.com)
1448+ dialog, err := dialog.NewSessions(m.com, selectedSessionID)
1444 if err != nil {
1449 if err != nil {
1445 return uiutil.ReportError(err)
1450 return uiutil.ReportError(err)
1446 }
1451 }