Skip to content

Commit

Permalink
Best Version Yet
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jul 22, 2024
1 parent 59ae70d commit 64dd855
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
40 changes: 37 additions & 3 deletions app/history.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"strings"
"sync"

"github.com/TrueBlocks/trueblocks-core/sdk/v3"
Expand All @@ -12,15 +13,16 @@ import (
var addrToHistoryMap = map[base.Address][]TransactionEx{}
var m = sync.Mutex{}

func (a *App) GetHistory(addr string, first, pageSize int) []TransactionEx {
func (a *App) GetHistoryPage(addr string, first, pageSize int) []TransactionEx {
address := base.HexToAddress(addr)
m.Lock()
defer m.Unlock()

if len(addrToHistoryMap[address]) == 0 {
rCtx := output.NewStreamingContext()
opts := sdk.ExportOptions{
Addrs: []string{addr},
RenderCtx: output.NewStreamingContext(),
RenderCtx: rCtx,
Globals: sdk.Globals{
Cache: true,
Ether: true,
Expand Down Expand Up @@ -69,15 +71,47 @@ func (a *App) GetHistory(addr string, first, pageSize int) []TransactionEx {
}

func (a *App) GetHistoryCnt(addr string) int64 {
address := base.HexToAddress(addr)

opts := sdk.ListOptions{
Addrs: []string{addr},
}
monitors, _, err := opts.ListCount()
if err != nil {
// EventEmitter.Emit("error", err)
a.SendMessage(address, Error, err.Error())
return 0
} else if len(monitors) == 0 {
return 0
}
return monitors[0].NRecords
}

func (a *App) ConvertToAddress(addr string) (base.Address, bool) {
if !strings.HasSuffix(addr, ".eth") {
ret := base.HexToAddress(addr)
return ret, ret != base.ZeroAddr
}

m.Lock()
defer m.Unlock()
if ensAddr, exists := a.ensMap[addr]; exists {
return ensAddr, true
}

// Try to get an ENS or return the same input
opts := sdk.NamesOptions{
Terms: []string{addr},
}
if names, _, err := opts.Names(); err != nil {
a.SendMessage(base.ZeroAddr, Error, err.Error())
return base.ZeroAddr, false
} else {
if len(names) > 0 {
a.ensMap[addr] = names[0].Address
return names[0].Address, true
} else {
ret := base.HexToAddress(addr)
return ret, ret != base.ZeroAddr
}
}
}
22 changes: 12 additions & 10 deletions frontend/src/views/History/HistoryView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from "react";
import classes from "@/App.module.css";
import lClasses from "./HistoryView.module.css";
import { GetHistory, GetHistoryCnt } from "@gocode/app/App";
import { GetHistoryPage, GetHistoryCnt } from "@gocode/app/App";
import { app } from "@gocode/models";
import { flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useHotkeys } from "react-hotkeys-hook";
Expand All @@ -19,29 +19,31 @@ export function HistoryView() {
// const [address, setAddress] = useState<string>("0x1db3439a222c519ab44bb1144fc28167b4fa6ee6");
// const [address, setAddress] = useState<string>("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
// const [address, setAddress] = useState<string>("0x9531c059098e3d194ff87febb587ab07b30b1306");
const [address, setAddress] = useState<string>("0xf503017d7baf7fbc0fff7492b751025c6a78179b");
// const [address, setAddress] = useState<string>("0xf503017d7baf7fbc0fff7492b751025c6a78179b");
const [address, setAddress] = useState<string>("trueblocks.eth");
const [count, setCount] = useState<number>(0);
const [loading, setLoading] = useState<boolean>(false);
const [loaded, setLoaded] = useState<boolean>(false);
const [items, setItems] = useState<app.TransactionEx[]>([]);
const { curItem, perPage } = useKeyboardPaging2<app.TransactionEx>(items, count, [address]);

const fetch = async (addr: string, currentItem: number, itemsPerPage: number) => {
const cnt = await GetHistoryCnt(addr);
setCount(cnt);
const newItems = await GetHistory(addr, currentItem, itemsPerPage);
setItems(newItems);
};

useEffect(() => {
if (loaded && !loading) {
const fetch = async (addr: string, currentItem: number, itemsPerPage: number) => {
const newItems = await GetHistoryPage(addr, currentItem, itemsPerPage);
setItems(newItems);
};
fetch(address, curItem, perPage);
}
}, [curItem, perPage]);
}, [count, curItem, perPage]);

useEffect(() => {
try {
setLoading(true);
const fetch = async (addr: string, currentItem: number, itemsPerPage: number) => {
const cnt = await GetHistoryCnt(addr);
setCount(cnt);
};
fetch(address, curItem, perPage);
setLoaded(true);
} finally {
Expand Down
6 changes: 4 additions & 2 deletions frontend/wailsjs/go/app/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {output} from '../models';

export function Cancel(arg1:base.Address):Promise<number|boolean>;

export function ConvertToAddress(arg1:string):Promise<base.Address|boolean>;

export function GenerateEnhanced(arg1:string):Promise<string>;

export function GenerateImage(arg1:string):Promise<string>;
Expand All @@ -23,10 +25,10 @@ export function GetFilelist(arg1:string):Promise<Array<string>>;

export function GetFilename(arg1:string):Promise<string>;

export function GetHistory(arg1:string,arg2:number,arg3:number):Promise<Array<app.TransactionEx>>;

export function GetHistoryCnt(arg1:string):Promise<number>;

export function GetHistoryPage(arg1:string,arg2:number,arg3:number):Promise<Array<app.TransactionEx>>;

export function GetJson(arg1:string):Promise<string>;

export function GetLast(arg1:string):Promise<string>;
Expand Down
12 changes: 8 additions & 4 deletions frontend/wailsjs/go/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function Cancel(arg1) {
return window['go']['app']['App']['Cancel'](arg1);
}

export function ConvertToAddress(arg1) {
return window['go']['app']['App']['ConvertToAddress'](arg1);
}

export function GenerateEnhanced(arg1) {
return window['go']['app']['App']['GenerateEnhanced'](arg1);
}
Expand Down Expand Up @@ -34,14 +38,14 @@ export function GetFilename(arg1) {
return window['go']['app']['App']['GetFilename'](arg1);
}

export function GetHistory(arg1, arg2, arg3) {
return window['go']['app']['App']['GetHistory'](arg1, arg2, arg3);
}

export function GetHistoryCnt(arg1) {
return window['go']['app']['App']['GetHistoryCnt'](arg1);
}

export function GetHistoryPage(arg1, arg2, arg3) {
return window['go']['app']['App']['GetHistoryPage'](arg1, arg2, arg3);
}

export function GetJson(arg1) {
return window['go']['app']['App']['GetJson'](arg1);
}
Expand Down

0 comments on commit 64dd855

Please sign in to comment.