forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePWA.ts
More file actions
34 lines (27 loc) · 923 Bytes
/
Copy pathusePWA.ts
File metadata and controls
34 lines (27 loc) · 923 Bytes
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
import { useEffect, useState } from 'react';
export const usePWA = () => {
const [isOnline, setIsOnline] = useState(true);
const [installPrompt, setInstallPrompt] = useState<any>(null);
useEffect(() => {
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
setInstallPrompt(e);
});
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
const installApp = async () => {
if (installPrompt) {
installPrompt.prompt();
await installPrompt.userChoice;
setInstallPrompt(null);
}
};
return { isOnline, installPrompt, installApp };
};