Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render.gno

2.47 Kb · 106 lines
  1package item
  2
  3import (
  4	"net/url"
  5	"strconv"
  6	"strings"
  7
  8	"gno.land/p/jeronimoalbi/pager/v0"
  9)
 10
 11func Render(iURL string) string {
 12	assertRenderURLLen(iURL)
 13	u, err := url.Parse(iURL)
 14	if err != nil {
 15		return renderItemError("Invalid URL")
 16	}
 17
 18	switch u.Path {
 19	case "":
 20		return renderItemHome()
 21	case "owner":
 22		return renderItemOwner(iURL, address(u.Query().Get("address")))
 23	default:
 24		return renderItemError("Not found")
 25	}
 26}
 27
 28func renderItemHome() string {
 29	return `# AKKADIA Item
 30
 31Look up fungible items held by an address.
 32
 33<gno-form path="owner">
 34  <gno-input name="address" placeholder="Enter the address" />
 35</gno-form>
 36`
 37}
 38
 39func renderItemOwner(iURL string, owner address) string {
 40	if !owner.IsValid() {
 41		return renderItemError("Invalid address")
 42	}
 43
 44	output := `# Item Holdings
 45
 46[← Home](./item:)
 47
 48* **Address**: ` + owner.String() + `
 49`
 50
 51	total := GetOwnedCount(owner)
 52	output += "* **Total**: " + strconv.Itoa(total) + "\n\n"
 53	if total == 0 {
 54		return output + `*No items held.*
 55`
 56	}
 57
 58	pageSize := 20
 59	if pageSize > listLimit {
 60		pageSize = listLimit
 61	}
 62	pages, err := pager.New(iURL, total, pager.WithPageSize(pageSize))
 63	if err != nil {
 64		return renderItemError("Invalid page")
 65	}
 66
 67	rows := ListByOwner(owner, pages.Page(), pages.PageSize())
 68	for _, row := range rows {
 69		output += "* **" + escapeItemMarkdown(row["tokenId"]) + "**: " + escapeItemMarkdown(row["amount"]) + "\n"
 70	}
 71	if pages.HasPages() {
 72		output += "\n" + pager.Picker(pages)
 73	}
 74	return output
 75}
 76
 77func renderItemError(message string) string {
 78	return `# Error
 79
 80> ` + escapeItemMarkdown(message) + `
 81
 82[← Home](./item:)
 83`
 84}
 85
 86func escapeItemMarkdown(value string) string {
 87	value = strings.ReplaceAll(value, "\\", "\\\\")
 88	value = strings.ReplaceAll(value, "\n", "\\n")
 89	value = strings.ReplaceAll(value, "`", "\\`")
 90	value = strings.ReplaceAll(value, "*", "\\*")
 91	value = strings.ReplaceAll(value, "_", "\\_")
 92	value = strings.ReplaceAll(value, "{", "\\{")
 93	value = strings.ReplaceAll(value, "}", "\\}")
 94	value = strings.ReplaceAll(value, "[", "\\[")
 95	value = strings.ReplaceAll(value, "]", "\\]")
 96	value = strings.ReplaceAll(value, "(", "\\(")
 97	value = strings.ReplaceAll(value, ")", "\\)")
 98	value = strings.ReplaceAll(value, "#", "\\#")
 99	value = strings.ReplaceAll(value, "+", "\\+")
100	value = strings.ReplaceAll(value, "-", "\\-")
101	value = strings.ReplaceAll(value, ".", "\\.")
102	value = strings.ReplaceAll(value, "!", "\\!")
103	value = strings.ReplaceAll(value, "|", "\\|")
104	value = strings.ReplaceAll(value, ">", "\\>")
105	return value
106}