Skip to content

Commit

Permalink
refactor: overlays with modal
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 6, 2024
1 parent 34a6dde commit 3fa81c6
Show file tree
Hide file tree
Showing 25 changed files with 886 additions and 363 deletions.
13 changes: 7 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var (
client, err := getClient()
cobra.CheckErr(err)

partkeys, err := internal.GetPartKeys(context.Background(), client)
ctx := context.Background()
partkeys, err := internal.GetPartKeys(ctx, client)
if err != nil {
return fmt.Errorf(
style.Red.Render("failed to get participation keys: %s"),
Expand All @@ -75,11 +76,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 @@ -99,12 +103,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.3.2 // indirect
github.com/charmbracelet/x/ansi v0.3.2
github.com/charmbracelet/x/exp/golden v0.0.0-20241022174419-46d9bb99a691 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
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()

if s.Status.State == "SYNCING" {
lastRound = s.Status.LastRound
Expand Down Expand Up @@ -107,18 +110,18 @@ func (s *StateModel) UpdateMetricsFromRPC(ctx context.Context, client *api.Clien
s.Metrics.LastRX = 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()
}
}
2 changes: 2 additions & 0 deletions internal/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func Test_StateModel(t *testing.T) {
RX: 0,
TPS: 0,
},
Client: client,
Context: context.Background(),
}
count := 0
go state.Watch(func(model *StateModel, err error) {
Expand Down
81 changes: 81 additions & 0 deletions ui/modals/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/modals/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 3fa81c6

Please sign in to comment.