From b9aa62b1a5d3e68bd134eb4ac52b9c3fdc6816f2 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 16 Jan 2025 18:41:46 +0100 Subject: [PATCH] render --- .../gno.land/r/gnoland/users/v1/render.gno | 87 ++++++++++++------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/examples/gno.land/r/gnoland/users/v1/render.gno b/examples/gno.land/r/gnoland/users/v1/render.gno index 34aec8ed58b..953c2fcfeaf 100644 --- a/examples/gno.land/r/gnoland/users/v1/render.gno +++ b/examples/gno.land/r/gnoland/users/v1/render.gno @@ -17,52 +17,75 @@ func Render(path string) string { var out string req := realmpath.Parse(path) - // Render home page if req.Path == "" { - p := pager.NewPager(users.GetReadonlyAddrStore(), 50, false) - page := p.MustGetPageByPath(path) + out += renderHomePage(req.Path) + } - out += "# gno.land user registry\n" + // Render single user page + data, isLatest, isName := resolveUser(req.Path) + if data == nil { + return render404() + } - for _, item := range page.Items { - data := item.Value.(*users.UserData) - if !data.IsDeleted() { - out += ufmt.Sprintf("- User [**%s**](/r/gnoland/users/v1:%s)\n", data.Name(), data.Name()) - } - } + // Otherwise, render the user page + return renderUserPage(data, req.Path, isName, isLatest) +} - out += "\n" - out += page.Picker() - out += "\n\n" - out += "Page " + strconv.Itoa(page.PageNumber) + " of " + strconv.Itoa(page.TotalPages) + "\n\n" +func renderHomePage(path string) string { + var out string + p := pager.NewPager(users.GetReadonlyAddrStore(), 50, false) + page := p.MustGetPageByPath(path) - return out - } + out += "# gno.land user registry\n" - // Render specific user page - var data *users.UserData - if std.Address(req.Path).IsValid() { - data = users.ResolveAddress(std.Address(req.Path)) - } else { - data, _ = users.ResolveName(req.Path) + for _, item := range page.Items { + data := item.Value.(*users.UserData) + if !data.IsDeleted() { + out += ufmt.Sprintf("- User [**%s**](/r/gnoland/users/v1:%s)\n", data.Name(), data.Name()) + } } - if data == nil { - out += md.H1("404") - out += "This user does not exist or has been deleted.\n" - } else { - out += renderUserPage(data) - } + out += "\n" + out += page.Picker() + out += "\n\n" + out += "Page " + strconv.Itoa(page.PageNumber) + " of " + strconv.Itoa(page.TotalPages) + "\n\n" return out } -func renderUserPage(lookup *users.UserData) string { - out := md.H1("User " + lookup.Name()) - out += ufmt.Sprintf("Address: %s\n\n", lookup.Addr().String()) +// resolveUser resolves the user based on the path, determining if it's a name or address +func resolveUser(path string) (*users.UserData, bool, bool) { + if std.Address(path).IsValid() { + return users.ResolveAddress(std.Address(path)), false, false + } + + data, isLatest := users.ResolveName(path) + return data, isLatest, true +} + +// renderUserPage generates the user page based on user data and path +func renderUserPage(data *users.UserData, query string, isName, isLatest bool) string { + var out string + + out += md.H1("User " + data.Name()) + out += ufmt.Sprintf("Address: %s\n\n", data.Addr().String()) + + if isName && !isLatest { + out += md.Paragraph(ufmt.Sprintf( + "Note: `%s` is a previous name of `%s`.", + query, data.Name())) + } + out += md.H2("Bio") - out += profile.GetStringField(lookup.Addr(), "Bio", "No bio defined.") + out += profile.GetStringField(data.Addr(), "Bio", "No bio defined.") out += "\n\n" return out } + +// render404 generates a 404 error page +func render404() (out string) { + out += md.H1("404") + out += "This user does not exist or has been deleted.\n" + return out +}