All files / app-play/src replay-view-settings.ts

86.36% Statements 19/22
85.71% Branches 12/14
80% Functions 4/5
85% Lines 17/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      8x                           8x   8x       17x 17x 17x 17x       17x 17x 68x   17x             11x 11x   2x                 2x 2x   8x                            
import type { ReplayPlaybackOptions } from '@be-music/player-engine/replay';
import { optionDefault } from './option-defaults.ts';
 
const DEFAULT_REPLAY_VIEW: Readonly<ReplayPlaybackOptions> = {
  tempo: optionDefault('replayViewTempo'),
  hiSpeed: optionDefault('replayViewHiSpeed'),
  covers: optionDefault('replayViewCovers'),
  lift: optionDefault('replayViewLift'),
};
 
/**
 * The Options dialog's "Replay / ghost" settings: which of what a replay recorded the player seeing (tempo, hi-speed,
 * SUDDEN+ / HIDDEN+, LIFT) a replay or ghost reproduces, the rest staying the viewer's own. All on by default;
 * remembered in `localStorage` and read once per play, at its start.
 */
 
/** `localStorage` key of the replay view settings. */
export const REPLAY_VIEW_STORAGE_KEY = 'be-music.replay-view';
 
const KEYS = ['tempo', 'hiSpeed', 'covers', 'lift'] as const satisfies readonly (keyof ReplayPlaybackOptions)[];
 
/** Stored settings, each toggle checked on its own; on (the default) for anything missing or unreadable. */
export function parseStoredReplayView(raw: string | null | undefined): ReplayPlaybackOptions {
  let stored: Record<string, unknown> = {};
  try {
    const parsed: unknown = raw === null || raw === undefined ? undefined : JSON.parse(raw);
    if (typeof parsed === 'object' && parsed !== null) stored = parsed as Record<string, unknown>;
  } catch {
    // Unreadable: the defaults.
  }
  const settings = { ...DEFAULT_REPLAY_VIEW };
  for (const key of KEYS) {
    if (typeof stored[key] === 'boolean') settings[key] = stored[key];
  }
  return settings;
}
 
/** The remembered settings, or the defaults when none are stored or storage is blocked. */
export function readStoredReplayView(
  storage: Pick<Storage, 'getItem'> | undefined = defaultStorage(),
): ReplayPlaybackOptions {
  try {
    return parseStoredReplayView(storage?.getItem(REPLAY_VIEW_STORAGE_KEY));
  } catch {
    return { ...DEFAULT_REPLAY_VIEW };
  }
}
 
/** Remembers the settings for the next visit. */
export function storeReplayView(
  settings: Readonly<ReplayPlaybackOptions>,
  storage: Pick<Storage, 'setItem'> | undefined = defaultStorage(),
): void {
  try {
    storage?.setItem(
      REPLAY_VIEW_STORAGE_KEY,
      JSON.stringify(Object.fromEntries(KEYS.map((key) => [key, settings[key]]))),
    );
  } catch {
    // Private windows / blocked storage: the settings still apply for this session.
  }
}
 
function defaultStorage(): Storage | undefined {
  try {
    return typeof window === 'undefined' ? undefined : window.localStorage;
  } catch {
    return undefined;
  }
}