All files / app-play/src install-offer.ts

100% Statements 15/15
100% Branches 18/18
100% Functions 1/1
100% Lines 10/10

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                                    10x 8x 7x     7x 10x 4x 10x   2x 2x 1x    
// Whether the header offers to install the player, and how: Chromium hands the page an install prompt
// (`beforeinstallprompt`), while Safari has no prompt, so the button explains where Safari keeps its own command.
 
/** How the player can be installed here: Chromium's prompt, or Safari's Share or File menu. */
export type InstallOffer = 'prompt' | 'ios-safari' | 'mac-safari';
 
export interface InstallOfferInput {
  /** The page already runs installed (`display-mode: standalone` or the like, or iOS's `navigator.standalone`). */
  readonly installed: boolean;
  /** Chromium fired `beforeinstallprompt` and the page kept it. */
  readonly hasPrompt: boolean;
  readonly userAgent: string;
  /** `navigator.maxTouchPoints`: an iPad asks for the desktop site and says it is a Mac. */
  readonly maxTouchPoints: number;
}
 
/** The install the header offers, or `undefined` when there is none to offer (installed, or no way to install). */
export function resolveInstallOffer(input: InstallOfferInput): InstallOffer | undefined {
  if (input.installed) return undefined;
  if (input.hasPrompt) return 'prompt';
  const ua = input.userAgent;
  // Safari only: Chrome, Edge and Firefox on iOS name themselves (CriOS / EdgiOS / FxiOS) and cannot add to the Home
  // Screen from their own menu in the same way, and Chromium on a Mac fires the prompt instead.
  const safari = /Safari\//.test(ua) && !/(Chrome|Chromium|CriOS|EdgiOS|FxiOS|Edg|OPR|Android)\//.test(ua);
  if (!safari) return undefined;
  const iOS = /\b(iPhone|iPad|iPod)\b/.test(ua) || (/\bMacintosh\b/.test(ua) && input.maxTouchPoints > 1);
  if (iOS) return 'ios-safari';
  // Safari 17 on macOS Sonoma adds a web app to the Dock.
  const version = /Version\/(\d+)/.exec(ua);
  if (/\bMacintosh\b/.test(ua) && version !== null && Number(version[1]) >= 17) return 'mac-safari';
  return undefined;
}