diff --git a/public/pwaInstall.js b/public/pwaInstall.js index 2fe94d0..d427d8c 100644 --- a/public/pwaInstall.js +++ b/public/pwaInstall.js @@ -1,39 +1,28 @@ // src/pwaInstall.js let deferredPrompt = null; +// Écoute l'événement natif PWA (se déclenche quand Chrome pense que l'app est installable) window.addEventListener('beforeinstallprompt', (e) => { + // Empêche le prompt auto (on veut le contrôler avec le bouton) e.preventDefault(); deferredPrompt = e; - console.log('PWA install prompt ready'); + console.log('PWA install prompt ready - waiting for user click'); }); +// Fonction appelée quand on clique sur le bouton "Install" export function promptInstall() { if (deferredPrompt) { - deferredPrompt.prompt(); - deferredPrompt.userChoice.then((choice) => { - console.log('Install outcome:', choice.outcome); + deferredPrompt.prompt(); // Affiche le prompt natif "Add to home screen" + deferredPrompt.userChoice.then((choiceResult) => { + if (choiceResult.outcome === 'accepted') { + console.log('User accepted PWA install'); + } else { + console.log('User dismissed PWA install'); + } deferredPrompt = null; }); } else { - console.log('No install prompt available'); + console.log('No install prompt available yet - try again later'); + alert('PWA installation not ready yet. Try again in a few seconds.'); } -} - -// Fonction pour ajouter le bouton dans TopBar (on l'appelle depuis TopBar.jsx) -export function addPwaInstallButton(container) { - const btn = document.createElement('button'); - btn.innerHTML = ' Installer'; // icône + texte - btn.style.marginLeft = '10px'; - btn.style.padding = '6px 12px'; - btn.style.fontSize = '12px'; - btn.style.background = '#0ea5e9'; - btn.style.color = 'white'; - btn.style.border = 'none'; - btn.style.borderRadius = '999px'; - btn.style.cursor = 'pointer'; - btn.style.display = 'flex'; - btn.style.alignItems = 'center'; - btn.style.gap = '6px'; - btn.onclick = promptInstall; - container.appendChild(btn); } \ No newline at end of file diff --git a/src/components/Layout/TopBar.jsx b/src/components/Layout/TopBar.jsx index ad77fe8..93151d7 100644 --- a/src/components/Layout/TopBar.jsx +++ b/src/components/Layout/TopBar.jsx @@ -3,6 +3,9 @@ import { useAuth } from "../../auth/AuthContext"; import { registerUser } from "../../api/client"; import AuthToast from "../Auth/AuthToast"; +// Import pour le bouton PWA +import { promptInstall } from '../../pwaInstall'; + const THEME_LABELS = { dark: "Dark", blue: "Blue", light: "Light" }; const LAST_SIGNUP_KEY = "sociowire:lastSignup"; @@ -219,16 +222,6 @@ export default function TopBar({ theme = "dark", onChangeTheme }) { return null; }, [lastError, lastInfo, needsEmailVerification]); - // Fonction pour afficher le prompt PWA (appelée par le bouton) - const handleInstall = () => { - if ('beforeinstallprompt' in window) { - const e = new Event('beforeinstallprompt'); - window.dispatchEvent(e); - } else { - alert('PWA installation not supported on this browser.'); - } - }; - return ( <> {/* Toast area (under topbar) */} @@ -274,7 +267,7 @@ export default function TopBar({ theme = "dark", onChangeTheme }) { {/* BOUTON INSTALL PWA ici en haut */}