@@ -7,11 +7,10 @@ import (
7
7
"github.com/charmbracelet/bubbles/key"
8
8
tea "github.com/charmbracelet/bubbletea"
9
9
"github.com/charmbracelet/lipgloss"
10
- "github.com/charmbracelet/log"
11
10
12
11
"github.com/dlvhdr/gh-dash/config"
13
12
"github.com/dlvhdr/gh-dash/ui/common"
14
- "github.com/dlvhdr/gh-dash/ui/components/container "
13
+ "github.com/dlvhdr/gh-dash/ui/components"
15
14
"github.com/dlvhdr/gh-dash/ui/components/prssection"
16
15
"github.com/dlvhdr/gh-dash/ui/components/section"
17
16
"github.com/dlvhdr/gh-dash/ui/components/sectioncard"
@@ -21,25 +20,27 @@ import (
21
20
)
22
21
23
22
type Model struct {
24
- ctx * context.ProgramContext
25
- containers []container.Model
26
- cards []* sectioncard.Model
27
- focusedCardId int
23
+ ctx * context.ProgramContext
24
+ boards []board
25
+ focusedCardId int
26
+ focusedBoardId int
27
+ }
28
+
29
+ type board struct {
30
+ cards []* sectioncard.Model
28
31
}
29
32
30
33
func NewModel (ctx context.ProgramContext ) Model {
31
- return Model {ctx : & ctx , cards : []* sectioncard. Model {}}
34
+ return Model {ctx : & ctx , boards : []board {}}
32
35
}
33
36
34
37
func (m Model ) Update (msg tea.Msg ) (Model , tea.Cmd ) {
35
38
var cmd tea.Cmd
36
39
switch msg := msg .(type ) {
37
40
case tea.KeyMsg :
38
- if len (m .cards ) == 0 {
39
- break
40
- }
41
41
42
- currCard := m .cards [m .focusedCardId ]
42
+ board := m .boards [m .focusedBoardId ]
43
+ currCard := board .cards [m .focusedCardId ]
43
44
44
45
switch {
45
46
@@ -61,43 +62,23 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
61
62
62
63
case key .Matches (msg , keys .Keys .PrevCard ):
63
64
m .GoToPrevCard ()
64
- }
65
-
66
- case common.ConfigReadMsg :
67
- sections , fetchSectionsCmds := m .fetchAllDashboards ()
68
- for i := 0 ; i < len (sections ); i ++ {
69
- section := sections [i ]
70
- prsSection := prssection .NewModel (section .Id , m .ctx , config.PrsSectionConfig {
71
- Title : section .Config .Title ,
72
- Filters : section .Config .Filters ,
73
- Layout : config.PrsLayoutConfig {},
74
- }, time .Now (), true )
75
- prsSection .Table .SetHeaderHidden (true )
76
- prsSection .Table .SetFooterHidden (true )
77
- prsSection .Table .EmptyState = nil
78
- prsSection .Table .SetSeparatorHidden (true )
79
- prsSection .Table .IsActive = i == 0
80
- sectionStyle := lipgloss .NewStyle ().Padding (0 )
81
- prsSection .Style = & sectionStyle
82
- cellStyle := m .ctx .Styles .Table .CellStyle .Copy ().Background (m .ctx .Styles .Card .Content .GetBackground ())
83
- prsSection .Table .CellStyle = & cellStyle
84
65
85
- card := sectioncard .NewModel (m .ctx )
86
- card .Title = section .Config .Title
87
- card .Subtitle = "(loading...)"
88
- card .Section = & prsSection
66
+ case key .Matches (msg , keys .Keys .PrevSection ):
67
+ m .previousBoard ()
89
68
90
- card .UpdateProgramContext (m .ctx )
91
- m .cards = append (m .cards , & card )
92
- log .Debug ("SectionDataFetchedMsg fetching" , "title" , section .Config .Title )
69
+ case key .Matches (msg , keys .Keys .NextSection ):
70
+ m .nextBoard ()
93
71
}
94
72
73
+ case common.ConfigReadMsg :
74
+ fetchSectionsCmds := m .fetchAllDashboards ()
95
75
cmd = fetchSectionsCmds
96
76
97
77
case section.SectionDataFetchedMsg :
98
- log .Debug ("SectionDataFetchedMsg fetched" , "title" , m .cards [msg .Id ].Title , "prs" , len (msg .Prs ))
78
+ boardId := 0
79
+ cards := m .boards [boardId ].cards
99
80
if msg .Type == string (config .PRsView ) {
100
- m . cards [msg .Id ].SetPrs (prssection.SectionPullRequestsFetchedMsg {
81
+ cards [msg .Id ].SetPrs (prssection.SectionPullRequestsFetchedMsg {
101
82
Prs : msg .Prs ,
102
83
TotalCount : msg .TotalCount ,
103
84
PageInfo : msg .PageInfo ,
@@ -108,16 +89,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
108
89
}
109
90
110
91
func (m Model ) View () string {
111
- elements := make ([]string , len (m .cards ))
112
- for i := 0 ; i < len (m .cards ); i ++ {
113
- log .Debug (
114
- "view" ,
115
- "getItem" ,
116
- m .cards [i ].Section .Table .GetCurrItem (),
117
- "isActive" ,
118
- m .cards [i ].Section .Table .IsActive ,
119
- )
120
- elements [i ] = m .cards [i ].View ()
92
+ cards := m .boards [m .focusedBoardId ].cards
93
+ elements := make ([]string , len (cards ))
94
+ for i := 0 ; i < len (cards ); i ++ {
95
+ elements [i ] = cards [i ].View ()
121
96
}
122
97
content := lipgloss .JoinVertical (lipgloss .Left , elements ... )
123
98
return lipgloss .PlaceVertical (
@@ -130,51 +105,103 @@ func (m Model) View() string {
130
105
func (m * Model ) UpdateProgramContext (ctx * context.ProgramContext ) {
131
106
m .ctx = ctx
132
107
133
- for i := 0 ; i < len (m .cards ); i ++ {
134
- m .cards [i ].UpdateProgramContext (ctx )
108
+ for i := 0 ; i < len (m .boards ); i ++ {
109
+ for j := 0 ; j < len (m .boards [i ].cards ); j ++ {
110
+ m .boards [i ].cards [j ].UpdateProgramContext (ctx )
111
+ }
135
112
}
136
113
}
137
114
138
- func (m * Model ) fetchAllDashboards () ([]section. Model , tea.Cmd ) {
115
+ func (m * Model ) fetchAllDashboards () tea.Cmd {
139
116
var cmds []tea.Cmd
140
- var sections []section.Model
141
-
142
- for i := 0 ; i < len (m .ctx .Config .Dashboards [0 ].Sections ); i ++ {
143
- log .Debug ("[Dashboard] Creating task with id" , "i" )
144
- config := m .ctx .Config .Dashboards [0 ].Sections [i ]
145
- sectionModel := section.Model {
146
- BaseModel : section.BaseModel {
147
- Id : i ,
148
- Ctx : m .ctx ,
149
- Type : "prs" ,
150
- Config : config ,
151
- },
152
- }
153
- sections = append (sections , sectionModel )
154
- if strings .Contains (sectionModel .Config .Filters , "is:pr" ) {
155
- cmds = append (cmds , sectionModel .FetchSectionRows (nil )... )
117
+
118
+ for i := 0 ; i < len (m .ctx .Config .Dashboards ); i ++ {
119
+ var cards []* sectioncard.Model
120
+ for j := 0 ; j < len (m .ctx .Config .Dashboards [i ].Sections ); j ++ {
121
+ sectionConfig := m .ctx .Config .Dashboards [i ].Sections [j ]
122
+ sectionModel := section.Model {
123
+ BaseModel : section.BaseModel {
124
+ Id : j ,
125
+ Ctx : m .ctx ,
126
+ Type : "prs" ,
127
+ Config : sectionConfig ,
128
+ },
129
+ }
130
+ if strings .Contains (sectionModel .Config .Filters , "is:pr" ) {
131
+ cmds = append (cmds , sectionModel .FetchSectionRows (nil )... )
132
+ }
133
+
134
+ prsSection := prssection .NewModel (
135
+ sectionModel .Id ,
136
+ m .ctx ,
137
+ config.PrsSectionConfig {
138
+ Title : sectionModel .Config .Title ,
139
+ Filters : sectionModel .Config .Filters ,
140
+ Layout : config.PrsLayoutConfig {},
141
+ },
142
+ time .Now (),
143
+ true ,
144
+ )
145
+ prsSection .Table .SetHeaderHidden (true )
146
+ prsSection .Table .SetFooterHidden (true )
147
+ prsSection .Table .EmptyState = nil
148
+ prsSection .Table .SetSeparatorHidden (true )
149
+ prsSection .Table .IsActive = j == 0
150
+ sectionStyle := lipgloss .NewStyle ().Padding (0 )
151
+ prsSection .Style = & sectionStyle
152
+ cellStyle := m .ctx .Styles .Table .CellStyle .Copy ().
153
+ Background (m .ctx .Styles .Card .Content .GetBackground ())
154
+ prsSection .Table .CellStyle = & cellStyle
155
+
156
+ card := sectioncard .NewModel (m .ctx )
157
+ card .Title = sectionModel .Config .Title
158
+ card .Subtitle = "(loading...)"
159
+ card .Section = & prsSection
160
+
161
+ card .UpdateProgramContext (m .ctx )
162
+
163
+ cards = append (cards , & card )
156
164
}
165
+ m .boards = append (m .boards , board {
166
+ cards : cards ,
167
+ })
157
168
}
158
169
159
- return sections , tea .Batch (cmds ... )
170
+ return tea .Batch (cmds ... )
160
171
}
161
172
162
173
func (m * Model ) GoToNextCard () {
163
- currCard := m .cards [m .focusedCardId ]
174
+ cards := m .boards [m .focusedBoardId ].cards
175
+ currCard := cards [m .focusedCardId ]
164
176
currCard .Section .Unfocus ()
165
177
166
- m .focusedCardId = utils .Min (len (m . cards )- 1 , m .focusedCardId + 1 )
167
- nextCard := m . cards [m .focusedCardId ]
178
+ m .focusedCardId = utils .Min (len (cards )- 1 , m .focusedCardId + 1 )
179
+ nextCard := cards [m .focusedCardId ]
168
180
nextCard .Section .Focus ()
169
181
nextCard .Section .FirstItem ()
170
182
}
171
183
172
184
func (m * Model ) GoToPrevCard () {
173
- currCard := m .cards [m .focusedCardId ]
185
+ cards := m .boards [m .focusedBoardId ].cards
186
+ currCard := cards [m .focusedCardId ]
174
187
currCard .Section .Unfocus ()
175
188
176
189
m .focusedCardId = utils .Max (0 , m .focusedCardId - 1 )
177
- nextCard := m . cards [m .focusedCardId ]
190
+ nextCard := cards [m .focusedCardId ]
178
191
nextCard .Section .FirstItem ()
179
192
nextCard .Section .Focus ()
180
193
}
194
+
195
+ func (m * Model ) previousBoard () {
196
+ m .focusedBoardId = components .GetPrevCyclicItem (
197
+ m .focusedBoardId ,
198
+ len (m .boards ),
199
+ )
200
+ }
201
+
202
+ func (m * Model ) nextBoard () {
203
+ m .focusedBoardId = components .GetNextCyclicItem (
204
+ m .focusedBoardId ,
205
+ len (m .boards ),
206
+ )
207
+ }
0 commit comments