npm-pkg-hook 1.9.6 → 1.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/hooks/index.js +1 -0
- package/src/hooks/usePWAInstall/index.js +38 -0
- package/src/index.jsx +3 -0
package/package.json
CHANGED
package/src/hooks/index.js
CHANGED
|
@@ -22,6 +22,7 @@ export * from './useUploadProducts'
|
|
|
22
22
|
export * from './useAmountInput'
|
|
23
23
|
export * from './useColorByLetters'
|
|
24
24
|
export * from './newMessageSubscription'
|
|
25
|
+
export * from './usePWAInstall'
|
|
25
26
|
export * from './useGetCookies'
|
|
26
27
|
export * from './generateTemplate'
|
|
27
28
|
export * from './isTokenExpired'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export const usePWAInstall = () => {
|
|
4
|
+
const [deferredPrompt, setDeferredPrompt] = useState(null);
|
|
5
|
+
const [isInstallable, setIsInstallable] = useState(false);
|
|
6
|
+
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const handleBeforeInstallPrompt = (e) => {
|
|
9
|
+
e.preventDefault(); // Evita que el navegador muestre el diálogo automáticamente
|
|
10
|
+
setDeferredPrompt(e); // Almacena el evento para que puedas llamarlo más tarde
|
|
11
|
+
setIsInstallable(true); // Marca que la PWA es instalable
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
|
|
15
|
+
|
|
16
|
+
return () => {
|
|
17
|
+
window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt);
|
|
18
|
+
};
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
const installPWA = () => {
|
|
22
|
+
if (deferredPrompt) {
|
|
23
|
+
deferredPrompt.prompt();
|
|
24
|
+
|
|
25
|
+
deferredPrompt.userChoice.then((choiceResult) => {
|
|
26
|
+
if (choiceResult.outcome === 'accepted') {
|
|
27
|
+
console.log('User accepted the install prompt');
|
|
28
|
+
} else {
|
|
29
|
+
console.log('User dismissed the install prompt');
|
|
30
|
+
}
|
|
31
|
+
setDeferredPrompt(null); // Limpia el evento después de usarlo
|
|
32
|
+
setIsInstallable(false); // Oculta el botón de instalación
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return { isInstallable, installPWA };
|
|
38
|
+
};
|