All files / app-play/src/ui course-missing.ts

0% Statements 0/24
0% Branches 0/20
0% Functions 0/9
0% Lines 0/20

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                                                                                                                                                                                               
import { html, nothing, render, type TemplateResult } from 'lit';
import type { LibraryCourse } from '@be-music/player-scene-model';
import { describeMissingCourseStages, type MissingCourseStage, type MissingStageLink } from '../course-table-songs.ts';
import { playerText } from '../player-copy.ts';
import { dialogShortcutAria, dialogShortcutHint } from './dialog-shortcuts.ts';
 
// The dialog a course opens when it is decided with stages missing from the collection (LR2 and beatoraja refuse to
// start it too). It lists those stages, each with the pages its difficulty table names for it (the song, its chart
// when it ships apart, the pack), which open in a new tab: the player downloads and loads them, then plays the course.
 
/**
 * Opens the dialog for `course`: its missing stages at once, then their download pages once `lookup` (the table's song
 * list) answers. A lookup that answers after the dialog closed, or after it opened for another course, is dropped.
 */
export function showMissingCourseStages(
  course: LibraryCourse,
  lookup: (course: LibraryCourse) => Promise<readonly MissingCourseStage[]>,
): void {
  const dialog = document.querySelector<HTMLDialogElement>('#course-missing');
  if (dialog === null) return;
  const token = Symbol(course.id);
  activeToken = token;
  render(template(course, describeMissingCourseStages(course), true), dialog);
  // The dialog owns keyboard navigation; its keys must not reach the select screen behind it.
  dialog.onkeydown = (event) => event.stopPropagation();
  if (!dialog.open) dialog.showModal();
  void lookup(course).then(
    (stages) => {
      if (activeToken === token && dialog.open) render(template(course, stages, false), dialog);
    },
    () => {
      if (activeToken === token && dialog.open)
        render(template(course, describeMissingCourseStages(course), false), dialog);
    },
  );
}
 
let activeToken: symbol | undefined;
 
function linkLabel(link: MissingStageLink): string {
  const t = playerText().course.missingStages;
  const kind = t[link.kind];
  return link.name !== undefined ? `${kind}: ${link.name}` : kind;
}
 
function stageItem(stage: MissingCourseStage, looking: boolean): TemplateResult {
  const t = playerText().course.missingStages;
  return html`<li class="course-missing-stage">
    <span class="course-missing-number">${t.stage(stage.stage)}</span>
    <span class="course-missing-title">${stage.title}</span>
    ${stage.artist !== undefined ? html`<span class="course-missing-artist">${stage.artist}</span>` : nothing}
    <span class="course-missing-links">
      ${
        stage.links.length > 0
          ? stage.links.map(
              (link) =>
                html`<a
                  class="btn"
                  data-size="sm"
                  data-variant="outline"
                  href=${link.href}
                  target="_blank"
                  rel="noopener noreferrer"
                  >${linkLabel(link)}</a
                >`,
            )
          : html`<span class="course-missing-none">${looking ? t.looking : t.noLink}</span>`
      }
    </span>
  </li>`;
}
 
function template(course: LibraryCourse, stages: readonly MissingCourseStage[], looking: boolean): TemplateResult {
  const t = playerText().course.missingStages;
  return html`<form method="dialog" class="account-dialog-body">
    <header>
      <h2 id="course-missing-title">${t.title}</h2>
      <p id="course-missing-hint">${t.hint(course.definition.name)}</p>
    </header>
    <ol class="course-missing-list">
      ${stages.map((stage) => stageItem(stage, looking))}
    </ol>
    <footer>
      <button
        type="submit"
        class="btn"
        autofocus
        data-dialog-shortcut="primary"
        aria-keyshortcuts=${dialogShortcutAria('primary')}
      >
        ${playerText().shell.ok}${dialogShortcutHint('primary')}
      </button>
    </footer>
  </form>`;
}