Skip to content

Commit

Permalink
fix: update icon, add update modal and service worker updater
Browse files Browse the repository at this point in the history
  • Loading branch information
sajald77 committed Aug 9, 2023
1 parent 537bc40 commit 71d5d5b
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 21 deletions.
Binary file modified public/icons/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icons/180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icons/192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icons/32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified public/icons/512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 28 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ import { BrowserRouter } from 'react-router-dom'
import { AppLayout } from './AppLayout'
import { client } from './config'
import { Head } from './config/Head'
import { AuthProvider, ChakraThemeProvider, NavProvider } from './context'
import {
AuthProvider,
ChakraThemeProvider,
NavProvider,
ServiceWorkerProvider,
} from './context'
import { BtcProvider } from './context/btc'

export const App = () => (
<ChakraProvider>
<ChakraThemeProvider>
<BrowserRouter>
<ApolloProvider client={client}>
<AuthProvider>
<NavProvider>
<BtcProvider>
<Head />
<AppLayout />
</BtcProvider>
</NavProvider>
</AuthProvider>
</ApolloProvider>
</BrowserRouter>
</ChakraThemeProvider>
</ChakraProvider>
)
export const App = () => {
return (
<ChakraProvider>
<ChakraThemeProvider>
<ServiceWorkerProvider>
<BrowserRouter>
<ApolloProvider client={client}>
<AuthProvider>
<NavProvider>
<BtcProvider>
<Head />
<AppLayout />
</BtcProvider>
</NavProvider>
</AuthProvider>
</ApolloProvider>
</BrowserRouter>
</ServiceWorkerProvider>
</ChakraThemeProvider>
</ChakraProvider>
)
}
1 change: 1 addition & 0 deletions src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './auth'
export * from './filter'
export * from './nav'
export * from './project'
export * from './serviceWorkerUpdate'
export * from './theme'
102 changes: 102 additions & 0 deletions src/context/serviceWorkerUpdate.tsx
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)
6 changes: 5 additions & 1 deletion src/translations/English.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,5 +645,9 @@
"Check your SPAM folder for the email.":"Check your SPAM folder for the email.",
"As a project creator, you cannot delete your profile":"As a project creator, you cannot delete your profile",
"Successfully updated user email":"Successfully updated user email",
"Email verification successfull!":"Email verification successfull!"
"Email verification successfull!":"Email verification successfull!",
"Update available":"Update available",
"There is a newer version of the app available":"There is a newer version of the app available",
"Would you like to reload the page to get the latest update?":"Would you like to reload the page to get the latest update?"

}
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { VitePWA, VitePWAOptions } from 'vite-plugin-pwa'
const pwaOptions: Partial<VitePWAOptions> = {
mode: 'development',
base: '/',
registerType: 'autoUpdate',
includeAssets: ['logo-brand.svg'],
manifest: {
start_url: '.',
Expand Down

0 comments on commit 71d5d5b

Please sign in to comment.