forked from hamster1963/nezha-dash-v1
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4032ff6
commit 9073fa6
Showing
8 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
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,52 @@ | ||
import { useRegisterSW } from 'virtual:pwa-register/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { toast } from 'sonner'; | ||
|
||
function ReloadPrompt() { | ||
const { t } = useTranslation(); | ||
|
||
const { | ||
needRefresh: [needRefresh, setNeedRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ | ||
onRegisteredSW(swUrl) { | ||
console.log(`SW Registered: ${swUrl} (Version: ${import.meta.env.VITE_APP_VERSION})`); | ||
}, | ||
onRegisterError(error) { | ||
console.log('SW registration error', error); | ||
}, | ||
onOfflineReady() { | ||
toast.success(t('pwa.offlineReady')); | ||
}, | ||
}); | ||
|
||
const close = () => { | ||
setNeedRefresh(false); | ||
}; | ||
|
||
const update = () => { | ||
updateServiceWorker(true); | ||
}; | ||
|
||
if (!needRefresh) { | ||
return null; | ||
} | ||
|
||
|
||
|
||
toast.message( | ||
`${t('pwa.newContent')} (${import.meta.env.VITE_APP_VERSION})`, | ||
{ | ||
action: { | ||
label: t('pwa.reload'), | ||
onClick: () => update(), | ||
}, | ||
onDismiss: close, | ||
duration: Infinity, | ||
} | ||
); | ||
|
||
return null; | ||
} | ||
|
||
export default ReloadPrompt; |
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 |
---|---|---|
@@ -1 +1,22 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare module 'virtual:pwa-register/react' { | ||
import type { Dispatch, SetStateAction } from 'react' | ||
|
||
export interface RegisterSWOptions { | ||
immediate?: boolean | ||
onNeedRefresh?: () => void | ||
onOfflineReady?: () => void | ||
onRegistered?: (registration: ServiceWorkerRegistration | undefined) => void | ||
onRegisteredSW?: (swScriptUrl: string, registration: ServiceWorkerRegistration | undefined) => void | ||
onRegisterError?: (error) => void | ||
} | ||
|
||
export interface RegisterSWHook { | ||
needRefresh: [boolean, Dispatch<SetStateAction<boolean>>] | ||
offlineReady: [boolean, Dispatch<SetStateAction<boolean>>] | ||
updateServiceWorker: (reloadPage?: boolean) => Promise<void> | ||
} | ||
|
||
export function useRegisterSW(options?: RegisterSWOptions): RegisterSWHook | ||
} |
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