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

initial UI spike #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
159 changes: 159 additions & 0 deletions cmd/gigawallet/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package main

import (
"cmp"
"encoding/json"
"fmt"
"log"
"net/url"
"slices"
"strings"

giga "github.com/dogecoinfoundation/gigawallet/pkg"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type BrowserState struct {
Accounts map[string]*BrowserAccount
Current *BrowserAccount
}

type BrowserAccount struct {
ID string
Invoices []giga.PublicInvoice
}

func (b BrowserAccount) updateInvoices(c giga.Config) {
url := adminURL(c, fmt.Sprintf("account/%s/invoices", b.ID))
s, err := getURL(url)
if err != nil {
log.Fatalf("failed to get blasdfhjafdk", err)
}
if err := json.NewDecoder(strings.NewReader(s)).Decode(&b.Invoices); err != nil {
log.Fatalf("bad bad", err)
// bad
}
}

type Browser struct {
newAccountField *tview.InputField
accountList *tview.List
pages *tview.Pages
state *BrowserState
}

func LaunchBrowser(conf giga.Config) {

app := tview.NewApplication().EnableMouse(true)
s := BrowserState{
Accounts: map[string]*BrowserAccount{},
}
b := Browser{
state: &s,
pages: tview.NewPages(),
}
b.loadState()
b.buildMainView()
b.refreshUI()
if err := app.SetRoot(b.pages, true).SetFocus(b.pages).Run(); err != nil {
panic(err)
}
}

func (b *Browser) loadState() {
// TODO: load from .. something?
data := []string{"crumpets", "raffe", "tjs", "inevitable", "bluezr", "michi", "ed", "Marshall", "Jens"}
for _, n := range data {
b.state.Accounts[n] = &BrowserAccount{ID: n}
}
b.state.Current = b.state.Accounts[data[0]]
}

func (b *Browser) buildMainView() {
newPrimitive := func(text string) tview.Primitive {
return tview.NewTextView().
SetTextAlign(tview.AlignCenter).
SetText(text)
}

accUI, input, list := b.buildAccountList(b.state)
b.accountList = list
b.newAccountField = input

grid := tview.NewGrid().
SetRows(1, 0).
SetColumns(30, 0).
SetBorders(true).
AddItem(newPrimitive("🐶 GigaWallet Browser"), 0, 0, 1, 2, 0, 0, false).
AddItem(accUI, 1, 0, 1, 1, 0, 15, true).
AddItem(newPrimitive("Invoices"), 1, 1, 1, 1, 0, 0, false)

b.pages.AddPage("main", grid, true, true)
}

func (b *Browser) switchAccount(a *BrowserAccount) {
b.state.Current = a
b.refreshUI()
}

func (b *Browser) refreshUI() {
b.updateAccountList()
}

func (b *Browser) updateAccountList() {
b.accountList.Clear()
accounts := []*BrowserAccount{}
for _, acc := range b.state.Accounts {
accounts = append(accounts, acc)
}
slices.SortFunc(accounts, func(a, b *BrowserAccount) int {
return cmp.Compare(a.ID, b.ID)
})

selected := 0
for i, acc := range accounts {
name := fmt.Sprintf(" %s", acc.ID)
if acc.ID == b.state.Current.ID {
selected = i
name = fmt.Sprintf("> %s", acc.ID)
}
accCopy := acc // for closure, because Go is weird
b.accountList.AddItem(name, "", rune(0), func() {
b.switchAccount(accCopy)
})
}
b.accountList.SetCurrentItem(selected)
}

func (b *Browser) buildAccountList(s *BrowserState) (*tview.Grid, *tview.InputField, *tview.List) {
newAccountField := tview.NewInputField().
SetLabel("Find Account: ").
SetFieldWidth(15).
SetDoneFunc(func(key tcell.Key) {
// save a thing
})
list := tview.NewList().
SetHighlightFullLine(true).
ShowSecondaryText(false)

grid := tview.NewGrid().
SetRows(1, 0).
SetColumns(0).
SetBorders(false).
AddItem(newAccountField, 0, 0, 1, 1, 0, 0, true).
AddItem(list, 1, 0, 1, 1, 0, 0, false)

return grid, newAccountField, list
}

func adminURL(c giga.Config, path string) string {
host := c.WebAPI.AdminBind
if host == "" {
host = "localhost"
}
base := fmt.Sprintf("http://%s:%s/", host, c.WebAPI.AdminPort)
u, _ := url.Parse(base)
p, _ := url.Parse(path)
return u.ResolveReference(p).String()
}
20 changes: 20 additions & 0 deletions cmd/gigawallet/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -93,3 +94,22 @@ func postURL(url string, body interface{}) error {

return nil
}

func getURL(url string) (string, error) {
response, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("HTTP GET request failed:", err)
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP request failed with status code: %d", response.StatusCode)
}

data, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("Error reading response body:", err)
}

return string(data), nil
}
3 changes: 3 additions & 0 deletions cmd/gigawallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func main() {
case "server":
Server(config)
os.Exit(0)
case "browser":
LaunchBrowser(config)
os.Exit(0)
case "printconf":
o, _ := json.MarshalIndent(config, ">", " ")
fmt.Println(string(o))
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ require (
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/tview v0.0.0-20230909130259-ba6a2a345459 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/yosssi/gmq v0.0.1 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
42 changes: 42 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/dogeorg/go-libdogecoin v0.0.45 h1:M4V69ho6cdE/Xw9KyLxwErC17RFHIOZrNvsONKxPvEs=
github.com/dogeorg/go-libdogecoin v0.0.45/go.mod h1:1ByEB/S1JNzgYy3bwZfeTXf+3683dxALhmYbw4B/4B0=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/pebbe/zmq4 v1.2.9 h1:JlHcdgq6zpppNR1tH0wXJq0XK03pRUc4lBlHTD7aj/4=
github.com/pebbe/zmq4 v1.2.9/go.mod h1:nqnPueOapVhE2wItZ0uOErngczsJdLOGkebMxaO8r48=
github.com/rivo/tview v0.0.0-20230909130259-ba6a2a345459 h1:siWUqEVzxnotJ195QmJ05UyP6PSFfYmexlte3piUPDg=
github.com/rivo/tview v0.0.0-20230909130259-ba6a2a345459/go.mod h1:nVwGv4MP47T0jvlk7KuTTjjuSmrGO4JF0iaiNt4bufE=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
Expand All @@ -24,6 +37,35 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yosssi/gmq v0.0.1 h1:GhlDVaAQoi3Mvjul/qJXXGfL4JBeE0GQwbWp3eIsja8=
github.com/yosssi/gmq v0.0.1/go.mod h1:mReykazh0U1JabvuWh1PEbzzJftqOQWsjr0Lwg5jL1Y=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
Expand Down