|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Primer |
| 4 | + module OpenProject |
| 5 | + # OpenProject-specific Avatar component that extends Primer::Beta::Avatar |
| 6 | + # to support fallback rendering with initials when no image source is provided. |
| 7 | + # |
| 8 | + # When `src` is nil, this component renders an SVG with initials extracted from |
| 9 | + # the alt text. The AvatarFallbackElement web component then enhances it client-side |
| 10 | + # by applying a consistent background color based on the user's unique_id (using the |
| 11 | + # same hash function as OP Core for consistency). |
| 12 | + # |
| 13 | + # This component follows the "extension over mutation" pattern - it extends |
| 14 | + # Primer::Beta::Avatar without modifying its interface, ensuring compatibility |
| 15 | + # with upstream changes. |
| 16 | + class AvatarWithFallback < Primer::Beta::Avatar |
| 17 | + status :open_project |
| 18 | + |
| 19 | + # @see |
| 20 | + # - https://primer.style/foundations/typography/ |
| 21 | + # - https://github.com/primer/css/blob/main/src/support/variables/typography.scss |
| 22 | + FONT_STACK = "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'" |
| 23 | + |
| 24 | + # @param src [String] The source url of the avatar image. When nil, renders a fallback with initials. |
| 25 | + # @param alt [String] Alt text for the avatar. Used for accessibility and to generate initials when src is nil. |
| 26 | + # @param size [Integer] <%= one_of(Primer::Beta::Avatar::SIZE_OPTIONS) %> |
| 27 | + # @param shape [Symbol] Shape of the avatar. <%= one_of(Primer::Beta::Avatar::SHAPE_OPTIONS) %> |
| 28 | + # @param href [String] The URL to link to. If used, component will be wrapped by an `<a>` tag. |
| 29 | + # @param unique_id [String, Integer] Unique identifier for generating consistent avatar colors across renders. |
| 30 | + # @param system_arguments [Hash] <%= link_to_system_arguments_docs %> |
| 31 | + def initialize(src: nil, alt: nil, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, href: nil, unique_id: nil, **system_arguments) |
| 32 | + require_src_or_alt_arguments(src, alt) |
| 33 | + |
| 34 | + @unique_id = unique_id |
| 35 | + @use_fallback = src.blank? |
| 36 | + final_src = @use_fallback ? generate_fallback_svg(alt, size) : src |
| 37 | + |
| 38 | + super(src: final_src, alt: alt, size: size, shape: shape, href: href, **system_arguments) |
| 39 | + end |
| 40 | + |
| 41 | + def call |
| 42 | + render( |
| 43 | + Primer::ConditionalWrapper.new( |
| 44 | + condition: @use_fallback, |
| 45 | + tag: :"avatar-fallback", |
| 46 | + data: { |
| 47 | + unique_id: @unique_id, |
| 48 | + alt_text: @system_arguments[:alt] |
| 49 | + } |
| 50 | + ) |
| 51 | + ) { super } |
| 52 | + end |
| 53 | + |
| 54 | + private |
| 55 | + |
| 56 | + def require_src_or_alt_arguments(src, alt) |
| 57 | + return if src.present? || alt.present? |
| 58 | + |
| 59 | + raise ArgumentError, "`src` or `alt` is required" |
| 60 | + end |
| 61 | + |
| 62 | + def generate_fallback_svg(alt, size) |
| 63 | + svg_content = content_tag( |
| 64 | + :svg, |
| 65 | + safe_join([ |
| 66 | + # Use a neutral dark gray as default to minimize flicker in both light/dark modes |
| 67 | + # JS will replace with the hashed color (hsl(hue, 50%, 30%)) |
| 68 | + tag.rect(width: "100%", height: "100%", fill: "hsl(0, 0%, 35%)"), |
| 69 | + content_tag( |
| 70 | + :text, |
| 71 | + extract_initials(alt), |
| 72 | + x: "50%", |
| 73 | + y: "50%", |
| 74 | + "text-anchor": "middle", |
| 75 | + "dominant-baseline": "central", |
| 76 | + fill: "white", |
| 77 | + "font-size": fallback_font_size(size), |
| 78 | + "font-weight": "600", |
| 79 | + "font-family": FONT_STACK, |
| 80 | + style: "user-select: none; text-transform: uppercase;" |
| 81 | + ) |
| 82 | + ]), |
| 83 | + xmlns: "http://www.w3.org/2000/svg", |
| 84 | + width: size, |
| 85 | + height: size, |
| 86 | + viewBox: "0 0 #{size} #{size}", |
| 87 | + ) |
| 88 | + |
| 89 | + "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_content)}" |
| 90 | + end |
| 91 | + |
| 92 | + def extract_initials(name) |
| 93 | + name = name.to_s.strip |
| 94 | + return "" if name.empty? |
| 95 | + |
| 96 | + chars = name.chars |
| 97 | + first = chars[0]&.upcase || "" |
| 98 | + |
| 99 | + last_space = name.rindex(" ") |
| 100 | + if last_space && last_space < name.length - 1 |
| 101 | + last = name[last_space + 1]&.upcase || "" |
| 102 | + "#{first}#{last}" |
| 103 | + else |
| 104 | + first |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + def fallback_font_size(size) |
| 109 | + # Font size is 45% of avatar size for good readability, with a minimum of 8px |
| 110 | + [(size * 0.45).round, 8].max |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments