Skip to content

Commit 3a3c79b

Browse files
committed
fix: delete popup when no topic selected
1 parent 2dbc756 commit 3a3c79b

5 files changed

Lines changed: 55 additions & 21 deletions

File tree

tests/keys.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func (k *KeyBoard) F5() *KeyBoard {
118118
return k
119119
}
120120

121+
func (k *KeyBoard) F2() *KeyBoard {
122+
k.view.Update(Key(tea.KeyF2))
123+
return k
124+
}
125+
121126
func NewKeyboard(view ui.View) *KeyBoard {
122127
return &KeyBoard{
123128
view: view,

ui/pages/nav/nav.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ type LoadTopicsPageMsg struct {
1111

1212
type LoadCreateTopicPageMsg struct{}
1313

14-
type LoadTopicConfigPageMsg struct{}
14+
type LoadTopicConfigPageMsg struct {
15+
Topic string
16+
}
1517

1618
type LoadPublishPageMsg struct {
1719
Topic *kadmin.ListedTopic

ui/pages/topics_page/topics_page.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ package topics_page
33
import (
44
"context"
55
"fmt"
6-
"github.com/charmbracelet/bubbles/spinner"
7-
"github.com/charmbracelet/bubbles/table"
8-
tea "github.com/charmbracelet/bubbletea"
9-
"github.com/charmbracelet/lipgloss"
10-
"github.com/charmbracelet/log"
116
"ktea/kadmin"
127
"ktea/kontext"
138
"ktea/styles"
@@ -24,6 +19,12 @@ import (
2419
"sort"
2520
"strconv"
2621
"strings"
22+
23+
"github.com/charmbracelet/bubbles/spinner"
24+
"github.com/charmbracelet/bubbles/table"
25+
tea "github.com/charmbracelet/bubbletea"
26+
"github.com/charmbracelet/lipgloss"
27+
"github.com/charmbracelet/log"
2728
)
2829

2930
const name = "topics-page"
@@ -98,10 +99,11 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
9899
case "ctrl+n":
99100
return ui.PublishMsg(nav.LoadCreateTopicPageMsg{})
100101
case "ctrl+o":
101-
if m.SelectedTopic() == nil {
102+
topic := m.SelectedTopic()
103+
if topic == nil {
102104
return nil
103105
}
104-
return ui.PublishMsg(nav.LoadTopicConfigPageMsg{})
106+
return ui.PublishMsg(nav.LoadTopicConfigPageMsg{Topic: topic.Name})
105107
case "ctrl+p":
106108
if m.SelectedTopic() == nil {
107109
return nil
@@ -143,7 +145,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
143145
}
144146
case spinner.TickMsg:
145147
selectedTopic := m.SelectedTopicName()
146-
_, c := m.tcb.Update(msg, &selectedTopic)
148+
_, c := m.tcb.Update(msg, selectedTopic)
147149
if c != nil {
148150
cmds = append(cmds, c)
149151
}
@@ -165,7 +167,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
165167

166168
var cmd tea.Cmd
167169
name := m.SelectedTopicName()
168-
msg, cmd = m.tcb.Update(msg, &name)
170+
msg, cmd = m.tcb.Update(msg, name)
169171
m.tableFocussed = !m.tcb.IsFocussed()
170172
cmds = append(cmds, cmd)
171173

@@ -254,20 +256,19 @@ func (m *Model) createRows() []table.Row {
254256
func (m *Model) SelectedTopic() *kadmin.ListedTopic {
255257
selectedTopic := m.SelectedTopicName()
256258
for _, t := range m.topics {
257-
if t.Name == selectedTopic {
259+
if selectedTopic != nil && t.Name == *selectedTopic {
258260
return &t
259261
}
260262
}
261263
return nil
262264
}
263265

264-
func (m *Model) SelectedTopicName() string {
266+
func (m *Model) SelectedTopicName() *string {
265267
selectedRow := m.table.SelectedRow()
266-
var selectedTopic string
267268
if selectedRow != nil {
268-
selectedTopic = selectedRow[0]
269+
return &selectedRow[0]
269270
}
270-
return selectedTopic
271+
return nil
271272
}
272273

273274
func (m *Model) Title() string {

ui/pages/topics_page/topics_page_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package topics_page
22

33
import (
44
"fmt"
5-
tea "github.com/charmbracelet/bubbletea"
6-
"github.com/stretchr/testify/assert"
75
"ktea/kadmin"
86
"ktea/tests"
97
"ktea/ui/tabs"
108
"strings"
119
"testing"
10+
11+
tea "github.com/charmbracelet/bubbletea"
12+
"github.com/stretchr/testify/assert"
1213
)
1314

1415
func TestTopicsPage(t *testing.T) {
@@ -435,4 +436,28 @@ func TestTopicsPage(t *testing.T) {
435436
page.View(tests.NewKontext(), tests.Renderer)
436437
assert.Equal(t, 3, page.hiddenInternalTopicsCount)
437438
})
439+
440+
t.Run("Do not show deletion cmdbar when no topic is selected", func(t *testing.T) {
441+
page, _ := New(
442+
kadmin.NewMockKadmin(),
443+
tabs.NewMockTopicsTabNavigator(),
444+
)
445+
446+
kb := tests.NewKeyboard(page)
447+
kb.F2()
448+
449+
render := page.View(tests.NewKontext(), tests.Renderer)
450+
assert.NotContains(t, render, "will be deleted permanently")
451+
})
452+
453+
t.Run("Do not navigate to topic config page when no topic is selected", func(t *testing.T) {
454+
page, _ := New(
455+
kadmin.NewMockKadmin(),
456+
tabs.NewMockTopicsTabNavigator(),
457+
)
458+
459+
cmd := page.Update(tests.Key(tea.KeyCtrlO))
460+
461+
assert.Nil(t, cmd)
462+
})
438463
}

ui/tabs/topics_tab/topics_tab.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package topics_tab
22

33
import (
44
"context"
5-
tea "github.com/charmbracelet/bubbletea"
6-
"github.com/charmbracelet/lipgloss"
7-
"github.com/charmbracelet/log"
85
"ktea/kadmin"
96
"ktea/kontext"
107
"ktea/ui"
@@ -21,6 +18,10 @@ import (
2118
"ktea/ui/pages/topics_page"
2219
"ktea/ui/tabs"
2320
"reflect"
21+
22+
tea "github.com/charmbracelet/bubbletea"
23+
"github.com/charmbracelet/lipgloss"
24+
"github.com/charmbracelet/log"
2425
)
2526

2627
type Model struct {
@@ -66,7 +67,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
6667
m.active = m.topicsPage
6768

6869
case nav.LoadTopicConfigPageMsg:
69-
page, cmd := configs_page.New(m.ka, m.ka, m.topicsPage.SelectedTopicName())
70+
page, cmd := configs_page.New(m.ka, m.ka, msg.Topic)
7071
cmds = append(cmds, cmd)
7172
m.active = page
7273

0 commit comments

Comments
 (0)