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.89 Kb · 117 lines
  1package equipment
  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 renderEquipmentError("Invalid URL")
 16	}
 17
 18	switch u.Path {
 19	case "":
 20		return renderEquipmentHome()
 21	case "owner":
 22		return renderEquipmentOwner(iURL, address(u.Query().Get("address")))
 23	default:
 24		return renderEquipmentError("Not found")
 25	}
 26}
 27
 28func renderEquipmentHome() string {
 29	return `# AKKADIA Equipment
 30
 31Look up equipment 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 renderEquipmentOwner(iURL string, owner address) string {
 40	if !owner.IsValid() {
 41		return renderEquipmentError("Invalid address")
 42	}
 43
 44	output := `# Equipment Holdings
 45
 46[← Home](./equipment:)
 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 equipment 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 renderEquipmentError("Invalid page")
 65	}
 66
 67	rows := ListByOwner(owner, pages.Page(), pages.PageSize())
 68	for _, row := range rows {
 69		output += formatEquipmentHolding(row)
 70	}
 71	if pages.HasPages() {
 72		output += "\n" + pager.Picker(pages)
 73	}
 74	return output
 75}
 76
 77func formatEquipmentHolding(row map[string]string) string {
 78	line := "* **" + escapeEquipmentMarkdown(row[tokenIDKey]) + "**: definitionId " + escapeEquipmentMarkdown(row[definitionIDKey])
 79	for key, value := range row {
 80		if key == tokenIDKey || key == definitionIDKey || key == createdAtKey {
 81			continue
 82		}
 83		line += " · " + escapeEquipmentMarkdown(key) + " " + escapeEquipmentMarkdown(value)
 84	}
 85	return line + "\n"
 86}
 87
 88func renderEquipmentError(message string) string {
 89	return `# Error
 90
 91> ` + escapeEquipmentMarkdown(message) + `
 92
 93[← Home](./equipment:)
 94`
 95}
 96
 97func escapeEquipmentMarkdown(value string) string {
 98	value = strings.ReplaceAll(value, "\\", "\\\\")
 99	value = strings.ReplaceAll(value, "\n", "\\n")
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	value = strings.ReplaceAll(value, "[", "\\[")
106	value = strings.ReplaceAll(value, "]", "\\]")
107	value = strings.ReplaceAll(value, "(", "\\(")
108	value = strings.ReplaceAll(value, ")", "\\)")
109	value = strings.ReplaceAll(value, "#", "\\#")
110	value = strings.ReplaceAll(value, "+", "\\+")
111	value = strings.ReplaceAll(value, "-", "\\-")
112	value = strings.ReplaceAll(value, ".", "\\.")
113	value = strings.ReplaceAll(value, "!", "\\!")
114	value = strings.ReplaceAll(value, "|", "\\|")
115	value = strings.ReplaceAll(value, ">", "\\>")
116	return value
117}