All files / src/components TournamentIndex.vue

100% Statements 158/158
100% Branches 23/23
100% Functions 1/1
100% Lines 158/158

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 1941x 1x 1x 1x 1x 2x 2x 2x 2x   2x 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 1x 1x 1x 1x 1x 1x 1x       1x   1x 26x 26x 26x 26x 1x 1x 1x 1x 1x   1x 67x 1x 1x 50x 50x 50x 1x 1x 51x 51x 51x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 30x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 30x 1x    
<template>
  <div data-testid="tournament-index">
    <!-- A link to a tournament this viewer cannot see, or one that is gone.
         Says no more than that: never whether it exists. -->
    <p
      v-if="notice"
      class="mb-4 rounded-md border border-line bg-card px-4 py-3 text-sm text-fg-muted"
      role="status"
      data-testid="tournament-index-notice"
    >
      {{ notice }}
    </p>
 
    <ul class="grid grid-cols-1 sm:grid-cols-2 gap-3" role="list">
      <li v-for="card in cards" :key="card.tournament.id">
        <button
          type="button"
          :class="[
            'w-full h-full text-left bg-card rounded-xl border p-4 flex items-start gap-3 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 focus-visible:ring-offset-2 dark:focus-visible:ring-offset-0',
            card.state === 'live'
              ? 'border-red-400 ring-1 ring-red-400 dark:border-red-500 dark:ring-red-500 hover:border-red-500'
              : 'border-line hover:border-brand-400',
          ]"
          data-testid="tournament-card"
          :data-state="card.state"
          :data-id="card.tournament.id"
          @click="$emit('open', card.tournament.id)"
        >
          <img
            v-if="card.tournament.logo_url"
            :src="card.tournament.logo_url"
            alt=""
            class="w-12 h-12 object-contain shrink-0"
            data-testid="tournament-card-logo"
          />
          <!-- No logo: a neutral mark, the same size, so the names line up. -->
          <span
            v-else
            class="w-12 h-12 rounded-lg border border-line bg-surface-alt text-fg-muted shrink-0 flex items-center justify-center"
            aria-hidden="true"
            data-testid="tournament-logo-placeholder"
          >
            <svg
              viewBox="0 0 24 24"
              class="w-6 h-6"
              fill="none"
              stroke="currentColor"
              stroke-width="1.5"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <path d="M8 21h8M12 17v4M7 4h10v5a5 5 0 0 1-10 0V4Z" />
              <path d="M7 6H4v2a3 3 0 0 0 3 3M17 6h3v2a3 3 0 0 1-3 3" />
            </svg>
          </span>
 
          <span class="min-w-0 flex-1">
            <span class="flex items-start justify-between gap-2">
              <span
                class="min-w-0 break-words font-semibold text-fg leading-tight"
                data-testid="tournament-card-name"
                >{{ card.tournament.name }}</span
              >
              <!-- Status in words as well as colour. No dates, no chip:
                   unknown is not a state. -->
              <span
                v-if="card.chip"
                :class="[
                  'inline-flex items-center gap-1.5 shrink-0 px-2 py-0.5 rounded-full text-[11px] font-semibold uppercase tracking-wide',
                  card.chipClass,
                ]"
                data-testid="tournament-card-status"
              >
                <span
                  v-if="card.state === 'live'"
                  class="w-1.5 h-1.5 rounded-full bg-red-500"
                  aria-hidden="true"
                ></span>
                {{ card.chip }}
              </span>
            </span>
            <span
              class="mt-1 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-sm text-fg-muted"
            >
              <span v-if="card.dates" data-testid="tournament-card-dates">{{
                card.dates
              }}</span>
              <span
                v-if="card.dates && card.tournament.location"
                aria-hidden="true"
                >·</span
              >
              <span
                v-if="card.tournament.location"
                data-testid="tournament-card-location"
                >{{ card.tournament.location }}</span
              >
            </span>
            <!-- Only a count the server gave. A count it could not load is
                 absent, never "0 matches". -->
            <span
              v-if="card.matchCount != null"
              class="mt-1 block text-xs text-fg-muted tabular-nums"
              data-testid="tournament-card-count"
              >{{ card.matchCount }}
              {{ card.matchCount === 1 ? 'match' : 'matches' }}</span
            >
            <!-- The admin's one line (SB-1797). None set: no element at all. -->
            <span
              v-if="card.announcement"
              class="mt-2 flex items-start gap-1.5 rounded-md bg-accent-50 px-2 py-1 text-sm font-semibold text-accent-700 break-words dark:bg-accent-900 dark:text-accent-100"
              data-testid="tournament-card-announcement"
            >
              <svg
                viewBox="0 0 24 24"
                class="mt-0.5 h-4 w-4 shrink-0"
                fill="none"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
              >
                <path d="M3 11v2a1 1 0 0 0 1 1h2l5 4V6L6 10H4a1 1 0 0 0-1 1Z" />
                <path d="M15.5 8.5a5 5 0 0 1 0 7M18.5 5.5a9 9 0 0 1 0 13" />
              </svg>
              <span class="min-w-0">{{ card.announcement }}</span>
            </span>
          </span>
        </button>
      </li>
    </ul>
  </div>
</template>
 
<script setup>
import { computed } from 'vue';
import {
  formatTournamentDateRange,
  tournamentStatus,
} from '../utils/tournamentStatus';
import { announcementText } from '../composables/useTournamentAnnouncements';
 
const props = defineProps({
  // Already in status order (`compareByStatus`); rendered as given.
  tournaments: { type: Array, required: true },
  notice: { type: String, default: null },
  now: { type: Date, default: null },
});
 
defineEmits(['open']);
 
const CHIPS = {
  // The list carries no matches, so "live" here is the dates alone — the
  // tournament is on today, not necessarily mid-match. Same words as the hero
  // card's date-only state, and a static dot: a pulse would claim a match in
  // progress on a rest day.
  live: {
    label: 'Happening today',
    cls: 'bg-red-100 text-red-700 dark:bg-red-900 dark:text-red-100',
  },
  soon: {
    label: 'Upcoming',
    cls: 'bg-brand-100 text-brand-700 dark:bg-brand-800 dark:text-brand-100',
  },
  upcoming: {
    label: 'Upcoming',
    cls: 'bg-brand-100 text-brand-700 dark:bg-brand-800 dark:text-brand-100',
  },
  completed: {
    label: 'Completed',
    cls: 'bg-surface-alt text-fg-muted',
  },
};
 
// The list rows carry no matches, so status here is from the dates alone —
// the same rule the hero card applies to a paged tournament.
const cards = computed(() =>
  props.tournaments.map(t => {
    const status = tournamentStatus(t, [], props.now ?? new Date());
    const chip = status ? CHIPS[status.state] : null;
    return {
      tournament: t,
      state: status?.state ?? 'unknown',
      chip: chip?.label ?? null,
      chipClass: chip?.cls ?? '',
      dates: formatTournamentDateRange(t),
      matchCount: typeof t.match_count === 'number' ? t.match_count : null,
      announcement: announcementText(t),
    };
  })
);
</script>