- commit
- 0417ee1
- parent
- 33c165a
- author
- Eric Bower
- date
- 2025-03-19 15:53:58 -0400 EDT
fix(tui): add loading states for analytics and tuns
2 files changed,
+38,
-11
+30,
-10
1@@ -20,15 +20,17 @@ type SiteStatsLoaded struct{}
2 type AnalyticsPage struct {
3 shared *SharedModel
4
5- sites []*db.VisitUrl
6- features []*db.FeatureFlag
7- err error
8- stats map[string]*db.SummaryVisits
9- selected string
10- interval string
11- focus string
12- leftPane list.Dynamic
13- rightPane *Pager
14+ loadingSites bool
15+ loadingDetails bool
16+ sites []*db.VisitUrl
17+ features []*db.FeatureFlag
18+ err error
19+ stats map[string]*db.SummaryVisits
20+ selected string
21+ interval string
22+ focus string
23+ leftPane list.Dynamic
24+ rightPane *Pager
25 }
26
27 func NewAnalyticsPage(shrd *SharedModel) *AnalyticsPage {
28@@ -74,6 +76,7 @@ func (m *AnalyticsPage) getLeftWidget(i uint, cursor uint) vxfw.Widget {
29 func (m *AnalyticsPage) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw.Command, error) {
30 switch msg := ev.(type) {
31 case PageIn:
32+ m.loadingSites = true
33 go m.fetchSites()
34 _ = m.fetchFeatures()
35 m.focus = "page"
36@@ -93,6 +96,7 @@ func (m *AnalyticsPage) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw
37 } else {
38 m.interval = "day"
39 }
40+ m.loadingDetails = true
41 go m.fetchSiteStats(m.selected, m.interval)
42 return vxfw.RedrawCmd{}, nil
43 }
44@@ -115,6 +119,7 @@ func (m *AnalyticsPage) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw
45 }
46 if msg.Matches(vaxis.KeyEnter) {
47 m.selected = m.sites[m.leftPane.Cursor()].Url
48+ m.loadingDetails = true
49 go m.fetchSiteStats(m.selected, m.interval)
50 return vxfw.RedrawCmd{}, nil
51 }
52@@ -164,6 +169,10 @@ func (m *AnalyticsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
53 wdgt = &m.leftPane
54 }
55
56+ if m.loadingSites {
57+ wdgt = text.New("Loading ...")
58+ }
59+
60 leftPane := NewBorder(wdgt)
61 leftPane.Label = "sites"
62 m.focusBorder(leftPane)
63@@ -192,9 +201,15 @@ func (m *AnalyticsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
64 rightSurf := vxfw.NewSurface(uint16(rightPaneW), math.MaxUint16, m)
65
66 ah := 0
67+
68 data, err := m.getSiteData()
69 if err != nil {
70- txt, _ := text.New("No data found").Draw(ctx)
71+ var txt vxfw.Surface
72+ if m.loadingDetails {
73+ txt, _ = text.New("Loading ...").Draw(ctx)
74+ } else {
75+ txt, _ = text.New("No data found").Draw(ctx)
76+ }
77 m.rightPane.Surface = txt
78 rightPane := NewBorder(m.rightPane)
79 rightPane.Label = "details"
80@@ -333,9 +348,12 @@ func (m *AnalyticsPage) fetchSites() {
81 Origin: utils.StartOfMonth(),
82 })
83 if err != nil {
84+ m.loadingSites = false
85 m.err = err
86+ return
87 }
88 m.sites = siteList
89+ m.loadingSites = false
90 m.shared.App.PostEvent(SitesLoaded{})
91 }
92
93@@ -356,9 +374,11 @@ func (m *AnalyticsPage) fetchSiteStats(site string, interval string) {
94 summary, err := m.shared.Dbpool.VisitSummary(opts)
95 if err != nil {
96 m.err = err
97+ m.loadingDetails = false
98 return
99 }
100 m.stats[site+":"+interval] = summary
101+ m.loadingDetails = false
102 m.shared.App.PostEvent(SiteStatsLoaded{})
103 }
104
+8,
-1
1@@ -81,6 +81,7 @@ type TunsLoaded struct{}
2 type TunsPage struct {
3 shared *SharedModel
4
5+ loading bool
6 err error
7 tuns []TunsClientSimple
8 selected string
9@@ -188,6 +189,7 @@ func (m *TunsPage) Footer() []Shortcut {
10 func (m *TunsPage) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command, error) {
11 switch msg := ev.(type) {
12 case PageIn:
13+ m.loading = true
14 go m.fetchTuns()
15 go func() {
16 _ = m.connectToLogs()
17@@ -260,6 +262,10 @@ func (m *TunsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
18 wdgt = &m.leftPane
19 }
20
21+ if m.loading {
22+ wdgt = text.New("Loading ...")
23+ }
24+
25 leftPane := NewBorder(wdgt)
26 leftPane.Label = "tuns"
27 m.focusBorder(leftPane)
28@@ -319,7 +325,7 @@ func (m *TunsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
29 Characters: vaxis.Characters,
30 Max: vxfw.Size{
31 Width: uint16(rightPaneW) - 4,
32- Height: 15,
33+ Height: ctx.Max.Height - uint16(ah) - 3,
34 },
35 })
36 rightSurf.AddChild(0, ah, surf)
37@@ -460,5 +466,6 @@ func (m *TunsPage) fetchTuns() {
38 })
39
40 m.tuns = ls
41+ m.loading = false
42 m.shared.App.PostEvent(TunsLoaded{})
43 }