avatar.go
⎇
Raw
1package views
2
3import (
4 "math"
5 "strconv"
6
7 g "maragu.dev/gomponents"
8 . "maragu.dev/gomponents/html"
9)
10
11// transparentGIF is a 1x1 transparent GIF. It is the img fallback so the
12// browser never shows a broken image while the source loads.
13const transparentGIF = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
14
15// Avatar renders a user avatar. A nil userID renders the placeholder.
16// A nil version omits the cache-busting query parameter.
17func Avatar(userID, version *int64, size int) g.Node {
18 style := "width:" + strconv.Itoa(size) + "px;height:" + strconv.Itoa(size) + "px"
19 if userID == nil {
20 fontSize := int(math.Round(float64(size) * 0.45))
21 return Span(Class("avatar avatar-placeholder"),
22 Style(style+";font-size:"+strconv.Itoa(fontSize)+"px"),
23 g.Text("?"))
24 }
25 src := "/avatars/" + strconv.FormatInt(*userID, 10) + ".png"
26 if version != nil {
27 src += "?v=" + strconv.FormatInt(*version, 10)
28 }
29 return Picture(Class("avatar"), Style(style),
30 Source(SrcSet(src), Type("image/png")),
31 Img(Src(transparentGIF), Alt(""), Width(strconv.Itoa(size)),
32 Height(strconv.Itoa(size)), Class("avatar-img")),
33 )
34}
35