Skip to content

Commit

Permalink
refactor: overlay with modals
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 6, 2024
1 parent da64fee commit c78efe4
Show file tree
Hide file tree
Showing 21 changed files with 733 additions and 395 deletions.
14 changes: 8 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ var (
client, err := getClient()
cobra.CheckErr(err)

partkeys, err := internal.GetPartKeys(context.Background(), client)
ctx := context.Background()

partkeys, err := internal.GetPartKeys(ctx, client)
cobra.CheckErr(err)

state := internal.StateModel{
Expand All @@ -61,11 +63,14 @@ var (
TX: 0,
},
ParticipationKeys: partkeys,

Client: client,
Context: ctx,
}
state.Accounts = internal.AccountsFromState(&state, new(internal.Clock), client)

// Fetch current state
err = state.Status.Fetch(context.Background(), client)
err = state.Status.Fetch(ctx, client)
cobra.CheckErr(err)

m, err := ui.MakeViewportViewModel(&state, client)
Expand All @@ -85,12 +90,9 @@ var (
p.Send(state)
p.Send(err)
}
}, context.Background(), client)
}, ctx, client)
}()
_, err = p.Run()
//for {
// time.Sleep(10 * time.Second)
//}
return err
},
}
Expand Down
15 changes: 9 additions & 6 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type StateModel struct {
// TODO: handle contexts instead of adding it to state
Admin bool
Watching bool

Client *api.ClientWithResponses
Context context.Context
}

func (s *StateModel) waitAfterError(err error, cb func(model *StateModel, err error)) {
Expand Down Expand Up @@ -60,7 +63,7 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
s.Status.Update(status.JSON200.LastRound, status.JSON200.CatchupTime, status.JSON200.UpgradeNodeVote)

// Fetch Keys
s.UpdateKeys(ctx, client)
s.UpdateKeys()

// Run Round Averages and RX/TX every 5 rounds
if s.Status.LastRound%5 == 0 {
Expand Down Expand Up @@ -95,18 +98,18 @@ func (s *StateModel) UpdateMetricsFromRPC(ctx context.Context, client *api.Clien
s.Metrics.RX = res["algod_network_received_bytes_total"]
}
}
func (s *StateModel) UpdateAccounts(client *api.ClientWithResponses) {
s.Accounts = AccountsFromState(s, new(Clock), client)
func (s *StateModel) UpdateAccounts() {
s.Accounts = AccountsFromState(s, new(Clock), s.Client)
}

func (s *StateModel) UpdateKeys(ctx context.Context, client *api.ClientWithResponses) {
func (s *StateModel) UpdateKeys() {
var err error
s.ParticipationKeys, err = GetPartKeys(ctx, client)
s.ParticipationKeys, err = GetPartKeys(s.Context, s.Client)
if err != nil {
s.Admin = false
}
if err == nil {
s.Admin = true
s.UpdateAccounts(client)
s.UpdateAccounts()
}
}
81 changes: 81 additions & 0 deletions ui/modal/confirm/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package confirm

import (
"fmt"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui/style"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type Msg *api.ParticipationKey

func EmitMsg(key *api.ParticipationKey) tea.Cmd {
return func() tea.Msg {
return Msg(key)
}
}

type ViewModel struct {
Width int
Height int
Title string
Controls string
BorderColor string
ActiveKey *api.ParticipationKey
Data *internal.StateModel
}

func New(state *internal.StateModel) *ViewModel {
return &ViewModel{
Width: 0,
Height: 0,
Title: "Delete Key",
BorderColor: "9",
Controls: "( " + style.Green.Render("(y)es") + " | " + style.Red.Render("(n)o") + " )",
Data: state,
}
}

func (m ViewModel) Init() tea.Cmd {
return nil
}

func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.HandleMessage(msg)
}
func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "y":
return &m, EmitMsg(m.ActiveKey)
case "n":
return &m, EmitMsg(nil)
}
case tea.WindowSizeMsg:
m.Width = msg.Width
m.Height = msg.Height
}
return &m, nil
}
func (m ViewModel) View() string {
if m.ActiveKey == nil {
return "No key selected"
}
return renderDeleteConfirmationModal(m.ActiveKey)

}

func renderDeleteConfirmationModal(partKey *api.ParticipationKey) string {
modalStyle := lipgloss.NewStyle().
Width(60).
Height(7).
Align(lipgloss.Center).
Padding(1, 2)

modalContent := fmt.Sprintf("Participation Key: %v\nAccount Address: %v", partKey.Id, partKey.Address)

return modalStyle.Render("Are you sure you want to delete this key from your node?\n" + modalContent)
}
86 changes: 86 additions & 0 deletions ui/modal/info/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package info

import (
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui/style"
"github.com/algorandfoundation/hack-tui/ui/utils"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
)

type ViewModel struct {
Width int
Height int
Title string
Controls string
BorderColor string
ActiveKey *api.ParticipationKey
Data *internal.StateModel
}

func New(state *internal.StateModel) *ViewModel {
return &ViewModel{
Width: 0,
Height: 0,
Title: "Key Information",
BorderColor: "3",
Controls: "( " + style.Red.Render("(d)elete") + " | " + style.Green.Render("(o)nline") + " )",
Data: state,
}
}

func (m ViewModel) Init() tea.Cmd {
return nil
}

func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.HandleMessage(msg)
}
func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.Width = msg.Width
m.Height = msg.Height
}
m.UpdateState()
return &m, nil
}
func (m *ViewModel) UpdateState() {
if m.ActiveKey == nil {
return
}
accountStatus := m.Data.Accounts[m.ActiveKey.Address].Status

if accountStatus == "Online" {
m.Controls = "( " + style.Red.Render("(d)elete") + " | " + style.Yellow.Render("(o)ffline") + " )"
} else {
m.Controls = "( " + style.Red.Render("(d)elete") + " | " + style.Green.Render("(o)nline") + " )"
}
}
func (m ViewModel) View() string {
if m.ActiveKey == nil {
return "No key selected"
}

id := style.Cyan.Render("Participation ID: ") + m.ActiveKey.Id
selection := style.Yellow.Render("Selection Key: ") + *utils.UrlEncodeBytesPtrOrNil(m.ActiveKey.Key.SelectionParticipationKey[:])
vote := style.Yellow.Render("Vote Key: ") + *utils.UrlEncodeBytesPtrOrNil(m.ActiveKey.Key.VoteParticipationKey[:])
stateProof := style.Yellow.Render("State Proof Key: ") + *utils.UrlEncodeBytesPtrOrNil(*m.ActiveKey.Key.StateProofKey)
voteFirstValid := style.Purple("Vote First Valid: ") + utils.IntToStr(m.ActiveKey.Key.VoteFirstValid)
voteLastValid := style.Purple("Vote Last Valid: ") + utils.IntToStr(m.ActiveKey.Key.VoteLastValid)
voteKeyDilution := style.Purple("Vote Key Dilution: ") + utils.IntToStr(m.ActiveKey.Key.VoteKeyDilution)

return ansi.Hardwrap(lipgloss.JoinVertical(lipgloss.Left,
id,
selection,
vote,
stateProof,
voteFirstValid,
voteLastValid,
voteKeyDilution,
), m.Width, true)

}
Loading

0 comments on commit c78efe4

Please sign in to comment.