All files / app-play/src loading-overlay.ts

95.65% Statements 22/23
100% Branches 6/6
75% Functions 3/4
95.65% Lines 22/23

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                                          6x 6x 6x 6x     6x 6x       6x 6x                             8x 8x   6x 6x 6x 6x     2x 2x 2x 1x     8x 1x   8x       8x    
import { render } from 'lit';
import { localized } from './ui/localized.ts';
import type { LoadProgress } from '@be-music/player-browser/collection';
import { playerText } from './player-copy.ts';
 
/**
 * DOM handles the loading-overlay controller writes into. Subset of {@link PlayerWebElements} so callers only need
 * to forward the four overlay-specific elements rather than the whole player element bundle.
 */
export interface LoadingOverlayElements {
  loadingOverlay: HTMLDivElement;
  loadingLabel: HTMLDivElement;
  loadingBarFill: HTMLElement;
  loadingCounter: HTMLDivElement;
}
 
/**
 * Reveals the centered loading overlay and resets its readout to a neutral "Loading…" state. The actual phase / counter
 * text fills in via {@link applyLoadProgress} as events fire from the loaders.
 */
export function showLoadingOverlay(elements: LoadingOverlayElements): void {
  elements.loadingOverlay.classList.add('visible');
  elements.loadingOverlay.setAttribute('aria-hidden', 'false');
  render(localized('loading'), elements.loadingLabel);
  render('', elements.loadingCounter);
  // Reset to indeterminate (no inline width) until the first `applyLoadProgress` lands. The CSS animates the bar so
  // the user sees motion even before the first phase event fires.
  elements.loadingBarFill.classList.add('indeterminate');
  elements.loadingBarFill.style.width = '';
}
 
export function hideLoadingOverlay(elements: LoadingOverlayElements): void {
  elements.loadingOverlay.classList.remove('visible');
  elements.loadingOverlay.setAttribute('aria-hidden', 'true');
}
 
/**
 * Maps a `LoadProgress` event from the player-web loaders onto the overlay DOM. Phases:
 *
 * - `enumerating` — total is `-1` (we're still walking the drop tree). Show the running file count + the current
 *   path, leave the bar in indeterminate animation mode.
 * - `reading` / `parsing` / `theme` — total is known. Switch the bar to determinate mode and set its width to
 *   `current / total`.
 *
 * Phase prefixes (`Reading files…` etc.) come from the `phaseLabels` map; the per-item label surfaces the underlying
 * filename / sub-task so the user can see which file is the current bottleneck.
 */
export function applyLoadProgress(elements: LoadingOverlayElements, progress: LoadProgress): void {
  const counterFragments: string[] = [];
  if (progress.total > 0) {
    // Determinate phase — set explicit width and pin the counter to "X / N (P%)" so the user can eyeball ETA.
    const ratio = Math.max(0, Math.min(1, progress.current / progress.total));
    elements.loadingBarFill.classList.remove('indeterminate');
    elements.loadingBarFill.style.width = `${(ratio * 100).toFixed(1)}%`;
    counterFragments.push(`${progress.current} / ${progress.total}`);
  } else {
    // Indeterminate (enumeration) — only `current` is meaningful.
    elements.loadingBarFill.classList.add('indeterminate');
    elements.loadingBarFill.style.width = '';
    if (progress.current > 0) {
      counterFragments.push(`${progress.current}`);
    }
  }
  if (progress.label) {
    counterFragments.push(progress.label);
  }
  render(
    localized(() => playerText().phases[progress.phase]),
    elements.loadingLabel,
  );
  render(counterFragments.join(' · '), elements.loadingCounter);
}