-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update icon, add update modal and service worker updater
- Loading branch information
Showing
10 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Button, HStack } from '@chakra-ui/react' | ||
import { createContext, Dispatch, SetStateAction, useContext } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { useRegisterSW } from 'virtual:pwa-register/react' | ||
|
||
import { Modal } from '../components/layouts' | ||
import { Body1 } from '../components/typography' | ||
|
||
const defaultContext: ServiceWorkerUpdateProps = { | ||
needRefresh: false, | ||
setNeedRefresh() {}, | ||
updateServiceWorker: () => | ||
new Promise((resolve) => { | ||
resolve() | ||
}), | ||
} | ||
|
||
export type ServiceWorkerUpdateProps = { | ||
needRefresh: boolean | ||
setNeedRefresh: Dispatch<SetStateAction<boolean>> | ||
updateServiceWorker: (reloadPage?: boolean | undefined) => Promise<void> | ||
} | ||
|
||
export const ServiceWorkerUpdate = | ||
createContext<ServiceWorkerUpdateProps>(defaultContext) | ||
|
||
const InternalMS = 20 * 1000 | ||
|
||
export const ServiceWorkerProvider = ({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) => { | ||
const { t } = useTranslation() | ||
const { | ||
needRefresh: [needRefresh, setNeedRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ | ||
onRegisteredSW(swUrl, r) { | ||
if (r) { | ||
setInterval(async () => { | ||
if (!(!r.installing && navigator)) return | ||
|
||
if ('connection' in navigator && !navigator.onLine) return | ||
|
||
const resp = await fetch(swUrl, { | ||
cache: 'no-store', | ||
headers: { | ||
cache: 'no-store', | ||
'cache-control': 'no-cache', | ||
}, | ||
}).catch((error) => console.log('error', error)) | ||
console.log('is it getting here?', resp) | ||
|
||
if (resp?.status === 200) await r.update() | ||
}, InternalMS) | ||
} | ||
}, | ||
onRegisterError(error: any) { | ||
console.log('SW registration error', error) | ||
}, | ||
}) | ||
|
||
const handleConfirm = () => { | ||
updateServiceWorker(true) | ||
setNeedRefresh(false) | ||
} | ||
|
||
return ( | ||
<ServiceWorkerUpdate.Provider | ||
value={{ | ||
needRefresh, | ||
setNeedRefresh, | ||
updateServiceWorker, | ||
}} | ||
> | ||
{children} | ||
|
||
<Modal | ||
isOpen={needRefresh} | ||
onClose={() => setNeedRefresh(false)} | ||
title={t('Update available')} | ||
> | ||
<Body1> | ||
{t('There is a newer version of the app available')}{' '} | ||
{t('Would you like to reload the page to get the latest update?')} | ||
</Body1> | ||
<HStack w="full" justifyContent="flex-end" marginTop="20px"> | ||
<Button variant="secondary" onClick={() => setNeedRefresh(false)}> | ||
{t('No')} | ||
</Button> | ||
<Button variant="primary" onClick={handleConfirm}> | ||
{t('Yes')} | ||
</Button> | ||
</HStack> | ||
</Modal> | ||
</ServiceWorkerUpdate.Provider> | ||
) | ||
} | ||
|
||
export const useServiceWorkerUpdate = (): ServiceWorkerUpdateProps => | ||
useContext(ServiceWorkerUpdate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters