|
1 | 1 | 'use client';
|
2 | 2 |
|
3 | 3 | import dynamic from 'next/dynamic';
|
4 |
| -import { memo, useEffect, useLayoutEffect } from 'react'; |
5 |
| -import { useTranslation } from 'react-i18next'; |
| 4 | +import { memo } from 'react'; |
6 | 5 |
|
7 |
| -import { BRANDING_NAME } from '@/const/branding'; |
8 |
| -import { PWA_INSTALL_ID } from '@/const/layoutTokens'; |
9 |
| -import { usePWAInstall } from '@/hooks/usePWAInstall'; |
10 | 6 | import { usePlatform } from '@/hooks/usePlatform';
|
11 |
| -import { useGlobalStore } from '@/store/global'; |
12 |
| -import { systemStatusSelectors } from '@/store/global/selectors'; |
13 | 7 | import { useUserStore } from '@/store/user';
|
14 | 8 |
|
15 |
| -// @ts-ignore |
16 |
| -const PWA: any = dynamic(() => import('@khmyznikov/pwa-install/dist/pwa-install.react.js'), { |
| 9 | +const Install: any = dynamic(() => import('./Install'), { |
17 | 10 | ssr: false,
|
18 | 11 | });
|
19 | 12 |
|
20 | 13 | const PWAInstall = memo(() => {
|
21 |
| - const { t } = useTranslation('metadata'); |
22 | 14 | const { isPWA } = usePlatform();
|
23 |
| - |
24 |
| - const { install, canInstall } = usePWAInstall(); |
25 |
| - |
26 | 15 | const isShowPWAGuide = useUserStore((s) => s.isShowPWAGuide);
|
27 |
| - const [hidePWAInstaller, updateSystemStatus] = useGlobalStore((s) => [ |
28 |
| - systemStatusSelectors.hidePWAInstaller(s), |
29 |
| - s.updateSystemStatus, |
30 |
| - ]); |
31 |
| - |
32 |
| - // we need to make the pwa installer hidden by default |
33 |
| - useLayoutEffect(() => { |
34 |
| - sessionStorage.setItem('pwa-hide-install', 'true'); |
35 |
| - }, []); |
36 |
| - |
37 |
| - const pwaInstall = |
38 |
| - // eslint-disable-next-line unicorn/prefer-query-selector |
39 |
| - typeof window === 'undefined' ? undefined : document.getElementById(PWA_INSTALL_ID); |
40 |
| - |
41 |
| - // add an event listener to control the user close installer action |
42 |
| - useEffect(() => { |
43 |
| - if (!pwaInstall) return; |
44 |
| - |
45 |
| - const handler = (e: Event) => { |
46 |
| - const event = e as CustomEvent; |
47 |
| - |
48 |
| - // it means user hide installer |
49 |
| - if (event.detail.message === 'dismissed') { |
50 |
| - updateSystemStatus({ hidePWAInstaller: true }); |
51 |
| - } |
52 |
| - }; |
53 |
| - |
54 |
| - pwaInstall.addEventListener('pwa-user-choice-result-event', handler); |
55 |
| - return () => { |
56 |
| - pwaInstall.removeEventListener('pwa-user-choice-result-event', handler); |
57 |
| - }; |
58 |
| - }, [pwaInstall]); |
59 |
| - |
60 |
| - // trigger the PWA guide on demand |
61 |
| - useEffect(() => { |
62 |
| - if (!canInstall || hidePWAInstaller) return; |
63 | 16 |
|
64 |
| - if (isShowPWAGuide) { |
65 |
| - install(); |
66 |
| - } |
67 |
| - }, [canInstall, hidePWAInstaller, isShowPWAGuide]); |
| 17 | + if (isPWA || !isShowPWAGuide) return null; |
68 | 18 |
|
69 |
| - if (isPWA) return null; |
70 |
| - return ( |
71 |
| - <PWA |
72 |
| - description={t('chat.description', { appName: BRANDING_NAME })} |
73 |
| - id={PWA_INSTALL_ID} |
74 |
| - manifest-url={'/manifest.webmanifest'} |
75 |
| - /> |
76 |
| - ); |
| 19 | + // only when the user is suitable for the pwa install and not install the pwa |
| 20 | + // then show the installation guide |
| 21 | + return <Install />; |
77 | 22 | });
|
78 | 23 |
|
79 | 24 | export default PWAInstall;
|
0 commit comments