All files / src/components InstallBanner.vue

100% Statements 102/102
96% Branches 24/25
100% Functions 3/3
100% Lines 102/102

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2511x 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 21x 21x 21x 21x 21x   21x 1x   36x 36x 36x 36x   36x 1x 1x 1x       1x 1x       1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 96x 96x 45x 1x   1x   1x 1x 1x 1x 1x 49x 49x 49x 1x   1x 49x 49x 49x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   2x 2x 2x 2x 2x   1x 23x 23x 23x 23x 23x 23x 1x                                                                                                                                                                                                                                                            
<template>
  <transition name="install-banner-fade">
    <div
      v-if="visible"
      class="install-banner"
      data-testid="install-banner"
      :data-mode="mode"
    >
      <button
        class="install-dismiss"
        aria-label="Dismiss"
        data-testid="install-banner-dismiss"
        @click.stop="dismiss"
      >
        Γ—
      </button>
      <div class="install-body">
        <span class="install-icon" aria-hidden="true">{{
          mode === 'ios' ? 'πŸ””' : '⚽'
        }}</span>
        <div class="install-text">
          <strong>{{ headline }}</strong>
          <span>{{ subline }}</span>
        </div>
      </div>
      <!-- iOS gets instructions because Apple ships no install API; everywhere
           else gets the real thing. The old version of this banner was a filled
           blue card with no click handler at all β€” users tapped it, nothing
           happened, and they gave up (SB-810). -->
      <button
        v-if="mode === 'ios'"
        class="install-action"
        data-testid="install-banner-action"
        @click="openGuide"
      >
        Show me how
      </button>
      <button
        v-else
        class="install-action"
        :disabled="prompting"
        data-testid="install-banner-install"
        @click="onInstall"
      >
        {{ prompting ? 'Installing…' : 'Install' }}
      </button>
    </div>
  </transition>
</template>
 
<script setup>
import { ref, computed, onMounted } from 'vue';
import { isIosNonStandalone, isTouchDevice } from '../utils/pwa';
import {
  openSetupGuide,
  useNotificationSetup,
} from '../composables/useNotificationSetup';
import { useInstallPrompt } from '../composables/useInstallPrompt';
 
const iosEligible = ref(false);
// Same dismissal record the guide writes: the banner and the guide are two
// faces of one nag, so "not now" in either silences both β€” including a
// dismissal that happens in the guide while this banner sits behind it.
const { dismissPrompt, promptDismissed } = useNotificationSetup();
const { canInstall, prompting, promptInstall } = useInstallPrompt();
 
// Phones only. Chromium offers beforeinstallprompt on desktop too, but a
// fixed bottom banner on a laptop is noise β€” the guide still carries the
// optional install offer there for anyone who wants it.
const nativeEligible = computed(() => canInstall.value && isTouchDevice());
 
/**
 * 'ios'    β€” no install API exists; the banner routes to instructions
 * 'native' β€” Chromium handed us a real prompt; the banner fires it
 */
const mode = computed(() => {
  if (iosEligible.value) return 'ios';
  if (nativeEligible.value) return 'native';
  return null;
});
 
const visible = computed(() => mode.value !== null && !promptDismissed.value);
 
// The pitch differs because the platforms differ. On iPhone, installing is the
// only way to get notifications at all. On Android notifications already work
// in a tab, so claiming otherwise would be inventing a barrier Google doesn't
// impose β€” there the honest pitch is the home-screen icon itself.
const headline = computed(() =>
  mode.value === 'ios'
    ? 'Want live match alerts?'
    : 'Add Missing Table to your phone'
);
 
const subline = computed(() =>
  mode.value === 'ios'
    ? 'iPhones can only send them from a home-screen app. Takes about 15 seconds.'
    : 'One tap to put it on your home screen β€” scores and alerts without hunting for a tab.'
);
 
function dismiss() {
  dismissPrompt();
}
 
async function onInstall() {
  const { outcome } = await promptInstall();
  // A dismissed native dialog is not a "leave me alone" β€” the user may just
  // have mis-tapped, and Chromium may offer the event again. Only record a
  // dismissal when they dismiss OUR banner.
  if (outcome === 'accepted') iosEligible.value = false;
}
 
function openGuide() {
  // Don't record a dismissal here β€” they said yes, not "not now". The banner
  // stays eligible so it returns if they close the guide without finishing.
  openSetupGuide('install');
}
 
onMounted(() => {
  // Every iOS browser, not just Safari: they all share WebKit, so they all
  // need the home-screen install before Web Push works. The Safari-only check
  // this replaced meant iPhone users on Chrome saw nothing at all (SB-810).
  // iOS is decided at mount (a UA + display-mode check, stable for the
  // session). The native path is event-driven, so it stays reactive.
  if (isIosNonStandalone()) iosEligible.value = true;
});
</script>
 
<style scoped>
.install-banner {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  z-index: 1000;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgb(var(--color-card));
  color: rgb(var(--color-fg));
  border: 1px solid rgb(var(--color-line));
  border-left: 4px solid #1e40af;
  border-radius: 12px;
  padding: 12px 34px 12px 14px;
  box-shadow:
    0 10px 25px -5px rgba(0, 0, 0, 0.25),
    0 4px 6px -2px rgba(0, 0, 0, 0.15);
  font-size: 13.5px;
  line-height: 1.35;
}
 
.install-body {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}
 
.install-icon {
  font-size: 20px;
  line-height: 1;
  flex-shrink: 0;
}
 
.install-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
 
.install-text strong {
  font-weight: 700;
}
 
.install-text span {
  color: rgb(var(--color-fg-muted));
  font-size: 12.5px;
}
 
.install-action {
  flex-shrink: 0;
  min-height: 40px;
  padding: 0 14px;
  border: none;
  border-radius: 8px;
  background: #1e40af;
  color: white;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
}
 
.install-action:hover:not(:disabled),
.install-action:focus-visible {
  background: #1d4ed8;
  outline: none;
}
 
.install-action:disabled {
  opacity: 0.65;
  cursor: progress;
}
 
.install-dismiss {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 26px;
  height: 26px;
  border: none;
  background: transparent;
  color: rgb(var(--color-fg-muted));
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
}
 
.install-dismiss:hover,
.install-dismiss:focus-visible {
  background: rgb(var(--color-line));
  outline: none;
}
 
@media (max-width: 380px) {
  .install-banner {
    flex-wrap: wrap;
  }
  .install-action {
    width: 100%;
  }
}
 
.install-banner-fade-enter-active,
.install-banner-fade-leave-active {
  transition:
    opacity 0.25s ease,
    transform 0.25s ease;
}
 
.install-banner-fade-enter-from,
.install-banner-fade-leave-to {
  opacity: 0;
  transform: translateY(12px);
}
</style>