Search Apps Documentation Source Content File Folder Download Copy Actions Download

render_board.gno

1.96 Kb ยท 76 lines
 1package boards2
 2
 3import (
 4	"gno.land/p/gnoland/boards"
 5	"gno.land/p/jeronimoalbi/pager"
 6	"gno.land/p/moul/md"
 7	"gno.land/p/nt/mux"
 8)
 9
10func renderBoard(res *mux.ResponseWriter, req *mux.Request) {
11	name := req.GetVar("board")
12	board, found := gBoards.GetByName(name)
13	if !found {
14		link := md.Link("create a new board", gRealmLink.Call("CreateBoard", "name", name, "listed", "true"))
15		res.Write(md.H3("The board you are looking for does not exist"))
16		res.Write("Do you want to " + link + " ?")
17		return
18	}
19
20	creatorLink := userLink(board.Creator)
21	date := board.CreatedAt.Format(dateFormat)
22
23	res.Write(md.H1(board.Name))
24	res.Write("Board created by " + creatorLink + " on " + date + ", #" + board.ID.String())
25	if board.Readonly {
26		res.Write("  \n_" + md.Bold("Starting new threads and commenting is disabled") + "_")
27	}
28
29	res.Write("\n" + renderBoardMenu(board, req))
30	res.Write(md.HorizontalRule())
31
32	if board.Threads.Size() == 0 {
33		res.Write(md.H3("This board doesn't have any threads"))
34		if !board.Readonly {
35			startConversationLink := md.Link("start a new conversation", makeCreateThreadURI(board))
36			res.Write("Do you want to " + startConversationLink + " in this board ?")
37		}
38		return
39	}
40
41	p, err := pager.New(req.RawPath, board.Threads.Size(), pager.WithPageSize(pageSizeDefault))
42	if err != nil {
43		panic(err)
44	}
45
46	render := func(thread *boards.Post) bool {
47		if !thread.Hidden {
48			res.Write(renderThreadSummary(thread) + "\n")
49		}
50		return false
51	}
52
53	res.Write("Sort by: ")
54
55	r := parseRealmPath(req.RawPath)
56	sortOrder := r.Query.Get("order")
57	if sortOrder == "desc" {
58		r.Query.Set("order", "asc")
59		res.Write(md.Link("newest first", r.String()) + "\n\n")
60	} else {
61		r.Query.Set("order", "desc")
62		res.Write(md.Link("oldest first", r.String()) + "\n\n")
63	}
64
65	count := p.PageSize()
66	if sortOrder == "desc" {
67		count = -count // Reverse iterate
68	}
69
70	board.Threads.Iterate(p.Offset(), count, render)
71
72	if p.HasPages() {
73		res.Write(md.HorizontalRule())
74		res.Write(pager.Picker(p))
75	}
76}