From fd9c2d0e1382b3a3632c50e17d7ac0f1429ed4dc Mon Sep 17 00:00:00 2001 From: FredrikMWold Date: Tue, 16 Jul 2024 23:10:48 +0200 Subject: [PATCH] fix: bug that would spam radix api more when swapping between application feat: adds enter and escape as naviagtion options between pipelines and aplications --- applicationDashboard/applicationDashboard.go | 45 ++++++++++---------- applicationTable/applicationTable.go | 22 +++------- applicationTable/model.go | 2 - pipelineTable/model.go | 2 - pipelineTable/pipelineTable.go | 13 ++---- 5 files changed, 33 insertions(+), 51 deletions(-) diff --git a/applicationDashboard/applicationDashboard.go b/applicationDashboard/applicationDashboard.go index eba996e..357d01a 100644 --- a/applicationDashboard/applicationDashboard.go +++ b/applicationDashboard/applicationDashboard.go @@ -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" @@ -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: @@ -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...) @@ -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() - } } diff --git a/applicationTable/applicationTable.go b/applicationTable/applicationTable.go index 2284e59..46f3777 100644 --- a/applicationTable/applicationTable.go +++ b/applicationTable/applicationTable.go @@ -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) { @@ -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: @@ -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 } diff --git a/applicationTable/model.go b/applicationTable/model.go index 2aa2901..adea13c 100644 --- a/applicationTable/model.go +++ b/applicationTable/model.go @@ -24,7 +24,6 @@ func New() Model { ), spinner: spiner, isLoadingApplications: true, - focused: true, } } @@ -33,7 +32,6 @@ type Model struct { spinner spinner.Model isLoadingApplications bool selectedApp string - focused bool } type Application struct { diff --git a/pipelineTable/model.go b/pipelineTable/model.go index 7cde99f..4a07c26 100644 --- a/pipelineTable/model.go +++ b/pipelineTable/model.go @@ -18,7 +18,6 @@ func New() Model { ), spinner: spiner, selectedApplication: "No application selected", - focused: false, isLoadingApplication: false, } } @@ -29,5 +28,4 @@ type Model struct { jobs []string selectedApplication string spinner spinner.Model - focused bool } diff --git a/pipelineTable/pipelineTable.go b/pipelineTable/pipelineTable.go index 759b344..b3e42f3 100644 --- a/pipelineTable/pipelineTable.go +++ b/pipelineTable/pipelineTable.go @@ -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) { @@ -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()