All files / service-web-theme/src account-identity-template.ts

100% Statements 6/6
93.75% Branches 15/16
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 49 50 51 52 53 54 55          42x                       145x                                       80x 80x   80x 80x                          
import { html, nothing, type TemplateResult } from 'lit';
import { resolveAccountIdentity, type ServiceAccountIdentity } from './account-model.ts';
import type { Locale } from './locale.ts';
 
/** The shared avatar style: square, cropped to cover, round, with a hairline ring that follows the circle. */
const AVATAR_CLASS = 'bg-muted ring-foreground/10 inline-block aspect-square shrink-0 rounded-full object-cover ring-1';
 
/**
 * A user's avatar, always a circle: the one avatar style every service and the player share. A non-square image is
 * cropped to the square (`object-cover`), never stretched. Upload and image fallback URLs belong to the application.
 */
export function avatarTemplate(input: {
  src?: string;
  size: number;
  className?: string;
  alt?: string;
}): TemplateResult {
  return input.src === undefined
    ? html``
    : html`<img
        class=${`${AVATAR_CLASS}${input.className ? ` ${input.className}` : ''}`}
        src=${input.src}
        width=${input.size}
        height=${input.size}
        alt=${input.alt ?? ''}
        loading="lazy"
        decoding="async"
      />`;
}
 
/** Compact header identity or an expanded account summary, sharing naming and anonymous-state presentation. */
export function accountIdentityTemplate(input: {
  account: ServiceAccountIdentity;
  locale: Locale;
  variant?: 'compact' | 'detail';
  showAvatar?: boolean;
}): TemplateResult {
  const view = resolveAccountIdentity(input.account, input.locale);
  const compact = input.variant === 'compact';
  const avatar =
    input.showAvatar === false ? nothing : avatarTemplate({ src: input.account.avatarURL, size: compact ? 24 : 40 });
  return compact
    ? html`${avatar}<span class="hidden max-w-[12ch] truncate sm:inline">${view.label}</span>`
    : html`<div class="flex min-w-0 items-center gap-3" data-account-identity>
        ${avatar}
        <div class="min-w-0">
          <div class="flex flex-wrap items-center gap-2">
            <span class="truncate font-medium">${input.account.displayName}</span>
            ${input.account.anonymous ? html`<span class="badge" data-variant="outline">${view.guest}</span>` : nothing}
          </div>
          ${input.account.handle === '' ? nothing : html`<div class="text-muted-foreground truncate text-xs">${view.label}</div>`}
        </div>
      </div>`;
}