1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require "pro_tacts/admin/phlex"
require "pro_tacts/admin/format"
module ProTacts
module Admin
# A contact's avatar: the card's picture when it carries one a
# browser can show, the initials otherwise. One component because
# the views that render this slot — the dashboard's rows, the
# birthdays' rows, the detail header — should not disagree about
# what an avatar is. (The detail header asks for it only when a
# picture exists; see ContactCard.)
#
# The picture renders from the photo route rather than the decoded
# bytes in hand, so the page carries a URL where it would otherwise
# carry megabytes of base64 (see the route in Web). Alt is empty:
# the contact's name is adjacent text in every slot, so the image
# is decoration, not information. Loaded lazily, because /contacts
# draws an avatar for every card in the book and a browser would
# otherwise fetch every picture before the page counts as loaded,
# most of them far below anything on screen.
class Avatar < Phlex::HTML
# @rbs @contact: Contact | ListedContact
# @rbs @size: String?
#: (contact: Contact | ListedContact, ?size: String?) -> void
def initialize(contact:, size: nil)
@contact = contact
@size = size
end
def view_template
if @contact.photo?
img(class: "avatar", src: "/contacts/#{@contact.id}/photo", alt: "", loading: "lazy", **attributes)
else
span(class: "avatar", **attributes) { Format.initials(@contact) }
end
end
private
#: () -> Hash[untyped, untyped]
def attributes
@size ? {data_size: @size} : {}
end
end
end
end