render.gno

2.26 Kb ยท 77 lines
 1package eventreg
 2
 3import (
 4	"math"
 5	"std"
 6	"strings"
 7	"time"
 8
 9	"gno.land/p/demo/ufmt"
10	"gno.land/p/moul/md"
11	"gno.land/p/zenao/events"
12	zenaov1 "gno.land/p/zenao/zenao/v1"
13	"gno.land/r/demo/profile"
14)
15
16func Render(path string) string {
17	title := "Zenao Events"
18	listFn := listEvents
19	switch {
20	case strings.HasPrefix(path, "created/"):
21		title = "My Zenao Events"
22		listFn = func(from int64, to int64, limit uint32) []*zenaov1.EventInfo {
23			return listEventsByCreator(strings.TrimPrefix(path, "created/"), from, to, limit)
24		}
25	case strings.HasPrefix(path, "tickets/"):
26		title = "My Zenao Tickets"
27		listFn = func(from int64, to int64, limit uint32) []*zenaov1.EventInfo {
28			return listEventsByParticipant(strings.TrimPrefix(path, "tickets/"), from, to, limit)
29		}
30	}
31
32	buf := strings.Builder{}
33	buf.WriteString(md.H1(title))
34	buf.WriteString(md.HorizontalRule())
35	renderSection(&buf, listFn, "๐ŸŽ‰ Upcoming", time.Now().Unix()+1, math.MaxInt64, 10)
36	renderSection(&buf, listFn, "๐Ÿ‘ด Past", time.Now().Unix(), 0, 10)
37	return buf.String()
38}
39
40func renderSection(buf *strings.Builder, listFn func(int64, int64, uint32) []*zenaov1.EventInfo, title string, from int64, to int64, limit uint32) {
41	buf.WriteString(md.H2(title))
42	evts := listFn(from, to, limit)
43	for _, evt := range evts {
44		buf.WriteString(md.HorizontalRule())
45		renderEvent(buf, evt)
46	}
47	if len(evts) == 0 {
48		buf.WriteString(md.HorizontalRule())
49		buf.WriteString(md.Paragraph("๐Ÿคท Nothing to show"))
50	}
51	buf.WriteString(md.HorizontalRule())
52}
53
54func renderEvent(buf *strings.Builder, evt *zenaov1.EventInfo) {
55	locStr, err := events.LocationString(evt.Location)
56	if err != nil {
57		locStr = "Error: " + err.Error()
58	}
59
60	orga := profile.GetStringField(std.Address(evt.Creator), "DisplayName", "")
61	if orga == "" {
62		orga = evt.Creator
63	}
64
65	buf.WriteString(md.H3(evt.Title + " | ๐Ÿ“ " + locStr))
66	timeStr := ufmt.Sprintf("โฑ๏ธ %s to %s", time.Unix(evt.StartDate, 0).Format(time.DateTime), time.Unix(evt.EndDate, 0).Format(time.DateTime))
67	buf.WriteString(md.Paragraph(timeStr + " | " + md.Link("๐Ÿ”— Details", trimDomain(evt.PkgPath))))
68	buf.WriteString(md.Paragraph("Organized by " + orga))
69}
70
71func trimDomain(pkgPath string) string {
72	slashIdx := strings.Index(pkgPath, "/")
73	if slashIdx == -1 {
74		return pkgPath
75	}
76	return pkgPath[slashIdx:]
77}