Skip to content

Commit

Permalink
Pulls forms and data tables into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Nov 3, 2024
1 parent f351d08 commit ea3cc0c
Show file tree
Hide file tree
Showing 92 changed files with 1,133 additions and 931 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"typescript.preferences.importModuleSpecifier": "relative",
"typescript.suggest.autoImports": true,
"javascript.suggest.autoImports": true,
"editor.acceptSuggestionOnEnter": "on"
"editor.acceptSuggestionOnEnter": "on",
"files.associations": {
"*.tbx": "json"
}
}
159 changes: 83 additions & 76 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@ package app
import (
"context"
"encoding/json"
"os"
"path/filepath"
"errors"
"fmt"
"sync"

"github.com/TrueBlocks/trueblocks-browse/pkg/daemons"
"github.com/TrueBlocks/trueblocks-browse/pkg/messages"
"github.com/TrueBlocks/trueblocks-browse/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
coreConfig "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/names"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
coreTypes "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
sdk "github.com/TrueBlocks/trueblocks-sdk/v3"
"github.com/wailsapp/wails/v2/pkg/runtime"
)

type App struct {
sdk.Globals `json:",inline"`
sdk.Globals

ctx context.Context
meta coreTypes.MetaData
dirty bool

renderCtxs map[base.Address][]*output.RenderCtx

// Containers
projects types.ProjectContainer
monitors types.MonitorContainer
Expand All @@ -43,101 +39,112 @@ type App struct {
config types.ConfigContainer

// Memory caches
HistoryCache *types.HistoryMap `json:"historyCache"`
EnsCache *sync.Map `json:"ensCache"`
BalanceCache *sync.Map `json:"balanceCache"`
ensCache *sync.Map
balanceCache *sync.Map
namesMap map[base.Address]coreTypes.Name
historyCache *types.HistoryMap
renderCtxs map[base.Address][]*output.RenderCtx

// Controllers
ScraperController *daemons.DaemonScraper
FreshenController *daemons.DaemonFreshen
IpfsController *daemons.DaemonIpfs
scraperController *daemons.DaemonScraper
freshenController *daemons.DaemonFreshen
ipfsController *daemons.DaemonIpfs

// During initialization, we do things that may cause errors, but
// we have not yet opened the window, so we defer them until we can
// decide what to do.
deferredErrors []error
}

func (a *App) String() string {
bytes, _ := json.Marshal(a)
return string(bytes)
}

func NewApp() *App {
a := App{
renderCtxs: make(map[base.Address][]*output.RenderCtx),
a := &App{
ensCache: &sync.Map{},
balanceCache: &sync.Map{},
namesMap: make(map[base.Address]coreTypes.Name),
historyCache: &types.HistoryMap{},
renderCtxs: make(map[base.Address][]*output.RenderCtx),
}
a.EnsCache = &sync.Map{}
a.BalanceCache = &sync.Map{}
a.HistoryCache = &types.HistoryMap{}
a.names.NamesMap = make(map[base.Address]coreTypes.Name)
a.projects = types.NewProjectContainer("", []types.HistoryContainer{})
a.freshenController = daemons.NewFreshen(a, "freshen", 3000, a.IsShowing("freshen"))
a.scraperController = daemons.NewScraper(a, "scraper", 7000, a.IsShowing("scraper"))
a.ipfsController = daemons.NewIpfs(a, "ipfs", 10000, a.IsShowing("ipfs"))
a.session.LastSub = make(map[string]string)

return &a
return a
}

func (a *App) String() string {
bytes, _ := json.MarshalIndent(a, "", " ")
return string(bytes)
}
var ErrLoadingNames = errors.New("error loading names")
var ErrWindowSize = errors.New("error fixing window size")

func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
a.loadSession()
a.Chain = a.session.LastChain

a.FreshenController = daemons.NewFreshen(a, "freshen", 3000, a.IsShowing("freshen"))
a.ScraperController = daemons.NewScraper(a, "scraper", 7000, a.IsShowing("scraper"))
a.IpfsController = daemons.NewIpfs(a, "ipfs", 10000, a.IsShowing("ipfs"))
go a.startDaemons()
// We do various setups prior to showing the window. Improves interactivity.
// But...something may fail, so we need to keep track of errors.
var err error
if err = a.session.Load(); err != nil {
a.deferredErrors = append(a.deferredErrors, err)
}

fn := filepath.Join(a.session.LastFolder, a.session.LastFile)
if file.FileExists(fn) {
a.LoadFile(fn)
a.dirty = false
} else {
a.dirty = true
// Load the trueBlocks.toml file
if err = a.config.Load(); err != nil {
a.deferredErrors = append(a.deferredErrors, err)
}
if a.session.LastChain, err = a.config.IsValidChain(a.session.LastChain); err != nil {
a.deferredErrors = append(a.deferredErrors, err)
}
a.Chain = a.session.LastChain

logger.Info("Starting freshen process...")
_ = a.Refresh()
// We always need names, so let's load it before showing the window
if a.namesMap, err = names.LoadNamesMap(a.getChain(), coreTypes.All, nil); err == nil {
wErr := fmt.Errorf("%w: %v", ErrLoadingNames, err)
a.deferredErrors = append(a.deferredErrors, wErr)
}
}

// DomReady is called by Wails when the app is ready to go. Adjust the window size and show it.
func (a *App) DomReady(ctx context.Context) {
win := a.GetWindow()
runtime.WindowSetPosition(a.ctx, win.X, win.Y)
runtime.WindowSetSize(a.ctx, win.Width, win.Height)
runtime.WindowShow(a.ctx)
var err error

if path, err := utils.GetConfigFn("", "trueBlocks.toml"); err != nil {
messages.EmitMessage(a.ctx, messages.Error, &messages.MessageMsg{
String1: err.Error(),
})
} else {
if err := coreConfig.ReadToml(path, &a.config.Config); err != nil {
messages.EmitMessage(a.ctx, messages.Error, &messages.MessageMsg{
String1: err.Error(),
})
}
// We're ready to open the window, but first we need to make sure it will show...
if a.session.Window, err = a.session.CleanWindowSize(a.ctx); err != nil {
wErr := fmt.Errorf("%w: %v", ErrWindowSize, err)
a.deferredErrors = append(a.deferredErrors, wErr)
}
// DO NOT COLLAPSE - A VALID WINDOW IS RETURNED EVEN ON ERROR
runtime.WindowSetPosition(a.ctx, a.session.Window.X, a.session.Window.Y)
runtime.WindowSetSize(a.ctx, a.session.Window.Width, a.session.Window.Height)
runtime.WindowShow(a.ctx)
if err != nil {
a.deferredErrors = append(a.deferredErrors, err)
}
}

func (a *App) Shutdown(ctx context.Context) {
a.saveSession()
}
// We now have a window, so we can finally show any accumulated errors
for _, err := range a.deferredErrors {
a.emitErrorMsg(err, nil)
}

func (a *App) saveSession() {
if !isTesting {
a.session.Window.X, a.session.Window.Y = runtime.WindowGetPosition(a.ctx)
a.session.Window.Width, a.session.Window.Height = runtime.WindowGetSize(a.ctx)
a.session.Window.Y += 38 // TODO: This is a hack to account for the menu bar - not sure why it's needed
fn := a.getFullPath()
if file.FileExists(fn) {
a.readFile(fn)
} else {
a.newFile()
}
_ = a.session.Save()
}

func (a *App) loadSession() {
_ = a.session.Load()
a.session.CleanWindowSize(a.ctx)
a.Chain = a.session.LastChain
}
go a.Refresh()

func (a *App) Logger(msg string) {
logger.Info(msg)
}
go a.freshenController.Run()
go a.scraperController.Run()
go a.ipfsController.Run()

var isTesting bool
logger.Info("Fininished loading...")
}

func init() {
isTesting = os.Getenv("TB_TEST_MODE") == "true"
// Shutdown is called by Wails when the app is closed
func (a *App) Shutdown(ctx context.Context) {
a.saveSession()
}
7 changes: 0 additions & 7 deletions app/app_debug.go

This file was deleted.

10 changes: 3 additions & 7 deletions app/app_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ func (a *App) GetEnv(key string) string {
return os.Getenv(key)
}

func (a *App) GetMeta() coreTypes.MetaData {
return a.meta
}

func (a *App) GetAppTitle() string {
return a.session.Window.Title
}

func (a *App) GetRoute() string {
if !a.IsConfigured() {
if !a.isConfigured() {
return "/wizard"
}

Expand All @@ -55,12 +51,12 @@ func (a *App) GetRoute() string {
return route
}

func (a *App) GetAddress() base.Address {
func (a *App) GetSelected() base.Address {
addr := a.session.LastSub["/history"]
return base.HexToAddress(addr)
}

func (a *App) GetChain() string {
func (a *App) getChain() string {
return a.Chain
}

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

import (
"path/filepath"

coreTypes "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

type AppInfo struct {
Chain string `json:"chain"`
Filename string `json:"filename"`
Dirty bool `json:"dirty"`
Meta coreTypes.MetaData `json:"meta"`
State coreTypes.WizState `json:"state"`
IsConfigured bool `json:"isConfigured"`
}

func (a *App) getFolder() string {
return a.session.LastFolder
}

func (a *App) getFilename() string {
return a.session.LastFile
}

func (a *App) getFullPath() string {
return filepath.Join(a.getFolder(), a.getFilename())
}

func (a *App) GetAppInfo() AppInfo {
return AppInfo{
Chain: a.Chain,
Filename: a.getFullPath(),
Dirty: a.dirty,
Meta: a.meta,
State: a.getWizardState(),
IsConfigured: a.isConfigured(),
}
}
6 changes: 3 additions & 3 deletions app/app_setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"os"

"github.com/TrueBlocks/trueblocks-browse/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
)

Expand All @@ -22,11 +21,12 @@ func (a *App) SetRoute(route, subRoute string) {
}

func (a *App) SetChain(chain string, address base.Address) {
a.CancelAllContexts() // cancel what's happening on the old chain
a.emitInfoMsg("Switching to chain", chain)
a.CancelAllContexts()
a.Chain = chain
a.session.LastChain = chain
a.saveSession()
a.Reload(address)
a.GoToAddress(address)
a.monitors = types.MonitorContainer{}
a.names = types.NameContainer{}
a.abis = types.AbiContainer{}
Expand Down
15 changes: 4 additions & 11 deletions app/data_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync/atomic"
"time"

"github.com/TrueBlocks/trueblocks-browse/pkg/messages"
"github.com/TrueBlocks/trueblocks-browse/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
coreTypes "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
Expand Down Expand Up @@ -76,12 +75,10 @@ func (a *App) loadAbis(wg *sync.WaitGroup, errorChan chan error) error {
// EXISTING_CODE
// EXISTING_CODE
if err := sdk.SortAbis(a.abis.Items, a.abis.Sorts); err != nil {
messages.EmitMessage(a.ctx, messages.Error, &messages.MessageMsg{
String1: err.Error(),
})
a.emitErrorMsg(err, nil)
}
a.abis.Summarize()
messages.EmitMessage(a.ctx, messages.Info, &messages.MessageMsg{String1: "Loaded abis"})
a.emitInfoMsg("Loaded abis", "")
}

return nil
Expand All @@ -95,10 +92,7 @@ func (a *App) ModifyAbi(modData *ModifyData) error {
opts.Globals.Decache = true

if _, _, err := opts.Abis(); err != nil {
messages.EmitMessage(a.ctx, messages.Error, &messages.MessageMsg{
String1: err.Error(),
Address: modData.Address,
})
a.emitAddressErrorMsg(err, modData.Address)
return err
} else {
newAbis := make([]coreTypes.Abi, 0, len(a.abis.Items))
Expand All @@ -113,8 +107,7 @@ func (a *App) ModifyAbi(modData *ModifyData) error {
}
a.abis.LastUpdate = time.Time{}
a.abis.Items = newAbis
msg := fmt.Sprintf("ModifyAbi delete: %s", modData.Address.Hex())
messages.EmitMessage(a.ctx, messages.Info, &messages.MessageMsg{String1: msg})
a.emitInfoMsg("ModifyAbi delete", modData.Address.Hex())
return nil
}
}
Expand Down
Loading

0 comments on commit ea3cc0c

Please sign in to comment.