103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
package widget
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.ohea.xyz/cursorius/tui/queries"
|
|
"github.com/Khan/genqlient/graphql"
|
|
"github.com/charmbracelet/bubbles/list"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type pipelineWidgetList struct {
|
|
list list.Model
|
|
client graphql.Client
|
|
}
|
|
|
|
func (m pipelineWidgetList) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m pipelineWidgetList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.WindowSizeMsg:
|
|
m.list.SetWidth(msg.Width - 10)
|
|
m.list.SetHeight(msg.Height - 10)
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "down":
|
|
if m.list.Index() < len(m.list.Items()) {
|
|
m.list.Select(m.list.Index() + 1)
|
|
}
|
|
case "up":
|
|
if m.list.Index() > 0 {
|
|
m.list.Select(m.list.Index() - 1)
|
|
}
|
|
case "enter":
|
|
if m.list.Index() < len(m.list.Items())-1 {
|
|
//item := m.list.SelectedItem()
|
|
//if item, ok := item.(pipelineListItem); ok {
|
|
// return m, func() tea.Cmd {
|
|
// view, err := createPipelineEditForm(client, item.id)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// return pipelineWidgetScreenSwitch(view)
|
|
// }
|
|
//}
|
|
} else {
|
|
return m, func() tea.Msg {
|
|
view, err := createPipelineCreateForm(m.client, m.list.Width(), m.list.Height())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return pipelineWidgetScreenSwitch(view)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
func (m pipelineWidgetList) View() string {
|
|
return m.list.View()
|
|
}
|
|
|
|
type pipelineListItem struct {
|
|
name string
|
|
id string
|
|
}
|
|
|
|
func createPipelineWidgetList(client graphql.Client) (tea.Model, error) {
|
|
getPipelineResp, err := queries.GetPipelines(context.Background(), client)
|
|
if err != nil {
|
|
return pipelineWidget{}, fmt.Errorf("Could not connect to server: %w", err)
|
|
}
|
|
|
|
var content []list.Item
|
|
|
|
if getPipelineResp.Pipelines != nil {
|
|
for _, pipeline := range getPipelineResp.Pipelines {
|
|
if pipeline != nil {
|
|
content = append(content, pipelineListItem{
|
|
name: pipeline.Name,
|
|
id: pipeline.Id,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
content = append(content, pipelineCreateListItem("[Create Pipeline]"))
|
|
|
|
pipelineList := list.New(content, pipelineListItemDelegate{}, 0, 0)
|
|
pipelineList.SetShowStatusBar(false)
|
|
pipelineList.Title = "Pipelines"
|
|
|
|
return pipelineWidgetList{
|
|
list: pipelineList,
|
|
client: client,
|
|
}, nil
|
|
}
|