package views import ( "math" "strconv" g "maragu.dev/gomponents" . "maragu.dev/gomponents/html" ) // transparentGIF is a 1x1 transparent GIF. It is the img fallback so the // browser never shows a broken image while the source loads. const transparentGIF = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" // Avatar renders a user avatar. A nil userID renders the placeholder. // A nil version omits the cache-busting query parameter. func Avatar(userID, version *int64, size int) g.Node { style := "width:" + strconv.Itoa(size) + "px;height:" + strconv.Itoa(size) + "px" if userID == nil { fontSize := int(math.Round(float64(size) * 0.45)) return Span(Class("avatar avatar-placeholder"), Style(style+";font-size:"+strconv.Itoa(fontSize)+"px"), g.Text("?")) } src := "/avatars/" + strconv.FormatInt(*userID, 10) + ".png" if version != nil { src += "?v=" + strconv.FormatInt(*version, 10) } return Picture(Class("avatar"), Style(style), Source(SrcSet(src), Type("image/png")), Img(Src(transparentGIF), Alt(""), Width(strconv.Itoa(size)), Height(strconv.Itoa(size)), Class("avatar-img")), ) }