Skip to content

Commit

Permalink
feat: short link tui render
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 16, 2024
1 parent a917fb8 commit 856a43a
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 121 deletions.
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ var (
ParticipationKeys: partkeys,

Client: client,
Http: new(internal.HttpPkg),
Context: ctx,
}
state.Accounts, err = internal.AccountsFromState(&state, new(internal.Clock), client)
cobra.CheckErr(err)
// Fetch current state
err = state.Status.Fetch(ctx, client, new(internal.HttpPkg))
err = state.Status.Fetch(ctx, client, state.Http)
cobra.CheckErr(err)

m, err := ui.NewViewportViewModel(&state, client)
Expand Down
14 changes: 10 additions & 4 deletions internal/participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ type OnlineShortLinkBody struct {
Network string `json:"network"`
}

// ToOnlineShortLink sends a POST request to create an online short link
// GetOnlineShortLink sends a POST request to create an online short link
// and returns the response or an error if it occurs.
func ToOnlineShortLink(http HttpPkgInterface, part OnlineShortLinkBody) (ShortLinkResponse, error) {
func GetOnlineShortLink(http HttpPkgInterface, part OnlineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(part)
if err != nil {
Expand Down Expand Up @@ -190,10 +190,10 @@ type OfflineShortLinkBody struct {
Network string `json:"network"`
}

// ToOfflineShortLink sends an OnlineShortLinkBody to create an offline short link and returns the corresponding response.
// GetOfflineShortLink sends an OnlineShortLinkBody to create an offline short link and returns the corresponding response.
// Uses the provided HttpPkgInterface for the POST request and handles JSON encoding/decoding of request and response.
// Returns an OfflineShortLinkResponse on success or an error if the operation fails.
func ToOfflineShortLink(http HttpPkgInterface, offline OfflineShortLinkBody) (ShortLinkResponse, error) {
func GetOfflineShortLink(http HttpPkgInterface, offline OfflineShortLinkBody) (ShortLinkResponse, error) {
var response ShortLinkResponse
data, err := json.Marshal(offline)
if err != nil {
Expand All @@ -212,3 +212,9 @@ func ToOfflineShortLink(http HttpPkgInterface, offline OfflineShortLinkBody) (Sh

return response, nil
}

// ToShortLink generates a shortened URL string using the unique
// identifier from the provided ShortLinkResponse.
func ToShortLink(link ShortLinkResponse) string {
return fmt.Sprintf("https://b.nodekit.run/%s", link.Id)
}
4 changes: 2 additions & 2 deletions internal/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (testOnlineShortner) Post(url string, bodyType string, body io.Reader) (res
}, nil
}
func Test_ToOnlineShortLink(t *testing.T) {
link, err := ToOnlineShortLink(new(testOnlineShortner), OnlineShortLinkBody{
link, err := GetOnlineShortLink(new(testOnlineShortner), OnlineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
VoteKeyB64: "WWHePYtNZ2T3sHkqdd/38EvoFWrnIKPrTo6xN/4T1l4=",
SelectionKeyB64: "e4kBLu7zXOorjLVzJHOiAn+IhOBsYBCqqHKaJCiCdJs=",
Expand Down Expand Up @@ -288,7 +288,7 @@ func (testOfflineShortner) Post(url string, bodyType string, body io.Reader) (re
}, nil
}
func Test_ToOfflineShortLink(t *testing.T) {
link, err := ToOfflineShortLink(new(testOfflineShortner), OfflineShortLinkBody{
link, err := GetOfflineShortLink(new(testOfflineShortner), OfflineShortLinkBody{
Account: "JPEGRZ6G4IBZCOC7UV6QZWJ6TENNKRIPENUJTLG5K7PKIKMVTJHUGERARE",
Network: "mainnet",
})
Expand Down
1 change: 1 addition & 0 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type StateModel struct {

// RPC
Client api.ClientWithResponsesInterface
Http HttpPkgInterface
Context context.Context
}

Expand Down
47 changes: 47 additions & 0 deletions ui/app/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package app

import (
"encoding/base64"
"github.com/algorandfoundation/algorun-tui/api"
"github.com/algorandfoundation/algorun-tui/internal"
tea "github.com/charmbracelet/bubbletea"
)

func EmitCreateShortLink(offline bool, part *api.ParticipationKey, state *internal.StateModel) tea.Cmd {
if part == nil || state == nil {
return nil
}

Check warning on line 13 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L12-L13

Added lines #L12 - L13 were not covered by tests
if offline {
res, err := internal.GetOfflineShortLink(state.Http, internal.OfflineShortLinkBody{
Account: part.Address,
Network: state.Status.Network,
})
if err != nil {
return func() tea.Msg {
return err
}

Check warning on line 22 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L15-L22

Added lines #L15 - L22 were not covered by tests
}
return func() tea.Msg {
return res
}

Check warning on line 26 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L24-L26

Added lines #L24 - L26 were not covered by tests
}

res, err := internal.GetOnlineShortLink(state.Http, internal.OnlineShortLinkBody{
Account: part.Address,
VoteKeyB64: base64.RawURLEncoding.EncodeToString(part.Key.VoteParticipationKey),
SelectionKeyB64: base64.RawURLEncoding.EncodeToString(part.Key.SelectionParticipationKey),
StateProofKeyB64: base64.RawURLEncoding.EncodeToString(*part.Key.StateProofKey),
VoteFirstValid: part.Key.VoteFirstValid,
VoteLastValid: part.Key.VoteLastValid,
KeyDilution: part.Key.VoteKeyDilution,
Network: state.Status.Network,
})
if err != nil {
return func() tea.Msg {
return err
}
}
return func() tea.Msg {
return res
}

Check warning on line 46 in ui/app/url.go

View check run for this annotation

Codecov / codecov/patch

ui/app/url.go#L44-L46

Added lines #L44 - L46 were not covered by tests
}
1 change: 1 addition & 0 deletions ui/internal/test/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func GetState(client api.ClientWithResponsesInterface) *internal.StateModel {
Admin: false,
Watching: false,
Client: client,
Http: new(internal.HttpPkg),
Context: context.Background(),
}
values := make(map[string]internal.Account)
Expand Down
4 changes: 4 additions & 0 deletions ui/modal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
m.Open = true
m.exceptionModal.Message = msg.Error()
m.SetType(app.ExceptionModal)
case internal.ShortLinkResponse:
m.Open = true
m.SetShortLink(msg)
m.SetType(app.TransactionModal)

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

View check run for this annotation

Codecov / codecov/patch

ui/modal/controller.go#L31-L34

Added lines #L31 - L34 were not covered by tests
case internal.StateModel:
m.State = &msg
m.transactionModal.State = &msg
Expand Down
4 changes: 4 additions & 0 deletions ui/modal/modal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package modal
import (
"bytes"
"errors"
"github.com/algorandfoundation/algorun-tui/internal"
"github.com/algorandfoundation/algorun-tui/internal/test/mock"
"github.com/algorandfoundation/algorun-tui/ui/app"
"github.com/algorandfoundation/algorun-tui/ui/internal/test"
Expand Down Expand Up @@ -60,6 +61,9 @@ func Test_Snapshot(t *testing.T) {
t.Run("TransactionModal", func(t *testing.T) {
model := New(lipgloss.NewStyle().Width(80).Height(80).Render(""), true, test.GetState(nil))
model.State.Status.Network = "testnet-v1.0"
model.SetShortLink(internal.ShortLinkResponse{
Id: "1234",
})
model.SetKey(&mock.Keys[0])
model.SetActive(true)
model.SetType(app.TransactionModal)
Expand Down
9 changes: 9 additions & 0 deletions ui/modal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type ViewModel struct {
// Address defines the string format address of the entity
Address string

// Link represents a reference to a ShortLinkResponse,
// typically used for processing or displaying shortened link data.
Link *internal.ShortLinkResponse

// Views
infoModal *info.ViewModel
transactionModal *transaction.ViewModel
Expand Down Expand Up @@ -56,6 +60,11 @@ func (m *ViewModel) SetActive(active bool) {
m.transactionModal.UpdateState()
}

func (m *ViewModel) SetShortLink(res internal.ShortLinkResponse) {
m.Link = &res
m.transactionModal.Link = &res
}

func (m *ViewModel) SetType(modal app.ModalType) {
m.Type = modal
switch modal {
Expand Down
58 changes: 29 additions & 29 deletions ui/modal/testdata/Test_Snapshot/TransactionModal.golden
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@



╭──Register Offline─────────────────────────────────────────╮
Sign this transaction to deregister your account keys:
Scan the QR code with Pera or Defly │
│ (make sure you use the testnet-v1.0 network) │
█████████████████████████████ │
██ ▄▄▄▄▄ █▀█▄▀█▀█ ▀█ ▄▄▄▄▄ ██ │
██ █ █ ██▀▄▀ █ █ █ █ █ ██ │
██ █▄▄▄█ █▄▀▀▀█▀ ██ █▄▄▄█ ██ │
██▄▄▄▄▄▄▄█▄█▄█▄▀ ▀ █▄▄▄▄▄▄▄██ │
██▄▀ ▀ █▄▀▀ ▄█▄ █▀▀▀▀▄██ ▀██ │
███▀ ▄▀▄▄▀▀█▄█▄▀▀█▀▄▀▀▀▄█ ▄██ │
███▀█ ▀▄ █ ▄▀▄▄█▄█▀ ██▀ ▀██ │
██▄▀█ ▄▄ █▄ █▀ ██▄▀█▄▄█▄▄██ │
██▄███▄█▄▄▀▄▀█ █▀ ▄▄▄ █▀ ██ │
██ ▄▄▄▄▄ ██▄ ▀▀▀▀█ █▄█ ██ ██ │
██ █ █ █▄ ▀█ ▄▀▀ ▄▄ ▀█████ │
██ █▄▄▄█ █▄▀ █▄▀ ▀▄▀▄ ▄▄▀▄██ │
██▄▄▄▄▄▄▄█▄█▄██▄█▄▄▄▄▄███▄▄██ │
│ -or- │
Click here to sign via Lora.
Note: this will take effect after 320 rounds (15 mins.)
│ Please keep your node online during this cooldown period.
╰────────────────────────────────────────────────( esc )────╯
╭──Register Offline─────────────────────────────────────────╮
Sign this transaction to register your account as offline:
Scan the QR code with Pera or Defly │
│ (make sure you use the testnet-v1.0 network)
█████████████████████████████ │
██ ▄▄▄▄▄ █▀█▄▀█▀█ ▀█ ▄▄▄▄▄ ██ │
██ █ █ ██▀▄▀ █ █ █ █ █ ██ │
██ █▄▄▄█ █▄▀▀▀█▀ ██ █▄▄▄█ ██ │
██▄▄▄▄▄▄▄█▄█▄█▄▀ ▀ █▄▄▄▄▄▄▄██ │
██▄▀ ▀ █▄▀▀ ▄█▄ █▀▀▀▀▄██ ▀██ │
███▀ ▄▀▄▄▀▀█▄█▄▀▀█▀▄▀▀▀▄█ ▄██ │
███▀█ ▀▄ █ ▄▀▄▄█▄█▀ ██▀ ▀██ │
██▄▀█ ▄▄ █▄ █▀ ██▄▀█▄▄█▄▄██ │
██▄███▄█▄▄▀▄▀█ █▀ ▄▄▄ █▀ ██ │
██ ▄▄▄▄▄ ██▄ ▀▀▀▀█ █▄█ ██ ██ │
██ █ █ █▄ ▀█ ▄▀▀ ▄▄ ▀█████ │
██ █▄▄▄█ █▄▀ █▄▀ ▀▄▀▄ ▄▄▀▄██ │
██▄▄▄▄▄▄▄█▄█▄██▄█▄▄▄▄▄███▄▄██ │
│ -or-
Open this URL in your browser:
https://b.nodekit.run/1234
│ Note: this will take effect after 320 rounds (~15 min.)
│ Please keep your node running during this cooldown period. │
╰─────────────────────────────────────────────────( esc )────╯



Expand Down
2 changes: 1 addition & 1 deletion ui/modals/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (*ViewModel, tea.Cmd) {
return &m, app.EmitShowModal(app.ConfirmModal)
}
case "o":
return &m, app.EmitShowModal(app.TransactionModal)
return &m, app.EmitCreateShortLink(m.Active, m.Participation, m.State)
}
case tea.WindowSizeMsg:
m.Width = msg.Width
Expand Down
1 change: 1 addition & 0 deletions ui/modals/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ViewModel struct {
// Active Participation Key
Participation *api.ParticipationKey
Active bool
Link *internal.ShortLinkResponse

// Pointer to the State
State *internal.StateModel
Expand Down
15 changes: 8 additions & 7 deletions ui/modals/transaction/testdata/Test_Snapshot/NotVisible.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Sign this transaction to register your account keys:

Mobile QR is available but it does not fit on screen.
Adjust terminal dimensions or font size to display.

-or-
Click here to sign via Lora.
Sign this transaction to register your account as online:

Mobile QR is available but it does not fit on screen.
Adjust terminal dimensions or font size to display.

-or-
Open this URL in your browser:
https://b.nodekit.run/1234
53 changes: 27 additions & 26 deletions ui/modals/transaction/testdata/Test_Snapshot/Offline.golden
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
Sign this transaction to deregister your account keys:

Scan the QR code with Pera or Defly
(make sure you use the testnet-v1.0 network)

█████████████████████████████
██ ▄▄▄▄▄ █▀█▄▀█▀█ ▀█ ▄▄▄▄▄ ██
██ █ █ ██▀▄▀ █ █ █ █ █ ██
██ █▄▄▄█ █▄▀▀▀█▀ ██ █▄▄▄█ ██
██▄▄▄▄▄▄▄█▄█▄█▄▀ ▀ █▄▄▄▄▄▄▄██
██▄▀ ▀ █▄▀▀ ▄█▄ █▀▀▀▀▄██ ▀██
███▀ ▄▀▄▄▀▀█▄█▄▀▀█▀▄▀▀▀▄█ ▄██
███▀█ ▀▄ █ ▄▀▄▄█▄█▀ ██▀ ▀██
██▄▀█ ▄▄ █▄ █▀ ██▄▀█▄▄█▄▄██
██▄███▄█▄▄▀▄▀█ █▀ ▄▄▄ █▀ ██
██ ▄▄▄▄▄ ██▄ ▀▀▀▀█ █▄█ ██ ██
██ █ █ █▄ ▀█ ▄▀▀ ▄▄ ▀█████
██ █▄▄▄█ █▄▀ █▄▀ ▀▄▀▄ ▄▄▀▄██
██▄▄▄▄▄▄▄█▄█▄██▄█▄▄▄▄▄███▄▄██

-or-

Click here to sign via Lora.

Note: this will take effect after 320 rounds (15 mins.)
Please keep your node online during this cooldown period.
Sign this transaction to register your account as offline:

Scan the QR code with Pera or Defly
(make sure you use the testnet-v1.0 network)

█████████████████████████████
██ ▄▄▄▄▄ █▀█▄▀█▀█ ▀█ ▄▄▄▄▄ ██
██ █ █ ██▀▄▀ █ █ █ █ █ ██
██ █▄▄▄█ █▄▀▀▀█▀ ██ █▄▄▄█ ██
██▄▄▄▄▄▄▄█▄█▄█▄▀ ▀ █▄▄▄▄▄▄▄██
██▄▀ ▀ █▄▀▀ ▄█▄ █▀▀▀▀▄██ ▀██
███▀ ▄▀▄▄▀▀█▄█▄▀▀█▀▄▀▀▀▄█ ▄██
███▀█ ▀▄ █ ▄▀▄▄█▄█▀ ██▀ ▀██
██▄▀█ ▄▄ █▄ █▀ ██▄▀█▄▄█▄▄██
██▄███▄█▄▄▀▄▀█ █▀ ▄▄▄ █▀ ██
██ ▄▄▄▄▄ ██▄ ▀▀▀▀█ █▄█ ██ ██
██ █ █ █▄ ▀█ ▄▀▀ ▄▄ ▀█████
██ █▄▄▄█ █▄▀ █▄▀ ▀▄▀▄ ▄▄▀▄██
██▄▄▄▄▄▄▄█▄█▄██▄█▄▄▄▄▄███▄▄██

-or-

Open this URL in your browser:
https://b.nodekit.run/1234

Note: this will take effect after 320 rounds (~15 min.)
Please keep your node running during this cooldown period.
Loading

0 comments on commit 856a43a

Please sign in to comment.