All files / src/components TournamentFilters.vue

100% Statements 404/404
88.88% Branches 112/126
100% Functions 16/16
100% Lines 404/404

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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 5221x 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 40x 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   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 121x 121x       121x 1x 1x 1x 1x 36x 36x 36x   36x 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 64x 64x 64x 64x   64x 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 138x 1x 1x 1x 1x 1x 1x 1x 1x 172x 172x 172x 172x 172x 172x 1x 1x 41x 41x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 32x 32x 32x 55x 55x 55x 32x 1x 1x 1x 1x 1x 74x 74x 74x 74x 74x 1x 1x 1x 41x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 200x 199x 74x 1x 1x 1x 1x   1x 1x 1x 73x 73x 182x 182x 182x 182x 73x 122x 122x 73x 73x 73x 73x 134x 134x 73x 1x   1x 249x 249x 1x   1x 1x 1x   1x   1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 1x 1x 43x 43x 86x 86x 86x 43x 1x   1x 1x 148x 148x 148x 24x 1x   1x 1x 147x 1x   1x 156x 156x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 156x 15x 15x 15x 15x 15x 15x 15x 156x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 156x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 5x 5x 5x   1x 8x 8x 8x 8x   1x 1x 1x 13x 13x 13x 13x 13x 13x   1x                      
<template>
  <!-- Filters for a large tournament (SB-1749, SB-1865, SB-1870). On a
       phone they fold into one "Filters" button plus the active choices as
       chips, and open as a bottom sheet; from `sm` up they sit inline. The
       controls themselves are the same elements either way. The league
       toggle and the team search live beside this, not in it. -->
  <div class="min-w-0">
    <!-- Phone toolbar -->
    <div class="flex sm:hidden items-center gap-2 min-w-0">
      <button
        type="button"
        class="shrink-0 inline-flex items-center gap-1.5 h-11 px-3.5 rounded-full border border-line bg-card text-sm font-semibold text-fg hover:border-brand-400 focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500"
        :aria-expanded="sheetOpen ? 'true' : 'false'"
        aria-controls="tournament-filter-panel"
        data-testid="tournament-filters-open"
        @click="openSheet"
      >
        <svg viewBox="0 0 20 20" class="w-4 h-4" aria-hidden="true">
          <path
            d="M3 5h14M6 10h8M9 15h2"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
          />
        </svg>
        Filters
      </button>
      <div class="flex-1 min-w-0 flex gap-1.5 overflow-x-auto no-scrollbar">
        <button
          v-if="ageName"
          type="button"
          class="shrink-0 h-11 px-3 rounded-full bg-brand-50 dark:bg-brand-900 text-brand-700 dark:text-brand-100 text-sm font-semibold focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500"
          :aria-label="`Age group ${ageName}. Change filters`"
          data-testid="active-filter-age"
          @click="openSheet"
        >
          {{ ageName }}
        </button>
        <button
          v-for="chip in clearableChips"
          :key="chip.key"
          type="button"
          class="shrink-0 inline-flex items-center gap-1 h-11 pl-3 pr-2.5 rounded-full bg-brand-600 text-white text-sm font-medium max-w-[11rem] focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 focus-visible:ring-offset-1"
          :aria-label="`Clear ${chip.kind} filter: ${chip.text}`"
          data-testid="active-filter-chip"
          :data-kind="chip.kind"
          @click="chip.clear"
        >
          <span class="truncate">{{ chip.text }}</span>
          <span aria-hidden="true" class="text-base leading-none opacity-80"
            >×</span
          >
        </button>
      </div>
    </div>
 
    <!-- Sheet backdrop (phone only) -->
    <div
      v-if="sheetOpen"
      class="fixed inset-0 z-40 bg-black/40 sm:hidden"
      aria-hidden="true"
      data-testid="tournament-filters-backdrop"
      @click="closeSheet"
    ></div>
 
    <div
      id="tournament-filter-panel"
      :role="sheetOpen ? 'dialog' : undefined"
      :aria-modal="sheetOpen ? 'true' : undefined"
      :aria-label="sheetOpen ? 'Filters' : undefined"
      :class="[
        sheetOpen
          ? 'fixed inset-x-0 bottom-0 z-50 max-h-[85vh] overflow-y-auto rounded-t-2xl bg-card border-t border-line shadow-2xl px-4 pt-3 pb-[max(1.25rem,env(safe-area-inset-bottom))]'
          : 'hidden',
        'sm:block sm:static sm:z-auto sm:max-h-none sm:overflow-visible sm:rounded-none sm:bg-transparent sm:border-0 sm:shadow-none sm:p-0',
      ]"
      @keydown.esc="closeSheet"
    >
      <div v-if="sheetOpen" class="sm:hidden">
        <div
          class="mx-auto mb-3 h-1.5 w-10 rounded-full bg-line"
          aria-hidden="true"
        ></div>
        <div class="flex items-center justify-between mb-3">
          <h2 class="text-base font-bold text-fg">Filters</h2>
          <button
            type="button"
            class="h-11 px-4 rounded-full bg-brand-600 text-white text-sm font-semibold focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 focus-visible:ring-offset-2"
            data-testid="tournament-filters-done"
            @click="closeSheet"
          >
            Done
          </button>
        </div>
      </div>
 
      <div
        class="flex flex-col sm:flex-row sm:flex-wrap sm:items-end gap-3"
        data-testid="tournament-filters"
      >
        <div v-if="ageGroups.length > 0" class="sm:w-36">
          <label for="tournament-age-group" :class="labelClass"
            >Age Group</label
          >
          <select
            id="tournament-age-group"
            ref="firstControl"
            :value="selectedAgeGroupId"
            :disabled="disabled"
            data-testid="tournament-age-group-select"
            :class="selectClass"
            @change="onAgeGroupChange"
          >
            <option v-for="ag in ageGroups" :key="ag.id" :value="ag.id">
              {{ optionLabel(ag.name, ageCount(ag)) }}
            </option>
          </select>
        </div>
 
        <!-- Hidden when this age has no groups ([]) and when the groups are not
             known (null). Neither case renders an empty control or a zero.
             Narrowed to the chosen conference / league side and stage. -->
        <div v-if="hasGroups" class="sm:w-44">
          <label for="tournament-group" :class="labelClass">Group</label>
          <select
            id="tournament-group"
            :value="selectedGroupId"
            :disabled="disabled"
            data-testid="tournament-group-select"
            :class="selectClass"
            @change="onGroupChange"
          >
            <option value="">All groups</option>
            <option
              v-for="g in visibleGroups"
              :key="groupOptionId(g)"
              :value="optionValue(g)"
            >
              {{ optionLabel(groupText(g), g.matches) }}
            </option>
          </select>
        </div>
      </div>
 
      <!-- Stage (SB-1870): the stages the server lists, with counts. Matches
           with no stage yet are only under All stages — said in a note,
           never offered as a stage of their own. -->
      <div
        v-if="hasStages"
        class="mt-3"
        role="group"
        aria-labelledby="tournament-stage-label"
        data-testid="tournament-stage-filter"
      >
        <span id="tournament-stage-label" :class="labelClass">Stage</span>
        <div class="flex flex-wrap gap-1.5">
          <button
            v-for="opt in stageOptions"
            :key="opt.key ?? 'all'"
            type="button"
            :disabled="disabled"
            :aria-pressed="opt.key === selectedStage ? 'true' : 'false'"
            data-testid="stage-chip"
            :data-stage="opt.key ?? ''"
            :class="[
              'inline-flex items-center gap-1.5 h-11 sm:h-9 px-3.5 rounded-full border text-sm font-medium transition-colors',
              'focus:outline-none focus-visible:ring-2 focus-visible:ring-brand-500 disabled:opacity-60',
              opt.key === selectedStage
                ? 'bg-brand-600 border-brand-600 text-white'
                : 'bg-card border-line text-fg hover:border-brand-400',
            ]"
            @click="emit('update:stage', opt.key)"
          >
            <span>{{ opt.label }}</span>
            <span
              v-if="opt.count != null"
              :class="[
                'text-xs tabular-nums',
                opt.key === selectedStage ? 'text-brand-100' : 'text-fg-muted',
              ]"
              >{{ opt.count }}</span
            >
          </button>
        </div>
        <p
          v-if="unstagedCount > 0"
          class="mt-1.5 text-xs text-fg-muted"
          data-testid="tournament-unstaged-note"
        >
          {{ unstagedNote }}
        </p>
      </div>
 
      <!-- Conference last (SB-1880): Age Group, Group and Stage pick the
           matches; Conference is a "teams in my conference" search on top.
           Hidden when the conferences are not known (null) or there are
           none ([]): an empty control promises a choice that is not there. -->
      <div
        v-if="hasConferences"
        class="mt-3 sm:w-60"
        data-testid="tournament-conference-filter"
      >
        <label for="tournament-conference" :class="labelClass"
          >Conference</label
        >
        <select
          id="tournament-conference"
          :value="selectedConferenceId ?? ''"
          :disabled="disabled"
          aria-describedby="tournament-conference-hint"
          data-testid="tournament-conference-select"
          :class="selectClass"
          @change="onConferenceChange"
        >
          <option value="">All conferences</option>
          <optgroup
            v-for="league in conferenceLeagues"
            :key="league.name"
            :label="league.name"
          >
            <option v-for="c in league.items" :key="c.id" :value="c.id">
              {{ conferenceLabel(c) }}
            </option>
          </optgroup>
        </select>
        <p
          id="tournament-conference-hint"
          class="mt-1 text-xs text-fg-muted"
          data-testid="tournament-conference-hint"
        >
          Search matches for teams in your conference.
        </p>
      </div>
 
      <!-- Teams the feed could not place in a conference only show under All
           conferences (and so under no single league). Said when it matters;
           never as "0", and not at all when the count is unknown. -->
      <p
        v-if="showUnplaced"
        class="mt-1 text-xs text-fg-muted"
        data-testid="tournament-unplaced-note"
      >
        {{ unplaced.count }} of {{ unplaced.total }}
        {{ unplaced.total === 1 ? 'team has' : 'teams have' }} no known
        conference;
        {{
          selectedConferenceId != null
            ? 'they appear under All conferences.'
            : 'they appear only under All.'
        }}
      </p>
    </div>
  </div>
</template>
 
<script setup>
import { computed, nextTick, ref } from 'vue';
import {
  conferenceDisplayName,
  findGroupOption,
  groupDisplayLabel,
  groupOptionId,
  offeredGroupsFor,
} from '../utils/tournamentDays';
 
const props = defineProps({
  // [{ id, name, matches, matches_on_date }] — a null count is not counted.
  ageGroups: { type: Array, default: () => [] },
  selectedAgeGroupId: { type: Number, default: null },
  // [{ key, division, label, matches }]. [] = this age has no groups;
  // null = unknown. Both hide the control.
  groups: { type: Array, default: null },
  selectedGroup: { type: String, default: null },
  // The stage of the Group option picked, when it was picked here: a stored
  // group can be an option of two stages (SB-1875).
  selectedGroupStage: { type: String, default: null },
  // [{ id, name, league, team_count }]. null / [] hide the control.
  conferences: { type: Array, default: null },
  selectedConferenceId: { type: Number, default: null },
  // { count, total } teams with no known conference, or null (unknown).
  unplaced: { type: Object, default: null },
  // 'Homegrown' | 'Academy' | null — chosen with the league toggle; narrows
  // the conferences and groups offered here.
  selectedLeague: { type: String, default: null },
  // [{ key, label, count }]. null / [] hide the Stage control.
  stages: { type: Array, default: null },
  selectedStage: { type: String, default: null },
  // Matches with no stage yet, or null (unknown — no note).
  unstagedCount: { type: Number, default: null },
  // A day is open: age counts are that day's.
  dayMode: { type: Boolean, default: false },
  disabled: { type: Boolean, default: false },
});
 
const emit = defineEmits([
  'update:ageGroup',
  'update:group',
  'update:conference',
  'update:stage',
]);
 
const labelClass =
  'block text-xs font-semibold text-fg-muted uppercase tracking-wider mb-1';
const selectClass =
  'block w-full h-11 sm:h-10 pl-3 pr-9 border border-line bg-card text-fg rounded-lg shadow-sm text-base sm:text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500 disabled:opacity-60';
 
const selectedConference = computed(
  () =>
    (props.conferences || []).find(c => c.id === props.selectedConferenceId) ??
    null
);
const selectedStageRow = computed(
  () => (props.stages || []).find(st => st.key === props.selectedStage) ?? null
);
// A conference (or league) narrows the groups to its own side (HD / AD) —
// SB-1753 — and a stage to its own.
const visibleGroups = computed(() =>
  offeredGroupsFor({
    groups: props.groups,
    conference: selectedConference.value,
    league: props.selectedLeague,
    stage: selectedStageRow.value,
  })
);
const selectedGroupOption = computed(() =>
  props.selectedGroup
    ? findGroupOption(
        props.groups,
        props.selectedGroup,
        props.selectedGroupStage ?? props.selectedStage
      )
    : null
);
// An option's value is its stored group, or — when the same group is an
// option of two stages — its stage and group (`groupOptionId`), so each is
// its own choice.
const sharedKeys = computed(() => {
  const seen = new Set();
  const shared = new Set();
  for (const g of visibleGroups.value || []) {
    if (seen.has(g.key)) shared.add(g.key);
    seen.add(g.key);
  }
  return shared;
});
// A group that is an option of two stages says which stage each one is.
const stageName = key =>
  (props.stages || []).find(st => st.key === key)?.label || key;
const groupText = g => {
  const text = groupDisplayLabel(g, props.selectedStage);
  return sharedKeys.value.has(g.key) && g.stage
    ? `${text} — ${stageName(g.stage)}`
    : text;
};
const optionValue = g =>
  sharedKeys.value.has(g.key) ? groupOptionId(g) : g.key;
const selectedGroupId = computed(() =>
  selectedGroupOption.value ? optionValue(selectedGroupOption.value) : ''
);
const hasGroups = computed(
  () => Array.isArray(visibleGroups.value) && visibleGroups.value.length > 0
);
// A conference counted at no teams here is not offered — except the one a
// link already selected, so the view does not silently change. An uncounted
// one (`team_count` null: the lookup failed) is still offered, with no count.
// A chosen league keeps only its own conferences.
const visibleConferences = computed(() =>
  (props.conferences || []).filter(
    c =>
      (c.team_count !== 0 || c.id === props.selectedConferenceId) &&
      (!props.selectedLeague || !c.league || c.league === props.selectedLeague)
  )
);
const hasConferences = computed(
  () => Array.isArray(props.conferences) && visibleConferences.value.length > 0
);
 
// Homegrown first, then Academy, then anything else the API sends.
const LEAGUE_ORDER = ['Homegrown', 'Academy'];
const conferenceLeagues = computed(() => {
  const byLeague = new Map();
  for (const c of visibleConferences.value) {
    const league = c.league || 'Other';
    if (!byLeague.has(league)) byLeague.set(league, []);
    byLeague.get(league).push(c);
  }
  const rank = name => {
    const i = LEAGUE_ORDER.indexOf(name);
    return i === -1 ? LEAGUE_ORDER.length : i;
  };
  return [...byLeague.entries()]
    .sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b))
    .map(([name, items]) => ({
      name,
      items: items.slice().sort((a, b) => a.name.localeCompare(b.name)),
    }));
});
 
const conferenceLabel = c =>
  typeof c.team_count === 'number'
    ? `${c.name} (${c.team_count} ${c.team_count === 1 ? 'team' : 'teams'})`
    : c.name;
 
// A count is shown only when there is one: a missing count is not "(0)".
const optionLabel = (name, matches) =>
  typeof matches === 'number' ? `${name} (${matches})` : name;
 
const ageCount = ag => (props.dayMode ? ag.matches_on_date : ag.matches);
 
const hasStages = computed(
  () => Array.isArray(props.stages) && props.stages.length > 0
);
const unstagedNote = computed(() => {
  const n = props.unstagedCount;
  const one = n === 1;
  const where = props.selectedStage
    ? `; ${one ? "it's" : "they're"} under All stages`
    : '';
  return `${n} ${one ? "match doesn't" : "matches don't"} have a stage yet${where}.`;
});
const stageOptions = computed(() => [
  { key: null, label: 'All stages', count: null },
  ...(props.stages || []).map(st => ({
    key: st.key,
    label: st.label || st.key,
    count: typeof st.count === 'number' ? st.count : null,
  })),
]);
 
const showUnplaced = computed(
  () =>
    (props.selectedConferenceId != null || !!props.selectedLeague) &&
    props.unplaced != null &&
    typeof props.unplaced.count === 'number' &&
    props.unplaced.count > 0
);
 
const ageName = computed(
  () =>
    props.ageGroups.find(ag => ag.id === props.selectedAgeGroupId)?.name ?? ''
);
 
const clearableChips = computed(() => {
  const out = [];
  if (props.selectedConferenceId != null) {
    const c = selectedConference.value;
    if (c) {
      out.push({
        key: 'conference',
        kind: 'conference',
        text: conferenceDisplayName(c, props.conferences),
        clear: () => emit('update:conference', null),
      });
    }
  }
  if (selectedStageRow.value) {
    out.push({
      key: 'stage',
      kind: 'stage',
      text: selectedStageRow.value.label || selectedStageRow.value.key,
      clear: () => emit('update:stage', null),
    });
  }
  if (props.selectedGroup) {
    out.push({
      key: 'group',
      kind: 'group',
      text: groupDisplayLabel(
        selectedGroupOption.value ?? { key: props.selectedGroup },
        props.selectedStage
      ),
      clear: () => emit('update:group', null),
    });
  }
  return out;
});
 
const sheetOpen = ref(false);
const firstControl = ref(null);
const openSheet = async () => {
  sheetOpen.value = true;
  await nextTick();
  firstControl.value?.focus?.();
};
const closeSheet = () => {
  sheetOpen.value = false;
};
 
const onAgeGroupChange = event => {
  const id = Number(event.target.value);
  if (Number.isFinite(id)) emit('update:ageGroup', id);
};
 
const onConferenceChange = event => {
  const raw = event.target.value;
  const id = raw === '' ? null : Number(raw);
  if (id === null || Number.isFinite(id)) emit('update:conference', id);
};
 
// The option a group is sent as carries its round (SB-1875), so the option,
// not just its key, goes with the change.
const onGroupChange = event => {
  const option =
    (visibleGroups.value || []).find(
      g => optionValue(g) === event.target.value
    ) ?? null;
  emit('update:group', option?.key ?? null, option);
};
 
defineExpose({ sheetOpen, openSheet, closeSheet });
</script>
 
<style scoped>
.no-scrollbar {
  scrollbar-width: none;
}
.no-scrollbar::-webkit-scrollbar {
  display: none;
}
</style>