All files / app-leaderboard/src/pages/components cards.ts

97.5% Statements 39/40
81.13% Branches 43/53
100% Functions 25/25
100% Lines 34/34

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234                            46x         399x                 18x         94x                 11x                                 29x 29x 29x 268x           268x 208x 29x                                         22x       22x                 118x                                             10x 10x 24x   24x 10x 10x               10x           12x     12x                                 11x                           11x 114x 66x 55x     11x                                 36x   36x   12x                   66x                              
import type { PageContent } from '../template.ts';
import { html, nothing } from '@be-music/service-web-theme/server';
/** The Leaderboard's cards: detail lists, figure tiles, distributions, links elsewhere and the lamp legend. */
import { ExternalLink as ExternalLinkIcon } from 'lucide-static';
import type { ExternalLink } from '../external.ts';
import { lampStyle } from '../format.ts';
import type { JudgeTotals } from '../stats.ts';
import type { Distribution } from '../stats.ts';
import { HelpTip } from './help-tip.ts';
import { type Messages } from '../i18n/index.ts';
import { Icon } from './icon.ts';
 
/** A titled block of details: label / value rows. */
export function Details(props: { title: string; rows: Array<[PageContent, PageContent]> }) {
  return html`<section class="card gap-4" data-size="sm">
    <header><h2>${props.title}</h2></header>
    <dl class="grid grid-cols-[max-content_1fr] gap-x-6 gap-y-2 px-4 text-sm">
      ${props.rows.map(
        ([label, value]) =>
          html`<dt class="text-muted-foreground">${label}</dt>
            <dd class="break-all">${value}</dd>`,
      )}
    </dl>
  </section>`;
}
 
/** A row of headline figures (label above, value below); six figures make two rows of three. */
export function Stats(props: { items: Array<[string, PageContent]> }) {
  return html`<dl
    class=${`grid grid-cols-2 gap-3 ${props.items.length % 3 === 0 ? 'sm:grid-cols-3' : 'sm:grid-cols-4'}`}
  >
    ${props.items.map(
      ([label, value]) =>
        html`<div class="card gap-1 px-4 py-3">
          <dt class="text-muted-foreground text-xs">${label}</dt>
          <dd class="text-xl font-semibold tabular-nums">${value}</dd>
        </div>`,
    )}
  </dl>`;
}
 
/** Shades for the DJ Rank bar, AAA darkest. */
const DJ_RANK_SHADES = ['#0f172a', '#334155', '#475569', '#64748b', '#94a3b8', '#cbd5e1', '#e2e8f0', '#f1f5f9'];
 
/**
 * A distribution as one stacked bar and a list of counts. Lamps take their lamp colours; DJ Ranks take grey shades.
 * Empty buckets stay out of the bar; the list keeps them (muted) when `showEmpty` is set, so every lamp or rank still
 * to get is there to see.
 */
export function DistributionCard<Key extends number | string>(
  props: {
    title: string;
    description: string;
    distribution: Distribution<Key>;
    kind: 'lamp' | 'dj-rank';
    showEmpty?: boolean;
  },
  messages: Messages,
) {
  const t = messages;
  const { total, buckets } = props.distribution;
  const shown = buckets
    .map((bucket, index) => ({
      ...bucket,
      label: props.kind === 'lamp' ? lampStyle(Number(bucket.key)).label : String(bucket.key),
      color: props.kind === 'lamp' ? lampStyle(Number(bucket.key)).color : (DJ_RANK_SHADES[index] ?? '#94a3b8'),
      meaning: props.kind === 'lamp' ? (t.lampMeanings[Number(bucket.key)] ?? t.lampMeanings.none) : undefined,
    }))
    .filter((bucket) => bucket.count > 0 || props.showEmpty === true);
  const filled = shown.filter((bucket) => bucket.count > 0);
  return html`<section class="card gap-4" data-size="sm">
    <header>
      <h2 class="inline-flex items-center gap-0.5">
        ${props.title}${HelpTip(
          {
            topic: props.kind === 'lamp' ? 'lamps' : 'scores',
            label: props.title,
            children: html`${props.kind === 'lamp' ? t.cards.lampsHelp : t.cards.djHelp}`,
          },
          messages,
        )}
      </h2>
      <p>${props.description}</p>
    </header>
    <div class="space-y-3 px-4">
      ${
        total === 0
          ? html`<p class="text-muted-foreground text-sm">${t.judgments.empty}</p>`
          : html`<div
                class="ring-foreground/10 flex h-3 overflow-hidden rounded-full ring-1"
                role="img"
                aria-label=${filled.map((bucket) => `${bucket.label}: ${bucket.count}`).join(', ') ?? nothing}
              >
                ${filled.map(
                  (bucket) =>
                    html`<span
                      title=${`${bucket.label}: ${bucket.count}`}
                      style=${`background:${bucket.color};width:${(bucket.count / total) * 100}%`}
                    ></span>`,
                )}
              </div>
              <ul class="grid grid-cols-[repeat(auto-fill,minmax(11rem,1fr))] gap-x-6 gap-y-1 text-sm">
                ${shown.map(
                  (bucket) =>
                    html`<li
                      class=${`flex items-center justify-between gap-2 ${bucket.count === 0 ? 'text-muted-foreground/70' : ''}`}
                      title=${bucket.meaning ?? nothing}
                    >
                      <span class="inline-flex items-center gap-2"
                        ><span
                          class="ring-foreground/20 inline-block size-2.5 rounded-sm ring-1"
                          style=${`background:${bucket.color}`}
                        ></span
                        >${bucket.label}</span
                      ><span class="text-muted-foreground whitespace-nowrap tabular-nums"
                        >${bucket.count === 0 ? '–' : `${bucket.count} (${Math.round((bucket.count / total) * 100)}%)`}</span
                      >
                    </li>`,
                )}
              </ul>`
      }
    </div>
  </section>`;
}
 
/** Links to the same chart on other Internet Rankings and BMS Search, one set per file the chart is known by. */
export function Elsewhere(props: { links: ExternalLink[] }, messages: Messages) {
  const t = messages;
  Iif (props.links.length === 0) return null;
  const name = (link: ExternalLink) => (link.kind === 'search' ? t.cards.search(link.name) : link.name);
  // One row per file: an MD5-keyed link joins the SHA-256 of its file when that is known.
  const fileOf = (link: ExternalLink) => link.file ?? link.hash ?? '';
  const files = [...new Set(props.links.map(fileOf))];
  return html`<section class="card gap-4" data-size="sm">
    <header>
      <h2>${t.cards.elsewhere}</h2>
      <p>${t.cards.elsewhereLead}</p>
    </header>
    <div class="space-y-3 px-4">
      ${files.map(
        (hash) =>
          html`<div class="flex flex-wrap items-center gap-2">
            ${
              files.length > 1 && hash !== ''
                ? html`<span class="text-muted-foreground font-mono text-xs">${t.cards.file(hash.slice(0, 8))}</span>`
                : null
            }${props.links
              .filter((link) => fileOf(link) === hash)
              .map(
                (link) =>
                  html`<a
                    class="btn"
                    data-size="sm"
                    data-variant="outline"
                    href=${link.url ?? nothing}
                    rel="noopener noreferrer"
                    target="_blank"
                    aria-label=${t.cards.opensAnotherSite(name(link)) ?? nothing}
                    >${name(link)}${Icon({ svg: ExternalLinkIcon, class: 'text-muted-foreground [&>svg]:size-3.5' })}</a
                  >`,
              )}
          </div>`,
      )}
    </div>
  </section>`;
}
 
const JUDGE_ROWS: Array<{ key: keyof JudgeTotals; label: string; name: string; color: string }> = [
  { key: 'pgreat', label: 'PG', name: 'PGREAT', color: '#38bdf8' },
  { key: 'great', label: 'GR', name: 'GREAT', color: '#facc15' },
  { key: 'good', label: 'GD', name: 'GOOD', color: '#4ade80' },
  { key: 'bad', label: 'BD', name: 'BAD', color: '#c084fc' },
  { key: 'poor', label: 'PR', name: 'POOR', color: '#f87171' },
  { key: 'emptyPoor', label: 'EPR', name: 'empty POOR', color: '#94a3b8' },
];
 
/**
 * How a player's notes were judged, summed over every play under the display ruleset: one stacked bar and the count
 * of each judgment. Empty POORs are presses, not notes, so they are listed but stay out of the bar's share.
 */
export function JudgeTotalsCard(props: { judges: JudgeTotals; ruleset: string }, messages: Messages) {
  const t = messages;
  const name = (row: (typeof JUDGE_ROWS)[number]) => (row.key === 'emptyPoor' ? t.judgments.emptyPoor : row.name);
  const notes = JUDGE_ROWS.filter((row) => row.key !== 'emptyPoor').reduce(
    (sum, row) => sum + props.judges[row.key],
    0,
  );
  return html`<section class="card gap-4" data-size="sm">
    <header>
      <h2 class="inline-flex items-center gap-0.5">
        ${t.judgments.title}${HelpTip(
          { topic: 'judgments', label: t.judgments.title, children: html`${t.judgments.help}` },
          messages,
        )}
      </h2>
      <p>${t.judgments.lead(props.ruleset)}</p>
    </header>
    <div class="space-y-3 px-4">
      ${
        notes === 0
          ? html`<p class="text-muted-foreground text-sm">${t.judgments.empty}</p>`
          : html`<div
              class="ring-foreground/10 flex h-3 overflow-hidden rounded-full ring-1"
              role="img"
              aria-label=${JUDGE_ROWS.map((row) => `${name(row)}: ${props.judges[row.key]}`).join(', ') ?? nothing}
            >
              ${JUDGE_ROWS.filter((row) => row.key !== 'emptyPoor' && props.judges[row.key] > 0).map(
                (row) =>
                  html`<span
                    title=${`${name(row)}: ${props.judges[row.key]}`}
                    style=${`background:${row.color};width:${(props.judges[row.key] / notes) * 100}%`}
                  ></span>`,
              )}
            </div>`
      }
      <dl class="grid grid-cols-3 gap-x-6 gap-y-1 text-sm sm:grid-cols-6">
        ${JUDGE_ROWS.map(
          (row) =>
            html`<div class="flex items-center justify-between gap-2 sm:block" title=${name(row) ?? nothing}>
              <dt class="text-muted-foreground inline-flex items-center gap-1.5 text-xs">
                <span
                  class="ring-foreground/20 inline-block size-2 rounded-full ring-1"
                  style=${`background:${row.color}`}
                ></span
                >${row.label}
              </dt>
              <dd class="font-semibold tabular-nums">${props.judges[row.key].toLocaleString('en-US')}</dd>
            </div>`,
        )}
      </dl>
    </div>
  </section>`;
}