Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modals #136

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions ui/modal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,22 @@
diff, isDifferent, count := participation.HasChanged(*m.transactionModal.Participation, acct.Participation)

// The account is valid and we registered
if isValid && !isDifferent && m.Type == app.TransactionModal && !m.transactionModal.Active {
if isValid && !isDifferent && m.Type == app.TransactionModal && !m.transactionModal.OfflineControls {

Check warning on line 104 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L104

Added line #L104 was not covered by tests
m.SetActive(true)
m.infoModal.Prefix = "Successfully registered online!\n"
m.HasPrefix = true
m.SetType(app.InfoModal)
// For the love of all that is good, please lets refactor this. Preferably with a daemon
} else if isValid && isDifferent && count != 6 && (m.Type == app.InfoModal || (m.Type == app.TransactionModal && !m.transactionModal.Active)) {
} else if isValid && isDifferent && count != 6 && (m.Type == app.InfoModal || (m.Type == app.TransactionModal && !m.transactionModal.OfflineControls)) {

Check warning on line 110 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L110

Added line #L110 was not covered by tests
// It is online, has a participation key but not the one we are looking at AND all the keys are not different
// (AND it's the info modal (this case we are checking on enter) OR we are waiting to register a key, and we made a mistake

// You know it's getting bad when the plugin recommendation is Grazie
// TODO: refactor this beast to have isolated state from the modal controller

// Ahh yes, classic "Set Active to the inverse then only navigate when there is no prefix"
// Ahh yes, classic "Set Active to the inverse then only navigate when there is no prefix and it's not the dilution changing"
// Dilution is likely to match some active keys so we just ignore it. First and last rounds must be unique pairs

Check warning on line 115 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L114-L115

Added lines #L114 - L115 were not covered by tests
// This is the closest thing we have to state, between this and the transaction modal state it works
// Set active ensures the offline modal is changed when a corruption happens

Check warning on line 117 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L117

Added line #L117 was not covered by tests
m.SetActive(false)
if m.infoModal.Prefix == "" {
if m.infoModal.Prefix == "" && diff.VoteKeyDilution {

Check warning on line 119 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L119

Added line #L119 was not covered by tests
m.infoModal.Prefix = "***WARNING***\nRegistered online but keys do not fully match\nCheck your registered keys carefully against the node keys\n\n"
if diff.VoteFirstValid {
m.infoModal.Prefix = m.infoModal.Prefix + "Mismatched: Vote First Valid\n"
Expand All @@ -141,7 +140,7 @@

m.SetType(app.InfoModal)
}
} else if !isOnline && m.Type == app.TransactionModal && m.transactionModal.Active && m.transactionModal.ATxn.VotePK == nil {
} else if !isOnline && m.Type == app.TransactionModal && m.transactionModal.OfflineControls && m.transactionModal.ATxn.VotePK == nil {

Check warning on line 143 in ui/modal/controller.go

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L143

Added line #L143 was not covered by tests
m.SetActive(false)
m.infoModal.Prefix = "Successfully registered offline!\n"
m.HasPrefix = true
Expand All @@ -160,13 +159,12 @@
}

if msg.Type == app.InfoModal {
m.infoModal.Prefix = msg.Prefix
m.generateModal.SetStep(generate.AddressStep)
}
// On closing events
if msg.Type == app.CloseModal {
m.Open = false
m.generateModal.Input.Focus()
m.generateModal.AddressInput.Focus()
} else {
m.Open = true
}
Expand All @@ -179,7 +177,7 @@
m.Open = false
m.SetType(app.InfoModal)
m.generateModal.SetStep(generate.AddressStep)
m.generateModal.Input.Focus()
m.generateModal.AddressInput.Focus()
case app.TransactionModal:
m.SetType(app.InfoModal)
case app.ExceptionModal:
Expand Down
8 changes: 8 additions & 0 deletions ui/modal/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package modal

type Modal interface {
Title() string
BorderColor() string
Controls() string
Body() string
}
59 changes: 1 addition & 58 deletions ui/modal/modal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,18 @@ package modal
import (
"bytes"
"errors"
"github.com/algorandfoundation/nodekit/internal/algod/participation"
"github.com/algorandfoundation/nodekit/internal/test/mock"
"github.com/algorandfoundation/nodekit/ui/app"
"github.com/algorandfoundation/nodekit/ui/internal/test"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/charmbracelet/x/exp/teatest"
"testing"
"time"
)

func Test_Snapshot(t *testing.T) {
t.Run("NoKey", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))

model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("InfoModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&mock.Keys[0])
model.SetType(app.InfoModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("ConfirmModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&mock.Keys[0])
model.SetType(app.ConfirmModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("ExceptionModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&mock.Keys[0])
model.SetType(app.ExceptionModal)
model, _ = model.HandleMessage(errors.New("test error"))
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Run("GenerateModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.SetKey(&mock.Keys[0])
model.SetAddress("ABC")
model.SetType(app.GenerateModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})

t.Run("TransactionModal", func(t *testing.T) {
t.Skip("qa is not a priority for this project")
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.State.Status.Network = "testnet-v1.0"
model.SetShortLink(participation.ShortLinkResponse{
Id: "1234",
})
model.SetKey(&mock.Keys[0])
model.SetActive(true)
model.SetType(app.TransactionModal)
model, _ = model.HandleMessage(tea.WindowSizeMsg{Width: 80, Height: 40})
got := ansi.Strip(model.View())
golden.RequireEqual(t, []byte(got))
})
t.Skip("TODO:")
}

func Test_Messages(t *testing.T) {
Expand Down
34 changes: 8 additions & 26 deletions ui/modal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type ViewModel struct {
Link *participation.ShortLinkResponse

// Views
infoModal *info.ViewModel
infoModal info.ViewModel
transactionModal *transaction.ViewModel
confirmModal *confirm.ViewModel
generateModal *generate.ViewModel
confirmModal confirm.ViewModel
generateModal generate.ViewModel
exceptionModal *exception.ViewModel

// Current Component Data
Expand All @@ -51,28 +51,26 @@ type ViewModel struct {
// SetAddress updates the ViewModel's Address property and synchronizes it with the associated generateModal.
func (m *ViewModel) SetAddress(address string) {
m.Address = address
m.generateModal.SetAddress(address)
//m.generateModal.SetAddress(address)
}

// SetKey updates the participation key across infoModal, confirmModal, and transactionModal in the ViewModel.
func (m *ViewModel) SetKey(key *api.ParticipationKey) {
m.infoModal.Participation = key
m.confirmModal.ActiveKey = key
m.confirmModal.Participation = key
m.transactionModal.Participation = key
}

// SetActive sets the active state for both infoModal and transactionModal, and updates their respective states.
func (m *ViewModel) SetActive(active bool) {
m.infoModal.Active = active
m.infoModal.UpdateState()
m.transactionModal.Active = active
m.infoModal.OfflineControls = active
m.transactionModal.OfflineControls = active
m.transactionModal.UpdateState()
}

// SetSuspended sets the suspended state
func (m *ViewModel) SetSuspended(sus bool) {
m.infoModal.Suspended = sus
m.infoModal.UpdateState()
m.transactionModal.Suspended = sus
m.transactionModal.UpdateState()
}
Expand All @@ -86,22 +84,6 @@ func (m *ViewModel) SetShortLink(res participation.ShortLinkResponse) {
func (m *ViewModel) SetType(modal app.ModalType) {
m.Type = modal
switch modal {
case app.InfoModal:
m.title = m.infoModal.Title
m.controls = m.infoModal.Controls
m.borderColor = m.infoModal.BorderColor
case app.ConfirmModal:
m.title = m.confirmModal.Title
m.controls = m.confirmModal.Controls
m.borderColor = m.confirmModal.BorderColor
case app.GenerateModal:
m.title = m.generateModal.Title
m.controls = m.generateModal.Controls
m.borderColor = m.generateModal.BorderColor
case app.TransactionModal:
m.title = m.transactionModal.Title
m.controls = m.transactionModal.Controls
m.borderColor = m.transactionModal.BorderColor
case app.ExceptionModal:
m.title = m.exceptionModal.Title
m.controls = m.exceptionModal.Controls
Expand All @@ -124,7 +106,7 @@ func New(parent string, open bool, state *algod.StateModel) *ViewModel {

infoModal: info.New(state),
transactionModal: transaction.New(state),
confirmModal: confirm.New(state),
confirmModal: confirm.New(state, nil),
generateModal: generate.New("", state),
exceptionModal: exception.New(""),

Expand Down
80 changes: 0 additions & 80 deletions ui/modal/testdata/Test_Snapshot/ConfirmModal.golden

This file was deleted.

Loading
Loading