Skip to content

Commit 403a213

Browse files
committed
feat: show loading indicator in tab name
1 parent 33b51f7 commit 403a213

File tree

6 files changed

+84
-47
lines changed

6 files changed

+84
-47
lines changed

ui/components/issuessection/issuessection.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
142142
currIssue.Assignees.Nodes = removeAssignees(currIssue.Assignees.Nodes, msg.RemovedAssignees.Nodes)
143143
}
144144
m.Issues[i] = currIssue
145-
m.Table.SetIsLoading(false)
145+
m.SetIsLoading(false)
146146
m.Table.SetRows(m.BuildRows())
147147
break
148148
}
@@ -156,7 +156,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
156156
m.Issues = msg.Issues
157157
}
158158
m.TotalCount = msg.TotalCount
159-
m.Table.SetIsLoading(false)
159+
m.SetIsLoading(false)
160160
m.PageInfo = &msg.PageInfo
161161
m.Table.SetRows(m.BuildRows())
162162
m.UpdateLastUpdated(time.Now())
@@ -435,19 +435,16 @@ func (m Model) GetItemPluralForm() string {
435435
return "Issues"
436436
}
437437

438-
func (m Model) GetTotalCount() *int {
439-
if m.IsLoading() {
440-
return nil
441-
}
442-
c := m.TotalCount
443-
return &c
438+
func (m Model) GetTotalCount() int {
439+
return m.TotalCount
444440
}
445441

446-
func (m Model) IsLoading() bool {
447-
return m.Table.IsLoading()
442+
func (m *Model) GetIsLoading() bool {
443+
return m.IsLoading
448444
}
449445

450446
func (m *Model) SetIsLoading(val bool) {
447+
m.IsLoading = val
451448
m.Table.SetIsLoading(val)
452449
}
453450

ui/components/prssection/prssection.go

+7-14
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
171171
currPr.Mergeable = ""
172172
}
173173
m.Prs[i] = currPr
174-
m.Table.SetIsLoading(false)
174+
m.SetIsLoading(false)
175175
m.Table.SetRows(m.BuildRows())
176176
break
177177
}
@@ -186,7 +186,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
186186
}
187187
m.TotalCount = msg.TotalCount
188188
m.PageInfo = &msg.PageInfo
189-
m.Table.SetIsLoading(false)
189+
m.SetIsLoading(false)
190190
m.Table.SetRows(m.BuildRows())
191191
m.Table.UpdateLastUpdated(time.Now())
192192
m.UpdateTotalItemsCount(m.TotalCount)
@@ -448,10 +448,10 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
448448
}
449449
cmds = append(cmds, fetchCmd)
450450

451+
m.IsLoading = true
451452
if isFirstFetch {
452-
m.Table.SetIsLoading(true)
453+
m.SetIsLoading(true)
453454
cmds = append(cmds, m.Table.StartLoadingSpinner())
454-
455455
}
456456

457457
return cmds
@@ -533,19 +533,12 @@ func (m Model) GetItemPluralForm() string {
533533
return "PRs"
534534
}
535535

536-
func (m Model) GetTotalCount() *int {
537-
if m.IsLoading() {
538-
return nil
539-
}
540-
c := m.TotalCount
541-
return &c
542-
}
543-
544-
func (m Model) IsLoading() bool {
545-
return m.Table.IsLoading()
536+
func (m Model) GetTotalCount() int {
537+
return m.TotalCount
546538
}
547539

548540
func (m *Model) SetIsLoading(val bool) {
541+
m.IsLoading = val
549542
m.Table.SetIsLoading(val)
550543
}
551544

ui/components/reposection/reposection.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
174174

175175
case repoMsg:
176176
m.repo = msg.repo
177-
m.Table.SetIsLoading(false)
177+
m.SetIsLoading(false)
178178
m.Table.SetRows(m.BuildRows())
179179
if msg.resetSelection {
180180
m.Table.ResetCurrItem()
@@ -545,16 +545,12 @@ func (m *Model) GetItemPluralForm() string {
545545
return "Branches"
546546
}
547547

548-
func (m *Model) GetTotalCount() *int {
549-
if m.IsLoading() {
550-
return nil
551-
}
552-
553-
c := len(m.Branches)
554-
return &c
548+
func (m *Model) GetTotalCount() int {
549+
return len(m.Branches)
555550
}
556551

557552
func (m *Model) SetIsLoading(val bool) {
553+
m.IsLoading = val
558554
m.Table.SetIsLoading(val)
559555
}
560556

ui/components/section/section.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type BaseModel struct {
4545
IsSearchSupported bool
4646
ShowAuthorIcon bool
4747
IsFilteredByCurrentRemote bool
48+
IsLoading bool
4849
}
4950

5051
type NewSectionOptions struct {
@@ -135,7 +136,7 @@ type Section interface {
135136
GetPagerContent() string
136137
GetItemSingularForm() string
137138
GetItemPluralForm() string
138-
GetTotalCount() *int
139+
GetTotalCount() int
139140
}
140141

141142
type Identifier interface {
@@ -159,7 +160,7 @@ type Table interface {
159160
FetchNextPageSectionRows() []tea.Cmd
160161
BuildRows() []table.Row
161162
ResetRows()
162-
IsLoading() bool
163+
GetIsLoading() bool
163164
SetIsLoading(val bool)
164165
}
165166

@@ -293,6 +294,10 @@ func (m *BaseModel) IsSearchFocused() bool {
293294
return m.IsSearching
294295
}
295296

297+
func (m *BaseModel) GetIsLoading() bool {
298+
return m.IsLoading
299+
}
300+
296301
func (m *BaseModel) SetIsSearching(val bool) tea.Cmd {
297302
m.IsSearching = val
298303
if val {
@@ -414,10 +419,6 @@ func (m *BaseModel) UpdateTotalItemsCount(count int) {
414419
m.Table.UpdateTotalItemsCount(count)
415420
}
416421

417-
func (m *BaseModel) IsLoading() bool {
418-
return m.Table.IsLoading()
419-
}
420-
421422
func (m *BaseModel) GetPromptConfirmation() string {
422423
if m.IsPromptConfirmationShown {
423424
var prompt string

ui/components/tabs/tabs.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/charmbracelet/bubbles/spinner"
78
tea "github.com/charmbracelet/bubbletea"
89
"github.com/charmbracelet/lipgloss"
910

@@ -13,9 +14,15 @@ import (
1314
"github.com/dlvhdr/gh-dash/v4/utils"
1415
)
1516

17+
type SectionState struct {
18+
Count int
19+
IsLoading bool
20+
spinner spinner.Model
21+
}
22+
1623
type Model struct {
1724
sectionsConfigs []config.SectionConfig
18-
sectionCounts []*int
25+
sectionCounts []SectionState
1926
CurrSectionId int
2027
}
2128

@@ -26,16 +33,32 @@ func NewModel(ctx *context.ProgramContext) Model {
2633
}
2734

2835
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
29-
return m, nil
36+
cmds := make([]tea.Cmd, 0)
37+
switch msg := msg.(type) {
38+
case spinner.TickMsg:
39+
for i, s := range m.sectionCounts {
40+
if s.IsLoading {
41+
var cmd tea.Cmd
42+
m.sectionCounts[i].spinner, cmd = s.spinner.Update(msg)
43+
cmds = append(cmds, cmd)
44+
}
45+
}
46+
}
47+
48+
return m, tea.Batch(cmds...)
3049
}
3150

3251
func (m Model) View(ctx *context.ProgramContext) string {
3352
sectionTitles := make([]string, 0, len(m.sectionsConfigs))
3453
for i, section := range m.sectionsConfigs {
3554
title := section.Title
3655
// handle search section
37-
if i > 0 && m.sectionCounts[i] != nil && ctx.Config.Theme.Ui.SectionsShowCount {
38-
title = fmt.Sprintf("%s (%s)", title, utils.ShortNumber(*m.sectionCounts[i]))
56+
if i > 0 {
57+
if m.sectionCounts[i].IsLoading {
58+
title = fmt.Sprintf("%s %s", title, m.sectionCounts[i].spinner.View())
59+
} else {
60+
title = fmt.Sprintf("%s (%s)", title, utils.ShortNumber(m.sectionCounts[i].Count))
61+
}
3962
}
4063
sectionTitles = append(sectionTitles, title)
4164
}
@@ -68,11 +91,29 @@ func (m *Model) SetCurrSectionId(id int) {
6891

6992
func (m *Model) UpdateSectionsConfigs(ctx *context.ProgramContext) {
7093
m.sectionsConfigs = ctx.GetViewSectionsConfig()
94+
m.sectionCounts = make([]SectionState, len(m.sectionsConfigs))
95+
for i := range m.sectionsConfigs {
96+
m.sectionCounts[i] = SectionState{
97+
Count: 0,
98+
IsLoading: false,
99+
spinner: spinner.New(spinner.WithSpinner(spinner.Dot), spinner.WithStyle(lipgloss.NewStyle().Foreground(ctx.Theme.FaintText).PaddingLeft(2))),
100+
}
101+
}
71102
}
72103

73104
func (m *Model) UpdateSectionCounts(sections []section.Section) {
74-
m.sectionCounts = make([]*int, len(sections))
75105
for i, s := range sections {
76-
m.sectionCounts[i] = s.GetTotalCount()
106+
m.sectionCounts[i].Count = s.GetTotalCount()
107+
m.sectionCounts[i].IsLoading = s.GetIsLoading()
77108
}
78109
}
110+
111+
func (m *Model) SetAllLoading() []tea.Cmd {
112+
cmds := make([]tea.Cmd, 0)
113+
for i := range m.sectionCounts {
114+
m.sectionCounts[i].IsLoading = true
115+
cmds = append(cmds, m.sectionCounts[i].spinner.Tick)
116+
}
117+
118+
return cmds
119+
}

ui/ui.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
604604
}
605605
}
606606

607+
m.tabs, tabsCmd = m.tabs.Update(msg)
607608
m.tabs.UpdateSectionCounts(m.getCurrentViewSections())
608609

609610
sectionCmd := m.updateCurrentSection(msg)
@@ -766,15 +767,23 @@ func (m *Model) syncSidebar() tea.Cmd {
766767
}
767768

768769
func (m *Model) fetchAllViewSections() ([]section.Section, tea.Cmd) {
770+
cmds := make([]tea.Cmd, 0)
771+
cmds = append(cmds, m.tabs.SetAllLoading()...)
772+
769773
if m.ctx.View == config.RepoView {
770774
var cmd tea.Cmd
771775
s, cmd := reposection.FetchAllBranches(m.ctx)
776+
cmds = append(cmds, cmd)
772777
m.repo = &s
773-
return nil, cmd
778+
return nil, tea.Batch(cmds...)
774779
} else if m.ctx.View == config.PRsView {
775-
return prssection.FetchAllSections(m.ctx, m.prs)
780+
s, prcmds := prssection.FetchAllSections(m.ctx, m.prs)
781+
cmds = append(cmds, prcmds)
782+
return s, tea.Batch(cmds...)
776783
} else {
777-
return issuessection.FetchAllSections(m.ctx)
784+
s, issuecmds := issuessection.FetchAllSections(m.ctx)
785+
cmds = append(cmds, issuecmds)
786+
return s, tea.Batch(cmds...)
778787
}
779788
}
780789

0 commit comments

Comments
 (0)