render.gno

2.45 Kb ยท 111 lines
  1package hof
  2
  3import (
  4	"strings"
  5
  6	"gno.land/p/demo/avl/pager"
  7	"gno.land/p/demo/fqname"
  8	"gno.land/p/demo/seqid"
  9	"gno.land/p/demo/ufmt"
 10	"gno.land/p/moul/txlink"
 11)
 12
 13const (
 14	pageSize = 5
 15)
 16
 17func Render(path string) string {
 18	out := "# Hall of Fame\n\n"
 19
 20	dashboardEnabled := path == "dashboard"
 21
 22	if dashboardEnabled {
 23		out += renderDashboard()
 24	}
 25
 26	out += exhibition.Render(path, dashboardEnabled)
 27
 28	return out
 29}
 30
 31func (e Exhibition) Render(path string, dashboard bool) string {
 32	out := ufmt.Sprintf("%s\n\n", e.description)
 33
 34	if e.items.Size() == 0 {
 35		out += "No items in this exhibition currently.\n\n"
 36		return out
 37	}
 38
 39	out += "<div class='columns-2'>\n\n"
 40
 41	page := pager.NewPager(e.itemsSorted, pageSize, true).MustGetPageByPath(path)
 42
 43	for _, item := range page.Items {
 44		out += "<div>\n\n"
 45		id, _ := seqid.FromString(item.Key)
 46		out += ufmt.Sprintf("### Submission #%d\n\n", int(id))
 47		out += item.Value.(*Item).Render(dashboard)
 48		out += "</div>"
 49	}
 50
 51	out += "</div><!-- /columns-2 -->\n\n"
 52
 53	out += page.Picker()
 54
 55	return out
 56}
 57
 58func (i Item) Render(dashboard bool) string {
 59	out := ufmt.Sprintf("\n```\n%s\n```\n\n", i.pkgpath)
 60	out += ufmt.Sprintf("by %s\n\n", strings.Split(i.pkgpath, "/")[2])
 61	out += ufmt.Sprintf("[View realm](%s)\n\n", strings.TrimPrefix(i.pkgpath, "gno.land")) // gno.land/r/leon/home > /r/leon/home
 62	out += ufmt.Sprintf("Submitted at Block #%d\n\n", i.blockNum)
 63
 64	out += ufmt.Sprintf("**[%d๐Ÿ‘](%s) - [%d๐Ÿ‘Ž](%s)**\n\n",
 65		i.upvote.Size(), txlink.Call("Upvote", "pkgpath", i.pkgpath),
 66		i.downvote.Size(), txlink.Call("Downvote", "pkgpath", i.pkgpath),
 67	)
 68
 69	if dashboard {
 70		out += ufmt.Sprintf("[Delete](%s)", txlink.Call("Delete", "pkgpath", i.pkgpath))
 71	}
 72
 73	return out
 74}
 75
 76func renderDashboard() string {
 77	out := "---\n\n"
 78	out += "## Dashboard\n\n"
 79	out += ufmt.Sprintf("Total submissions: %d\n\n", exhibition.items.Size())
 80
 81	out += ufmt.Sprintf("Exhibition admin: %s\n\n", Ownable.Owner().String())
 82
 83	if !Pausable.IsPaused() {
 84		out += ufmt.Sprintf("[Pause exhibition](%s)\n\n", txlink.Call("Pause"))
 85	} else {
 86		out += ufmt.Sprintf("[Unpause exhibition](%s)\n\n", txlink.Call("Unpause"))
 87	}
 88
 89	out += "---\n\n"
 90
 91	return out
 92}
 93
 94func RenderExhibWidget(itemsToRender int) string {
 95	if itemsToRender < 1 {
 96		return ""
 97	}
 98
 99	out := ""
100	i := 0
101	exhibition.items.Iterate("", "", func(key string, value interface{}) bool {
102		item := value.(*Item)
103
104		out += ufmt.Sprintf("- %s\n", fqname.RenderLink(item.pkgpath, ""))
105
106		i++
107		return i >= itemsToRender
108	})
109
110	return out
111}