|
| 1 | +package views |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + tea "github.com/charmbracelet/bubbletea" |
| 8 | + "github.com/maniac-en/req/internal/backend/collections" |
| 9 | + "github.com/maniac-en/req/internal/backend/endpoints" |
| 10 | + "github.com/maniac-en/req/internal/tui/components" |
| 11 | +) |
| 12 | + |
| 13 | +type EndpointSidebarView struct { |
| 14 | + list components.PaginatedList |
| 15 | + endpointsManager *endpoints.EndpointsManager |
| 16 | + collection collections.CollectionEntity |
| 17 | + width int |
| 18 | + height int |
| 19 | + initialized bool |
| 20 | + selectedIndex int |
| 21 | + endpoints []endpoints.EndpointEntity |
| 22 | +} |
| 23 | + |
| 24 | +func NewEndpointSidebarView(endpointsManager *endpoints.EndpointsManager, collection collections.CollectionEntity) EndpointSidebarView { |
| 25 | + return EndpointSidebarView{ |
| 26 | + endpointsManager: endpointsManager, |
| 27 | + collection: collection, |
| 28 | + selectedIndex: 0, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (v EndpointSidebarView) Init() tea.Cmd { |
| 33 | + return v.loadEndpoints |
| 34 | +} |
| 35 | + |
| 36 | +func (v *EndpointSidebarView) loadEndpoints() tea.Msg { |
| 37 | + result, err := v.endpointsManager.ListByCollection(context.Background(), v.collection.ID, 100, 0) |
| 38 | + if err != nil { |
| 39 | + return endpointsLoadError{err: err} |
| 40 | + } |
| 41 | + return endpointsLoaded{ |
| 42 | + endpoints: result.Endpoints, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (v EndpointSidebarView) Update(msg tea.Msg) (EndpointSidebarView, tea.Cmd) { |
| 47 | + var cmd tea.Cmd |
| 48 | + |
| 49 | + switch msg := msg.(type) { |
| 50 | + case tea.WindowSizeMsg: |
| 51 | + v.width = msg.Width |
| 52 | + v.height = msg.Height |
| 53 | + if v.initialized { |
| 54 | + v.list.SetSize(v.width, v.height) |
| 55 | + } |
| 56 | + |
| 57 | + case endpointsLoaded: |
| 58 | + v.endpoints = msg.endpoints |
| 59 | + items := make([]components.ListItem, len(msg.endpoints)) |
| 60 | + for i, endpoint := range msg.endpoints { |
| 61 | + items[i] = components.NewEndpointItem(endpoint) |
| 62 | + } |
| 63 | + |
| 64 | + title := fmt.Sprintf("Endpoints (%d)", len(msg.endpoints)) |
| 65 | + v.list = components.NewPaginatedList(items, title) |
| 66 | + v.list.SetIndex(v.selectedIndex) |
| 67 | + |
| 68 | + if v.width > 0 && v.height > 0 { |
| 69 | + v.list.SetSize(v.width, v.height) |
| 70 | + } |
| 71 | + v.initialized = true |
| 72 | + |
| 73 | + case endpointsLoadError: |
| 74 | + v.initialized = true |
| 75 | + |
| 76 | + case tea.KeyMsg: |
| 77 | + if v.initialized { |
| 78 | + v.list, cmd = v.list.Update(msg) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return v, cmd |
| 83 | +} |
| 84 | + |
| 85 | +func (v EndpointSidebarView) GetSelectedEndpoint() *endpoints.EndpointEntity { |
| 86 | + if !v.initialized || len(v.endpoints) == 0 { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + selectedIndex := v.list.SelectedIndex() |
| 91 | + if selectedIndex >= 0 && selectedIndex < len(v.endpoints) { |
| 92 | + return &v.endpoints[selectedIndex] |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (v EndpointSidebarView) GetSelectedIndex() int { |
| 98 | + if v.initialized { |
| 99 | + return v.list.SelectedIndex() |
| 100 | + } |
| 101 | + return v.selectedIndex |
| 102 | +} |
| 103 | + |
| 104 | +func (v *EndpointSidebarView) SetSelectedIndex(index int) { |
| 105 | + v.selectedIndex = index |
| 106 | + if v.initialized { |
| 107 | + v.list.SetIndex(index) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func (v EndpointSidebarView) View() string { |
| 112 | + if !v.initialized { |
| 113 | + title := "Endpoints" |
| 114 | + content := "Loading endpoints..." |
| 115 | + return v.formatEmptyState(title, content) |
| 116 | + } |
| 117 | + if len(v.endpoints) == 0 { |
| 118 | + title := "Endpoints (0)" |
| 119 | + content := "No endpoints found" |
| 120 | + return v.formatEmptyState(title, content) |
| 121 | + } |
| 122 | + return v.list.View() |
| 123 | +} |
| 124 | + |
| 125 | +func (v EndpointSidebarView) formatEmptyState(title, content string) string { |
| 126 | + var lines []string |
| 127 | + lines = append(lines, title) |
| 128 | + lines = append(lines, "") |
| 129 | + lines = append(lines, content) |
| 130 | + |
| 131 | + for len(lines) < v.height-2 { |
| 132 | + lines = append(lines, "") |
| 133 | + } |
| 134 | + |
| 135 | + result := "" |
| 136 | + for _, line := range lines { |
| 137 | + result += line + "\n" |
| 138 | + } |
| 139 | + return result |
| 140 | +} |
| 141 | + |
| 142 | +type endpointsLoaded struct { |
| 143 | + endpoints []endpoints.EndpointEntity |
| 144 | +} |
| 145 | + |
| 146 | +type endpointsLoadError struct { |
| 147 | + err error |
| 148 | +} |
0 commit comments