Skip to content

Commit

Permalink
render
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Jan 16, 2025
1 parent 4bc7ae7 commit b9aa62b
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions examples/gno.land/r/gnoland/users/v1/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b9aa62b

Please sign in to comment.