Skip to content

Commit

Permalink
fix: bug that would spam radix api more when swapping between applica…
Browse files Browse the repository at this point in the history
…tion

feat: adds enter and escape as naviagtion options between pipelines and aplications
  • Loading branch information
FredrikMWold committed Jul 16, 2024
1 parent df9a992 commit fd9c2d0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 51 deletions.
45 changes: 22 additions & 23 deletions applicationDashboard/applicationDashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appllicationDashboard

import (
"github.com/FredrikMWold/radix-tui/applicationTable"
"github.com/FredrikMWold/radix-tui/styles"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -13,6 +14,20 @@ func (m Model) Init() tea.Cmd {

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd

if m.focused == application {
var applicationsTableCmd tea.Cmd
m.applicationsTable, applicationsTableCmd = m.applicationsTable.Update(msg)
cmds = append(cmds, applicationsTableCmd)
}

if m.focused == pipeline {
var pipelineTableCmd tea.Cmd
m.pipelineTable, pipelineTableCmd = m.pipelineTable.Update(msg)
cmds = append(cmds, pipelineTableCmd)
}

switch msg := msg.(type) {

case tea.KeyMsg:
Expand All @@ -22,28 +37,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "tab":
m.ChangeFocus()
case "ctrl+r":
var cmd tea.Cmd
m.applicationsTable, cmd = m.applicationsTable.Update(msg)
return m, cmd
case "enter":
m.focused = pipeline
case "esc":
m.focused = application
}
case tea.WindowSizeMsg, applicationTable.SelectedApplication, applicationTable.Application, spinner.TickMsg, applicationTable.UpdateApplicationDataTick:
var appCmds, pipeCmds, envCmds tea.Cmd
m.applicationsTable, appCmds = m.applicationsTable.Update(msg)
m.pipelineTable, pipeCmds = m.pipelineTable.Update(msg)
m.enviromentTable, envCmds = m.enviromentTable.Update(msg)
return m, tea.Batch(appCmds, pipeCmds, envCmds)
}

if m.focused == application {
var applicationsTableCmd tea.Cmd
m.applicationsTable, applicationsTableCmd = m.applicationsTable.Update(msg)
cmds = append(cmds, applicationsTableCmd)
}

if m.focused == pipeline {
var pipelineTableCmd tea.Cmd
m.pipelineTable, pipelineTableCmd = m.pipelineTable.Update(msg)
cmds = append(cmds, pipelineTableCmd)
cmds = append(cmds, appCmds, pipeCmds, envCmds)
}

return m, tea.Batch(cmds...)
Expand All @@ -55,20 +61,13 @@ func (m Model) View() string {
lipgloss.Top,
lipgloss.JoinVertical(
lipgloss.Top,
m.applicationsTable.View(),
styles.SectionContainer(m.focused == application).Render(m.applicationsTable.View()),
m.enviromentTable.View(),
),
m.pipelineTable.View(),
styles.SectionContainer(m.focused == pipeline).Render(m.pipelineTable.View()),
)
}

func (m *Model) ChangeFocus() {
m.focused = (m.focused + 1) % (pipeline + 1)
if m.focused == pipeline {
m.pipelineTable.Focus()
m.applicationsTable.Blur()
} else {
m.applicationsTable.Focus()
m.pipelineTable.Blur()
}
}
22 changes: 7 additions & 15 deletions applicationTable/applicationTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (m Model) Init() tea.Cmd {
return tea.Batch(m.spinner.Tick, getApplications)
return tea.Batch(tick(), m.spinner.Tick, getApplications)
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
Expand All @@ -21,11 +21,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case "enter", "ctrl+r":
if len(m.table.SelectedRow()) > 0 {
m.selectedApp = m.table.SelectedRow()[0]
return m, tea.Batch(
getApplicationData(m.selectedApp),
selectApplication(m.selectedApp),
tick(),
)
if m.selectedApp == "" {
cmds = append(cmds, tick())
}
cmds = append(cmds, getApplicationData(m.selectedApp), selectApplication(m.selectedApp))
return m, tea.Batch(cmds...)
}
}
case tea.WindowSizeMsg:
Expand Down Expand Up @@ -74,13 +74,5 @@ func (m Model) View() string {
} else {
section = lipgloss.JoinVertical(lipgloss.Center, "Applications", table)
}
return styles.SectionContainer(m.focused).Render(section)
}

func (m *Model) Focus() {
m.focused = true
}

func (m *Model) Blur() {
m.focused = false
return section
}
2 changes: 0 additions & 2 deletions applicationTable/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func New() Model {
),
spinner: spiner,
isLoadingApplications: true,
focused: true,
}
}

Expand All @@ -33,7 +32,6 @@ type Model struct {
spinner spinner.Model
isLoadingApplications bool
selectedApp string
focused bool
}

type Application struct {
Expand Down
2 changes: 0 additions & 2 deletions pipelineTable/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func New() Model {
),
spinner: spiner,
selectedApplication: "No application selected",
focused: false,
isLoadingApplication: false,
}
}
Expand All @@ -29,5 +28,4 @@ type Model struct {
jobs []string
selectedApplication string
spinner spinner.Model
focused bool
}
13 changes: 4 additions & 9 deletions pipelineTable/pipelineTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ func (m Model) View() string {
section = lipgloss.JoinVertical(lipgloss.Center, m.selectedApplication, table)
}

return styles.SectionContainer(m.focused).Render(section)
}

func (m *Model) Focus() {
m.focused = true
}

func (m *Model) Blur() {
m.focused = false
return section
}

func (m *Model) loadApplication(application applicationTable.Application) {
Expand All @@ -105,6 +97,9 @@ func (m *Model) loadApplication(application applicationTable.Application) {
}

func (m Model) openJobInBrowser() {
if len(m.jobs) == 0 {
return
}
tableCursor := m.table.Cursor()
url := fmt.Sprintf("https://console.radix.equinor.com/applications/%s/jobs/view/%s", m.selectedApplication, m.jobs[tableCursor])
exec.Command("open", url).Start()
Expand Down

0 comments on commit fd9c2d0

Please sign in to comment.