All files / src/composables useInstallPrompt.js

100% Statements 94/94
89.47% Branches 17/19
100% Functions 6/6
100% Lines 94/94

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 1091x 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   4x 4x 4x 4x 4x 4x   1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 106x 106x 106x 106x 106x   106x 106x 106x 106x 106x 106x 106x 7x 2x 2x 5x 5x 5x 5x 5x 5x 5x   5x 5x 7x 7x 7x 1x 1x 7x 5x 5x 7x   106x 106x 106x 106x 106x 106x 106x   1x 1x 10x 10x 10x 10x 10x   1x 1x 5x 5x 5x  
/**
 * Native install prompt (SB-813).
 *
 * Chromium fires `beforeinstallprompt` when a site meets the installability
 * criteria. Suppressing the default mini-infobar and stashing the event lets us
 * offer install at a moment that makes sense, with a real one-tap button.
 *
 * This is Android/desktop only, and it is the mirror image of the iOS problem:
 * there we have no install API and must hand-write instructions (SB-810);
 * here the browser hands us a working one and MT simply never used it.
 *
 * Two rules the API imposes:
 *   - `prompt()` must be called from a user gesture, so the event is stored
 *     rather than fired on arrival
 *   - each captured event is single-use; after prompting it must be discarded
 *
 * Listeners attach at module load, not inside a component: the event fires
 * early in page life and is offered exactly once, so a listener registered
 * after mount can miss it entirely. main.js imports this for that reason.
 */
 
import { ref, computed } from 'vue';
import { isStandalone } from '../utils/pwa';
 
// The stashed BeforeInstallPromptEvent. Deliberately not a ref — it's a
// non-reactive browser object; `canInstall` carries the reactive signal.
let deferredPrompt = null;
 
const promptAvailable = ref(false);
const installed = ref(false);
const prompting = ref(false);
 
function onBeforeInstallPrompt(event) {
  // Stops Chrome's own mini-infobar so we control placement and timing.
  event.preventDefault();
  deferredPrompt = event;
  promptAvailable.value = true;
}
 
function onAppInstalled() {
  installed.value = true;
  promptAvailable.value = false;
  deferredPrompt = null;
}
 
if (typeof window !== 'undefined') {
  window.addEventListener('beforeinstallprompt', onBeforeInstallPrompt);
  window.addEventListener('appinstalled', onAppInstalled);
}
 
export function useInstallPrompt() {
  // Already running as an installed app: nothing to offer. Checked live rather
  // than cached, since a session can start in a tab and continue standalone.
  const canInstall = computed(
    () => promptAvailable.value && !installed.value && !isStandalone()
  );
 
  /**
   * Fire the native install dialog. Must be called from a user gesture.
   *
   * Returns the user's choice. A dismissal is not a failure — Chrome may hand
   * us another event later, but this particular one is spent either way.
   */
  async function promptInstall() {
    if (!deferredPrompt || prompting.value) {
      return { outcome: 'unavailable' };
    }
    prompting.value = true;
    try {
      const event = deferredPrompt;
      // Discard before awaiting: the event is single-use, and leaving it in
      // place would let a second tap call prompt() on a spent event.
      deferredPrompt = null;
      promptAvailable.value = false;
 
      event.prompt();
      const choice = await event.userChoice;
      if (choice?.outcome === 'accepted') installed.value = true;
      return { outcome: choice?.outcome || 'dismissed' };
    } catch {
      // A spent or invalidated event throws; treat it as nothing to offer.
      return { outcome: 'unavailable' };
    } finally {
      prompting.value = false;
    }
  }
 
  return {
    canInstall,
    installed: computed(() => installed.value),
    prompting: computed(() => prompting.value),
    promptInstall,
  };
}
 
// Test-only: reset module-level singleton between tests.
export function _resetInstallPromptForTest() {
  deferredPrompt = null;
  promptAvailable.value = false;
  installed.value = false;
  prompting.value = false;
}
 
// Test-only: inject a fake BeforeInstallPromptEvent without dispatching.
export function _setDeferredPromptForTest(event) {
  deferredPrompt = event;
  promptAvailable.value = !!event;
}