All files / src/components/matches MatchListRow.vue

100% Statements 220/220
91.93% Branches 57/62
100% Functions 3/3
100% Lines 220/220

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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 2881x 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   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x   1x 1x 48x 48x 48x 48x 48x 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 1x   1x 776x 776x 776x 776x 1x   1x     1x 1x 776x 776x 776x 776x 776x 1x   1x   1x   1x 1x 743x 743x 743x 743x 743x 743x 743x 743x 743x 743x   743x 1x 1x       1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x   1x   1x 1x   1x 1x 777x 777x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 777x 777x   777x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 1554x 777x   777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 777x 50x 2x 32x 1x 32x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x   1x   1x 1x 777x 777x 777x 777x 1x 1x   1x 777x 777x 777x 733x 1x   1x 4x 1x   1x                                        
<template>
  <li
    class="match-row bg-card border border-line rounded-lg px-3 py-2.5 cursor-pointer hover:border-brand-300 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand-500"
    :class="{ 'border-l-[3px] border-l-accent-400': highlight }"
    data-testid="match-list-row"
    :data-match-id="match.id"
    tabindex="0"
    role="button"
    :aria-label="ariaLabel"
    v-on="pressHandlers"
    @click="onClick"
    @keydown.enter.prevent="$emit('view', match)"
    @keydown.space.prevent="$emit('view', match)"
  >
    <!-- One line per team, each with its own score. Stacking is what makes
         full club names legible at 400px, and it removes the "is that
         away-home or home-away?" question the single-line score raised: each
         number sits against the team that scored it. Away over home, matching
         the "Away @ Home" ordering used everywhere else in MT. -->
    <div
      v-for="side in sides"
      :key="side.key"
      class="flex items-center gap-2 min-w-0"
      :data-testid="`match-row-${side.key}`"
    >
      <ClubLogo :logo-url="side.logoUrl" :name="side.name" size="xs" />
      <span
        class="flex-1 min-w-0 truncate text-sm leading-5"
        :class="side.winner ? 'font-bold text-fg' : 'font-medium text-fg'"
        >{{ side.name }}</span
      >
 
      <svg
        v-for="n in side.redCards"
        :key="`${side.key}-rc-${n}`"
        width="9"
        height="13"
        viewBox="0 0 24 24"
        class="shrink-0"
        :data-testid="`match-row-${side.key}-red-card`"
      >
        <title>Red card</title>
        <rect x="4" width="16" height="24" rx="2" fill="#EA3323" />
      </svg>
 
      <!-- The winner's tick, as today. The loser's cross is gone: bold plus a
           higher number already says which way it went, and a glyph on every
           row twice over is exactly the chrome this list is shedding. -->
      <span
        v-if="side.marker"
        class="shrink-0 text-xs"
        :class="side.markerClass"
        :aria-hidden="true"
        >{{ side.marker }}</span
      >
 
      <span
        class="shrink-0 w-6 text-right text-sm tabular-nums"
        :class="side.winner ? 'font-bold text-fg' : 'font-semibold text-fg'"
        :data-testid="`match-row-${side.key}-score`"
        >{{ side.score }}</span
      >
    </div>
 
    <!-- Meta line. Absent entirely for a finished match with nothing left to
         say, which is most of the archive — that is where the vertical space
         comes back. -->
    <div
      v-if="showMeta"
      class="flex items-center gap-2 mt-0.5 min-w-0"
      data-testid="match-row-meta"
    >
      <span
        v-if="kickoffLabel"
        class="text-xs font-medium text-fg-muted tabular-nums"
        data-testid="match-row-kickoff"
        >{{ kickoffLabel }}</span
      >
      <MatchStatusLabel
        v-else-if="showStatus"
        :status="match.match_status"
        :scoring-mode="match.scoring_mode"
        :match-date="match.match_date"
      />
 
      <span
        v-if="match.age_group_name"
        class="px-1.5 py-0.5 rounded bg-surface-alt text-[10px] font-semibold text-fg-muted shrink-0"
        data-testid="row-age-group"
        >{{ match.age_group_name }}</span
      >
 
      <!-- Which competition, on every row (SB-1105). The section band above
           says the division, and Flex is a child league of Homegrown — so the
           band says HOMEGROWN for a Flex fixture and a League one alike. This
           is the only thing on screen that tells them apart. -->
      <span
        v-if="competition"
        class="px-1.5 py-0.5 rounded text-[10px] font-semibold uppercase tracking-wide shrink-0"
        :class="competition.class"
        data-testid="row-competition"
        >{{ competition.label }}</span
      >
 
      <span class="flex-1" />
 
      <!-- Long press is undiscoverable on its own, and unreachable by keyboard
           or switch control at all. This button is the same sheet by another
           door: visible, tabbable, and named. -->
      <button
        v-if="hasSheet"
        type="button"
        class="shrink-0 -my-1 px-2 py-1 rounded text-fg-muted hover:text-fg hover:bg-surface-alt text-base leading-none"
        :aria-label="`More options for ${ariaLabel}`"
        data-testid="match-row-more"
        @click.stop="$emit('more', match)"
        @pointerdown.stop
        @keydown.enter.stop
        @keydown.space.stop
      >
        ⋯
      </button>
    </div>
  </li>
</template>
 
<script setup>
import { computed } from 'vue';
import ClubLogo from '../shared/ClubLogo.vue';
import MatchStatusLabel from '../ui/MatchStatusLabel.vue';
import { useLongPress } from '../../composables/useLongPress';
import { isMissingResult } from '../../utils/tournamentStatus';
import { competitionChip } from '../../utils/competitions';
 
/**
 * One match, compactly (SB-1101).
 *
 * The card this replaces spent most of its height on Type (which repeated the
 * section header), Status (which repeated the score), Match ID and Source
 * (admin forensics), and two full-width buttons. Three matches fitted on a
 * phone. Those four fields moved into a sheet behind a long press, the buttons
 * became the row itself, and the date moved up into a sticky day header.
 */
const props = defineProps({
  match: { type: Object, required: true },
  /** Whether this viewer may edit THIS match — resolved by the parent. */
  canEdit: { type: Boolean, default: false },
  /** Admin-only forensics (Match ID, Source) exist in the sheet. */
  isAdmin: { type: Boolean, default: false },
  /** Draw a left edge on the viewer's own team's matches. */
  highlight: { type: Boolean, default: false },
});
 
const emit = defineEmits(['view', 'more']);
 
/** Statuses under which a scoreline is real. Mirrors ScorePill's SCORABLE. */
const SCORED = ['completed', 'live', 'in_progress', 'forfeit'];
 
const hasScore = computed(
  () =>
    SCORED.includes(props.match.match_status) &&
    props.match.home_score != null &&
    props.match.away_score != null
);
 
const redCardsFor = teamId =>
  (props.match.red_cards ?? []).filter(c => c.team_id === teamId).length;
 
/**
 * Away first, then home. A team with neither more nor fewer goals than the
 * other is not a winner, so a draw marks both sides with `=` rather than
 * bolding either.
 */
const sides = computed(() => {
  const m = props.match;
  const drawn = hasScore.value && m.home_score === m.away_score;
 
  const build = (key, name, club, teamId, score, opponentScore) => {
    const won = hasScore.value && score > opponentScore;
    return {
      key,
      name: name || 'TBD',
      logoUrl: club?.logo_url || '',
      redCards: redCardsFor(teamId),
      // Absent is not zero: an unscored match shows an em dash, never a 0
      // that would claim nobody scored.
      score: hasScore.value ? score : '—',
      winner: won,
      marker: won ? '✓' : drawn ? '=' : '',
      markerClass: won ? 'text-green-600 dark:text-green-400' : 'text-fg-muted',
    };
  };
 
  return [
    build(
      'away',
      m.away_team_name,
      m.away_team_club,
      m.away_team_id,
      m.away_score,
      m.home_score
    ),
    build(
      'home',
      m.home_team_name,
      m.home_team_club,
      m.home_team_id,
      m.home_score,
      m.away_score
    ),
  ];
});
 
const missingResult = computed(() => isMissingResult(props.match));
 
/**
 * A fixture still to come says when, not "Scheduled" — the word adds nothing
 * next to a date header that already says which day. A fixture whose day has
 * passed with no result says "Not reported" instead (MatchStatusLabel), which
 * is the state `mt coverage` exists to find and the one a kickoff time would
 * misrepresent as still upcoming.
 */
const kickoffLabel = computed(() => {
  if (missingResult.value) return '';
  if (!['scheduled', 'tbd'].includes(props.match.match_status)) return '';
  const iso = props.match.scheduled_kickoff;
  if (!iso) return '';
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return '';
  return d.toLocaleTimeString('en-US', {
    hour: 'numeric',
    minute: '2-digit',
    hour12: true,
  });
});
 
/**
 * A finished match needs no status: the scoreline and the tick already say so.
 * Everything else does — a forfeit's score looks ordinary, and postponed,
 * cancelled, live and unreported are not readable from the numbers at all.
 */
const showStatus = computed(() => props.match.match_status !== 'completed');
 
const hasSheet = computed(() => props.canEdit || props.isAdmin);
 
const competition = computed(() => competitionChip(props.match));
 
const showMeta = computed(
  () =>
    Boolean(kickoffLabel.value) ||
    showStatus.value ||
    Boolean(props.match.age_group_name) ||
    Boolean(competition.value) ||
    hasSheet.value
);
 
const ariaLabel = computed(() => {
  const m = props.match;
  const fixture = `${m.away_team_name || 'TBD'} at ${m.home_team_name || 'TBD'}`;
  if (hasScore.value) return `${fixture}, ${m.away_score} to ${m.home_score}`;
  return fixture;
});
 
const { handlers: pressHandlers, guardClick } = useLongPress(() => {
  if (hasSheet.value) emit('more', props.match);
});
 
const onClick = guardClick(() => emit('view', props.match));
</script>
 
<style scoped>
/*
 * A long press on a row must not also select its text or raise iOS's callout
 * menu — both fire before our timer does and leave the reader with a selection
 * they did not ask for on top of the sheet.
 */
.match-row {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  /*
   * The row still scrolls vertically; only the double-tap-to-zoom delay and
   * horizontal panning are given up, so a press registers as a press.
   */
  touch-action: pan-y;
}
</style>