Skip to content

Commit d511ed7

Browse files
committed
fix(tui): preserve selection and page on view return
- Correctly preserves the selected item's index and current page when returning from the edit collection view. - Resets the selected item to the first option (index 0) when navigating between pages.
1 parent 45c3ac7 commit d511ed7

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

internal/tui/app/model.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Model struct {
2424
editCollectionView views.EditCollectionView
2525
width int
2626
height int
27+
selectedIndex int
2728
}
2829

2930
func NewModel(ctx *Context) Model {
@@ -66,6 +67,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6667
if m.mode == CollectionsViewMode {
6768
// Get selected collection and switch to edit mode
6869
if selectedItem := m.collectionsView.GetSelectedItem(); selectedItem != nil {
70+
m.selectedIndex = m.collectionsView.GetSelectedIndex()
6971
m.mode = EditCollectionViewMode
7072
m.editCollectionView = views.NewEditCollectionView(m.ctx.Collections, *selectedItem)
7173
return m, nil
@@ -94,6 +96,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
9496
case views.BackToCollectionsMsg:
9597
m.mode = CollectionsViewMode
9698
// Reload collections to show any changes
99+
m.collectionsView.SetSelectedIndex(m.selectedIndex)
97100
return m, m.collectionsView.Init()
98101
case views.EditCollectionMsg:
99102
m.mode = EditCollectionViewMode

internal/tui/components/paginated_list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (pl PaginatedList) SelectedIndex() int {
8181
return pl.list.Index()
8282
}
8383

84+
func (pl *PaginatedList) SetIndex(i int) {
85+
pl.list.Select(i)
86+
}
87+
8488
func (pl PaginatedList) IsFiltering() bool {
8589
return pl.list.FilterState() == list.Filtering
8690
}

internal/tui/views/collections.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type CollectionsView struct {
1717
width int
1818
height int
1919
initialized bool
20+
selectedIndex int
2021

2122
// Backend pagination state
2223
currentPage int
@@ -36,7 +37,22 @@ func (v CollectionsView) Init() tea.Cmd {
3637
}
3738

3839
func (v *CollectionsView) loadCollections() tea.Msg {
39-
return v.loadCollectionsPage(1, 20) // Load first page with 20 items
40+
pageToLoad := v.currentPage
41+
if pageToLoad == 0 {
42+
pageToLoad = 1
43+
}
44+
pageSizeToLoad := v.pageSize
45+
if pageSizeToLoad == 0 {
46+
pageSizeToLoad = 20
47+
}
48+
49+
if v.initialized {
50+
v.selectedIndex = v.list.SelectedIndex()
51+
} else {
52+
v.selectedIndex = 0
53+
}
54+
55+
return v.loadCollectionsPage(pageToLoad, pageSizeToLoad)
4056
}
4157

4258
func (v *CollectionsView) loadCollectionsPage(page, pageSize int) tea.Msg {
@@ -93,6 +109,7 @@ func (v CollectionsView) Update(msg tea.Msg) (CollectionsView, tea.Cmd) {
93109
// Create list with pagination info in title
94110
title := fmt.Sprintf("Collections (Page %d/%d)", v.currentPage, v.pagination.TotalPages)
95111
v.list = components.NewPaginatedList(items, title)
112+
v.list.SetIndex(v.selectedIndex)
96113

97114
if v.width > 0 && v.height > 0 {
98115
contentHeight := v.height - 4
@@ -115,6 +132,7 @@ func (v CollectionsView) Update(msg tea.Msg) (CollectionsView, tea.Cmd) {
115132
case "n", "right":
116133
// Next page
117134
if v.currentPage < v.pagination.TotalPages {
135+
v.selectedIndex = 0 // Reset selection on page change
118136
return v, func() tea.Msg {
119137
return v.loadCollectionsPage(v.currentPage+1, v.pageSize)
120138
}
@@ -123,6 +141,7 @@ func (v CollectionsView) Update(msg tea.Msg) (CollectionsView, tea.Cmd) {
123141
case "p", "left":
124142
// Previous page
125143
if v.currentPage > 1 {
144+
v.selectedIndex = 0 // Reset selection on page change
126145
return v, func() tea.Msg {
127146
return v.loadCollectionsPage(v.currentPage-1, v.pageSize)
128147
}
@@ -147,6 +166,13 @@ func (v CollectionsView) IsFiltering() bool {
147166
return v.initialized && v.list.IsFiltering()
148167
}
149168

169+
func (v *CollectionsView) SetSelectedIndex(index int) {
170+
v.selectedIndex = index
171+
if v.initialized {
172+
v.list.SetIndex(index)
173+
}
174+
}
175+
150176
func (v CollectionsView) GetSelectedItem() *collections.CollectionEntity {
151177
if !v.initialized {
152178
return nil
@@ -160,6 +186,10 @@ func (v CollectionsView) GetSelectedItem() *collections.CollectionEntity {
160186
return nil
161187
}
162188

189+
func (v CollectionsView) GetSelectedIndex() int {
190+
return v.list.SelectedIndex()
191+
}
192+
163193
func (v CollectionsView) View() string {
164194
if !v.initialized {
165195
return v.layout.FullView(

0 commit comments

Comments
 (0)