All files / src/utils competitions.js

100% Statements 95/95
88.63% Branches 39/44
100% Functions 6/6
100% Lines 95/95

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 1041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x 158x 158x 157x 157x 157x 157x 157x   1x 1x 1x 1x 1x 1x 2484x 2484x 2484x 2484x 2484x 412x 412x 412x 412x 2235x 2235x   1x 1x 1x 1x 1x 1x 2478x 2478x 2478x 2478x 2478x 2478x   1x 1x 1x 1x 1x 1x 214x 214x 214x 214x 214x 214x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1538x 1538x 1533x 1533x 1538x 1538x 1538x  
/**
 * Competition and league-hierarchy helpers shared by the Table and Matches
 * tabs (SB-1039, SB-1040).
 *
 * MLS NEXT is organised Division (Homegrown, Academy) → Competition (League,
 * Flex) → Conference. In MT a competition's conferences can live under a
 * child league row — Flex is a league whose `parent_league_id` is Homegrown
 * and whose `match_type_id` is Flex — so anything that groups by "league"
 * has to resolve a child to its parent first. These are pure so both tabs
 * read the same rule and a spec can pin it directly.
 */
 
/** "League + Flex": the combined view labelled by what it combines. */
export const combinedLabel = names => (names || []).filter(Boolean).join(' + ');
 
/** Title for the combined chip: honest about being a record, not a standing. */
export const combinedTitle = names => {
  const label = combinedLabel(names);
  if (!label) return '';
  return (
    `Combined record across ${label} — the competitions that ` +
    'qualify for MLS NEXT Cup. A record, not a standing.'
  );
};
 
/**
 * The top-level division a league belongs to: the league itself when it has
 * no parent, its parent otherwise. Unknown ids come back as null. A league
 * row without the column (an API that predates it) is its own top level.
 */
export const topLevelLeague = (leagues, leagueId) => {
  if (leagueId === null || leagueId === undefined) return null;
  const byId = new Map((leagues || []).map(l => [Number(l.id), l]));
  let league = byId.get(Number(leagueId)) || null;
  // Bounded walk: a cycle in the data must not hang the page.
  for (let hops = 0; league?.parent_league_id && hops < 5; hops++) {
    const parent = byId.get(Number(league.parent_league_id));
    if (!parent) break;
    league = parent;
  }
  return league;
};
 
/**
 * The top-level division name for a match, from its conference's league.
 * Falls back to the league name the match carries when the league list does
 * not know the id — older fixtures, or a list that has not loaded yet.
 */
export const topLevelLeagueName = (leagues, match) => {
  const division = match?.division || null;
  const leagueId = division?.league_id ?? division?.leagues?.id ?? null;
  const resolved = topLevelLeague(leagues, leagueId);
  if (resolved) return resolved.name;
  return division?.leagues?.name ?? null;
};
 
/**
 * The competition whose tables a league's conferences are — "League" for
 * Homegrown and Academy, "Flex" for the Flex league — or null when the
 * league does not say (an API that predates `match_type_id`).
 */
export const competitionOfLeague = (leagues, matchTypes, leagueId) => {
  const league = (leagues || []).find(l => Number(l.id) === Number(leagueId));
  const typeId = league?.match_type_id;
  if (typeId === null || typeId === undefined) return null;
  const type = (matchTypes || []).find(t => Number(t.id) === Number(typeId));
  return type?.name ?? null;
};
 
/**
 * Tints for the per-row competition chip (SB-1105), keyed by competition name
 * lowercased. League and Flex are the two a reader has to tell apart on a
 * matchday, so they get the two ends of the brand palette — navy and amber —
 * rather than two shades of the same pill. Anything unmapped still gets a
 * chip; it just reads neutral, like the age group beside it.
 */
const COMPETITION_TINTS = {
  league: 'bg-brand-100 text-brand-700 dark:bg-brand-700 dark:text-brand-100',
  flex: 'bg-accent-100 text-accent-700 dark:bg-accent-700 dark:text-accent-100',
  tournament:
    'bg-purple-100 text-purple-700 dark:bg-purple-800 dark:text-purple-100',
  friendly: 'bg-surface-alt text-fg-muted',
};
 
const NEUTRAL_TINT = 'bg-surface-alt text-fg-muted';
 
/**
 * The competition chip for a match, or null when the match does not say which
 * competition it is.
 *
 * Null is the point. The desktop card renders `match_type_name || 'League'`,
 * which turns "we don't know" into a claim that it was a league fixture — the
 * same pun as rendering an unrecorded score as 0. A row with no competition
 * shows no chip.
 */
export const competitionChip = match => {
  const name = match?.match_type_name;
  if (!name) return null;
  return {
    label: name,
    class: COMPETITION_TINTS[String(name).toLowerCase()] ?? NEUTRAL_TINT,
  };
};