@@ -10,19 +10,21 @@ import (
1010 "github.com/charmbracelet/bubbles/list"
1111 tea "github.com/charmbracelet/bubbletea"
1212 "github.com/maniac-en/req/internal/backend/collections"
13+ "github.com/maniac-en/req/internal/backend/endpoints"
1314 optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
1415 "github.com/maniac-en/req/internal/tui/keybinds"
1516 "github.com/maniac-en/req/internal/tui/messages"
1617)
1718
1819type CollectionsView struct {
19- width int
20- height int
21- list optionsProvider.OptionsProvider [collections.CollectionEntity , string ]
22- manager * collections.CollectionsManager
23- help help.Model
24- keys * keybinds.ListKeyMap
25- order int
20+ width int
21+ height int
22+ list optionsProvider.OptionsProvider [collections.CollectionEntity , string ]
23+ manager * collections.CollectionsManager
24+ endpointsManager * endpoints.EndpointsManager
25+ help help.Model
26+ keys * keybinds.ListKeyMap
27+ order int
2628}
2729
2830func (c CollectionsView ) Init () tea.Cmd {
@@ -85,33 +87,55 @@ func (c CollectionsView) OnBlur() {
8587
8688}
8789
88- func itemMapper (items []collections.CollectionEntity ) []list.Item {
90+ func itemMapper (items []collections.CollectionEntity , endpointsManager * endpoints. EndpointsManager ) []list.Item {
8991 opts := make ([]list.Item , len (items ))
92+
93+ counts , err := endpointsManager .GetCountsByCollections (context .Background ())
94+ if err != nil {
95+ for i , item := range items {
96+ opts [i ] = optionsProvider.Option {
97+ Name : item .GetName (),
98+ Subtext : "0 endpoints" ,
99+ ID : item .GetID (),
100+ }
101+ }
102+ return opts
103+ }
104+
105+ countMap := make (map [int64 ]int )
106+ for _ , count := range counts {
107+ countMap [count .CollectionID ] = int (count .Count )
108+ }
109+
90110 for i , item := range items {
91- newOpt := optionsProvider.Option {
111+ count := countMap [item .GetID ()]
112+ opts [i ] = optionsProvider.Option {
92113 Name : item .GetName (),
93- Subtext : fmt .Sprintf ("%d endpoints" , item . GetEnpointCount () ),
114+ Subtext : fmt .Sprintf ("%d endpoints" , count ),
94115 ID : item .GetID (),
95116 }
96- opts [i ] = newOpt
97117 }
118+
98119 return opts
99120}
100121
101- func NewCollectionsView (collManager * collections.CollectionsManager , order int ) * CollectionsView {
122+ func NewCollectionsView (collManager * collections.CollectionsManager , endpointsManager * endpoints. EndpointsManager , order int ) * CollectionsView {
102123 keybinds := keybinds .NewListKeyMap ()
103124 config := defaultListConfig [collections.CollectionEntity , string ](keybinds )
104125
105126 config .GetItemsFunc = collManager .List
106- config .ItemMapper = itemMapper
127+ config .ItemMapper = func (items []collections.CollectionEntity ) []list.Item {
128+ return itemMapper (items , endpointsManager )
129+ }
107130 config .AdditionalKeymaps = keybinds
108131 config .Source = "collections"
109132
110133 return & CollectionsView {
111- list : optionsProvider .NewOptionsProvider (config ),
112- manager : collManager ,
113- help : help .New (),
114- keys : keybinds ,
115- order : order ,
134+ list : optionsProvider .NewOptionsProvider (config ),
135+ manager : collManager ,
136+ endpointsManager : endpointsManager ,
137+ help : help .New (),
138+ keys : keybinds ,
139+ order : order ,
116140 }
117141}
0 commit comments