Skip to content

Commit

Permalink
refactor: generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 14, 2024
1 parent 603e9cf commit cd20d0e
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 45 deletions.
2 changes: 1 addition & 1 deletion internal/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GenerateKeyPair(
}

// Wait for the api to have a new key
keys, err := waitForNewKey(ctx, client, originalKeys, 2*time.Second, 20*time.Second)
keys, err := waitForNewKey(ctx, client, originalKeys, 2*time.Second, 20*time.Minute)
if err != nil {
return nil, err
}
Expand Down
46 changes: 26 additions & 20 deletions ui/app/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
tea "github.com/charmbracelet/bubbletea"
"time"
)

type DeleteFinished struct {
Expand All @@ -30,27 +31,32 @@ func EmitDeleteKey(ctx context.Context, client *api.ClientWithResponses, id stri
}
}

func GenerateCmd(account string, state *internal.StateModel) tea.Cmd {
params := api.GenerateParticipationKeysParams{
Dilution: nil,
First: int(state.Status.LastRound),
Last: int(state.Status.LastRound) + state.Offset,
}
func GenerateCmd(account string, duration time.Duration, state *internal.StateModel) tea.Cmd {
return func() tea.Msg {
params := api.GenerateParticipationKeysParams{
Dilution: nil,
First: int(state.Status.LastRound),
Last: int(state.Status.LastRound) + int((duration / state.Metrics.RoundTime)),
}

key, err := internal.GenerateKeyPair(state.Context, state.Client, account, &params)
if err != nil {
return ModalEvent{
Key: nil,
Address: "",
Active: false,
Err: &err,
Type: ExceptionModal,
}
}

key, err := internal.GenerateKeyPair(state.Context, state.Client, account, &params)
if err != nil {
return EmitModalEvent(ModalEvent{
Key: nil,
Address: "",
Err: &err,
Type: ExceptionModal,
})
return ModalEvent{
Key: key,
Address: key.Address,
Active: false,
Err: nil,
Type: InfoModal,
}
}

return EmitModalEvent(ModalEvent{
Key: key,
Address: key.Address,
Err: nil,
Type: InfoModal,
})
}
18 changes: 17 additions & 1 deletion ui/modal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package modal

import (
"github.com/algorandfoundation/hack-tui/ui/app"
"github.com/algorandfoundation/hack-tui/ui/modals/generate"
"github.com/algorandfoundation/hack-tui/ui/style"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

func (m ViewModel) Init() tea.Cmd {
return nil
return tea.Batch(
m.infoModal.Init(),
m.exceptionModal.Init(),
m.transactionModal.Init(),
m.confirmModal.Init(),
m.generateModal.Init(),
)
}
func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
var (
Expand All @@ -21,6 +28,9 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
m.exceptionModal.Message = msg.Error()
m.SetType(app.ExceptionModal)
case app.ModalEvent:
if msg.Type == app.InfoModal {
m.generateModal.SetStep(generate.AddressStep)
}
// On closing events
if msg.Type == app.CloseModal {
m.Open = false
Expand All @@ -35,6 +45,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
case app.GenerateModal:
m.Open = false
m.SetType(app.InfoModal)
m.generateModal.SetStep(generate.AddressStep)
case app.TransactionModal:
m.SetType(app.InfoModal)
case app.ExceptionModal:
Expand All @@ -59,6 +70,11 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
case app.DeleteFinished:
m.Open = false
m.Type = app.InfoModal
if msg.Err != nil {
m.Open = true
m.Type = app.ExceptionModal
m.exceptionModal.Message = "Delete failed"
}
// Handle View Size changes
case tea.WindowSizeMsg:
m.Width = msg.Width
Expand Down
96 changes: 85 additions & 11 deletions ui/modals/generate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,48 @@ package generate

import (
"github.com/algorandfoundation/hack-tui/ui/app"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"strconv"
"time"
)

func (m ViewModel) Init() tea.Cmd {
return textinput.Blink
return tea.Batch(textinput.Blink, spinner.Tick)
}

func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.HandleMessage(msg)
}
func (m ViewModel) SetInterval(dur time.Duration) {

}
func (m *ViewModel) SetStep(step Step) {
m.Step = step
switch m.Step {
case AddressStep:
m.Controls = "( esc to cancel )"
m.Title = DefaultTitle
m.BorderColor = DefaultBorderColor
case DurationStep:
m.Controls = "( (s)witch range )"
m.Title = "Validity Range"
m.InputTwo.Focus()
m.InputTwo.PromptStyle = focusedStyle
m.InputTwo.TextStyle = focusedStyle
m.Input.Blur()
case WaitingStep:
m.Controls = ""
m.Title = "Generating Keys"
m.BorderColor = "9"
}
}

func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
var (
cmd tea.Cmd
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
Expand All @@ -25,18 +52,65 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
return &m, app.EmitModalEvent(app.ModalEvent{
Type: app.CancelModal,
})
if m.Step != WaitingStep {
return &m, app.EmitModalEvent(app.ModalEvent{
Type: app.CancelModal,
})
}
case "s":
if m.Step == DurationStep {
switch m.Range {
case Day:
m.Range = Week
case Week:
m.Range = Month
case Month:
m.Range = Year
case Year:
m.Range = Day
}
return &m, nil
}
case "enter":
return &m, app.GenerateCmd(m.Input.Value(), m.State)
switch m.Step {
case AddressStep:
m.SetStep(DurationStep)
return &m, app.EmitShowModal(app.GenerateModal)
case DurationStep:
m.SetStep(WaitingStep)
val, _ := strconv.Atoi(m.InputTwo.Value())
var dur time.Duration
switch m.Range {
case Day:
dur = time.Duration(int(time.Hour*24) * val)
case Week:
dur = time.Duration(int(time.Hour*24*7) * val)
case Month:
dur = time.Duration(int(time.Hour*24*30) * val)
case Year:
dur = time.Duration(int(time.Hour*24*365) * val)
}
return &m, tea.Sequence(app.EmitShowModal(app.GenerateModal), app.GenerateCmd(m.Input.Value(), dur, m.State))

}

}

}
// Handle character input and blinking
var val textinput.Model
val, cmd = m.Input.Update(msg)

m.Input = &val
return &m, cmd
switch m.Step {
case AddressStep:
// Handle character input and blinking
var val textinput.Model
val, cmd = m.Input.Update(msg)
m.Input = &val
cmds = append(cmds, cmd)
case DurationStep:
var val textinput.Model
val, cmd = m.InputTwo.Update(msg)
m.InputTwo = &val
cmds = append(cmds, cmd)
}

return &m, tea.Batch(cmds...)
}
48 changes: 43 additions & 5 deletions ui/modals/generate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,37 @@ package generate
import (
"github.com/algorandfoundation/hack-tui/internal"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
)

type Step string

const (
AddressStep Step = "address"
DurationStep Step = "duration"
WaitingStep Step = "waiting"
)

type Range string

const (
Day Range = "day"
Week Range = "week"
Month Range = "month"
Year Range = "year"
)

type ViewModel struct {
Width int
Height int

Address string
Input *textinput.Model
Address string
Input *textinput.Model
InputTwo *textinput.Model
Spinner *spinner.Model
Step Step
Range Range

Title string
Controls string
Expand All @@ -26,21 +48,37 @@ func (m ViewModel) SetAddress(address string) {
m.Input.SetValue(address)
}

var DefaultControls = "( esc to cancel )"
var DefaultTitle = "Generate Participation Keys"
var DefaultBorderColor = "2"

func New(address string, state *internal.StateModel) *ViewModel {
input := textinput.New()
input2 := textinput.New()

m := ViewModel{
Address: address,
State: state,
Input: &input,
Title: "Generate Participation Key",
Controls: "( esc to cancel )",
BorderColor: "2",
InputTwo: &input2,
Step: AddressStep,
Range: Day,
Title: DefaultTitle,
Controls: DefaultControls,
BorderColor: DefaultBorderColor,
}
input.Cursor.Style = cursorStyle
input.CharLimit = 68
input.Placeholder = "Wallet Address"
input.Focus()
input.PromptStyle = focusedStyle
input.TextStyle = focusedStyle

input2.Cursor.Style = cursorStyle
input2.CharLimit = 68
input2.Placeholder = "Length of time"

input2.PromptStyle = noStyle
input2.TextStyle = noStyle
return &m
}
29 changes: 27 additions & 2 deletions ui/modals/generate/view.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
package generate

import (
"fmt"
"github.com/charmbracelet/lipgloss"
)

func (m ViewModel) View() string {
m.Input.Focused()
render := m.Input.View()
render := ""
//m.Input.Focus()
switch m.Step {
case AddressStep:
render = lipgloss.JoinVertical(lipgloss.Left,
"",
"Create keys required to participate in Algorand consensus.",
"",
"Account address:",
m.Input.View(),
"",
)
case DurationStep:
render = lipgloss.JoinVertical(lipgloss.Left,
"",
"How long should the keys be valid for?",
"",
fmt.Sprintf("Duration in %ss:", m.Range),
m.InputTwo.View(),
"",
)
case WaitingStep:
render = lipgloss.JoinVertical(lipgloss.Left,
"Generating Participation Keys...",
"Please wait. This operation can take a few minutes.")
}

if lipgloss.Width(render) < 70 {
return lipgloss.NewStyle().Width(70).Render(render)
Expand Down
6 changes: 3 additions & 3 deletions ui/pages/keys/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
m.table.SetRows(m.makeRows(m.Data))
// When a confirmation Modal is finished deleting
case app.DeleteFinished:
if msg.Err != nil {
panic(msg.Err)
}
//if msg.Err != nil {
// panic(msg.Err)
//}
internal.RemovePartKeyByID(m.Data, msg.Id)
m.table.SetRows(m.makeRows(m.Data))
// When the user interacts with the render
Expand Down
Loading

0 comments on commit cd20d0e

Please sign in to comment.