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

render.gno

5.47 Kb · 154 lines
  1package reactions
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/moul/kit/ui/v0"
  8	rx "gno.land/p/moul/reactions/v0"
  9)
 10
 11// maxListed caps the board index. The number of pages is unbounded (anyone can
 12// react to any key), and a Render that grows with it would eventually be too
 13// expensive to serve. The cap is on the INDEX only: every page keeps its own
 14// view at /r/moul/reactions/v0:<page>, whether or not it is listed here.
 15const maxListed = 25
 16
 17// Render serves two views:
 18//
 19//	""       the getting-started page: how to embed the block, plus the board
 20//	"<page>" one page's tally, where <page> is a package path
 21//
 22// The page view's path is the key itself, so the link under any embedded block
 23// is just this realm's path, a colon, and the page. That is why a page key may
 24// contain a colon of its own: "gno.land/r/you/blog:hello" reads back whole.
 25func Render(path string) string {
 26	if path == "" {
 27		return renderHome()
 28	}
 29	if !rx.ValidPage(path) {
 30		return "# Not a page\n\n" + ui.Inline(path) + " is not a page key: a key is a " +
 31			"package path, lowercase ASCII, at most " + strconv.Itoa(rx.MaxPageLen) +
 32			" bytes.\n\n[Back to the board](" + selfURL() + ")\n"
 33	}
 34	return renderPage(path)
 35}
 36
 37// selfURL is this realm's own gnoweb path, derived from realmPath the same way
 38// every link in the block is, so there is one rule and not two. A function and
 39// not a package-level var: a package-level var in a realm is realm STATE,
 40// persisted and re-read on every call, which is a strange thing for a string
 41// the compiler could have folded.
 42func selfURL() string { return rx.RealmURL(realmPath) }
 43
 44func renderHome() string {
 45	var b strings.Builder
 46
 47	b.WriteString("# Reactions\n\n")
 48	b.WriteString("A reaction block any realm can embed. One line in your `Render`, no state ")
 49	b.WriteString("on your side, and readers get one-click reactions keyed on the page they ")
 50	b.WriteString("are looking at.\n\n")
 51	b.WriteString("It is not a comment system, on purpose. There is no text field anywhere, ")
 52	b.WriteString("which is what lets it run with no moderation: a closed palette, one ")
 53	b.WriteString("reaction per address per page, changeable and removable.\n\n")
 54
 55	b.WriteString("## It looks like this\n\n")
 56	b.WriteString(blockFor(realmPath))
 57	b.WriteString("\nThat block is live and it is this realm's own page. Click one.\n\n")
 58
 59	b.WriteString("## Add it to your realm\n\n")
 60	b.WriteString("```go\n")
 61	b.WriteString("import \"" + realmPath + "\"\n\n")
 62	b.WriteString("func Render(path string) string {\n")
 63	b.WriteString("\treturn body + reactions.RenderBlock()\n")
 64	b.WriteString("}\n")
 65	b.WriteString("```\n\n")
 66	b.WriteString("That is the whole integration. `RenderBlock` takes no argument because it ")
 67	b.WriteString("does not need one: a plain read exported by one realm and called by ")
 68	b.WriteString("another opens no realm frame, so it sees YOUR path and uses it as the ")
 69	b.WriteString("page key. Nothing to register, nothing to configure.\n\n")
 70	b.WriteString("For one block per article rather than one per realm, name the page ")
 71	b.WriteString("yourself with the path you already serve:\n\n")
 72	b.WriteString("```go\n")
 73	b.WriteString("return body + reactions.RenderBlockFor(\"gno.land/r/you/blog:\" + path)\n")
 74	b.WriteString("```\n\n")
 75	b.WriteString("Reactions are recorded here, not in your realm, so redeploying yours ")
 76	b.WriteString("never loses them.\n\n")
 77
 78	b.WriteString("## The palette\n\n")
 79	t := ui.NewTable("key", "")
 80	for _, k := range rx.Keys() {
 81		t.Row("`"+k+"`", rx.Emoji(k))
 82	}
 83	b.WriteString(t.String())
 84	b.WriteString("\nSix, and the set is closed. `React` aborts on anything else, which is ")
 85	b.WriteString("the entire moderation policy.\n\n")
 86
 87	b.WriteString("## The board\n\n")
 88	b.WriteString(renderBoard())
 89
 90	return b.String()
 91}
 92
 93// renderBoard lists pages in key order, not by activity. Key order is the order
 94// the tree already walks, so the listing is stable between identical calls;
 95// ranking would need a second index maintained on every write, which is a lot
 96// of machinery for a table nobody navigates by.
 97func renderBoard() string {
 98	n := board.Pages()
 99	if n == 0 {
100		return ui.Empty("No page has been reacted to yet.")
101	}
102
103	t := ui.NewTable("page", "reactions", "last")
104	board.Each(func(page string, p *rx.Page) bool {
105		key, who, _ := p.Last()
106		last := "-"
107		if key != "" {
108			last = rx.Emoji(key) + " " + ui.Addr(who)
109		}
110		t.Row(
111			"["+ui.Cell(page)+"]("+rx.PageURL(realmPath, page)+")",
112			strconv.Itoa(p.Total()),
113			last,
114		)
115		return t.Len() >= maxListed
116	})
117
118	out := t.String()
119	if n > maxListed {
120		out += "\n" + strconv.Itoa(n) + " pages in total, " + strconv.Itoa(maxListed) +
121			" shown. Every page has its own view at `" + selfURL() + ":<page>`.\n"
122	}
123	return out
124}
125
126func renderPage(page string) string {
127	p := board.Page(page)
128
129	var b strings.Builder
130	b.WriteString("# " + ui.Inline(page) + "\n\n")
131	b.WriteString(blockFor(page))
132	b.WriteString("\n")
133
134	if p.Total() == 0 {
135		b.WriteString(ui.Empty("Nobody is reacting to this page right now."))
136	} else {
137		t := ui.NewTable("", "reaction", "addresses")
138		for _, k := range rx.Keys() {
139			if c := p.Count(k); c > 0 {
140				t.Row(rx.Emoji(k), "`"+k+"`", strconv.Itoa(c))
141			}
142		}
143		b.WriteString(t.String())
144	}
145
146	if key, who, height := p.Last(); key != "" {
147		b.WriteString("\nLast reaction: " + rx.Emoji(key) + " by " + ui.Addr(who) +
148			" at height " + strconv.FormatInt(height, 10) + ".\n")
149	}
150
151	b.WriteString("\n" + ui.ActionIn(realmPath, "Remove my reaction", "Unreact", "page", page))
152	b.WriteString(" · [The board](" + selfURL() + ")\n")
153	return b.String()
154}