-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): support version upgrade prompt (#1754)
* feat(web): support version upgrade prompt * chore
- Loading branch information
Showing
7 changed files
with
103 additions
and
11 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
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,59 @@ | ||
import { useEffect } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { Box, useToast, VStack } from "@chakra-ui/react"; | ||
import { useRegisterSW } from "virtual:pwa-register/react"; | ||
|
||
const UpgradePrompt = () => { | ||
const { | ||
needRefresh: [needRefresh], | ||
updateServiceWorker, | ||
} = useRegisterSW({ | ||
onRegistered(r) { | ||
if (!r) return; | ||
r.update(); | ||
|
||
setInterval(() => { | ||
if (!(!r.installing && navigator)) return; | ||
if ("connection" in navigator && !navigator.onLine) return; | ||
r.update(); | ||
}, 5 * 60 * 1000); // check new version every 5 mins | ||
}, | ||
}); | ||
|
||
const toast = useToast(); | ||
const { t } = useTranslation(); | ||
|
||
useEffect(() => { | ||
if (needRefresh && !toast.isActive("laf-version-upgrade")) { | ||
toast({ | ||
id: "laf-version-upgrade", | ||
position: "bottom-right", | ||
duration: null, | ||
render: () => ( | ||
<div className="flex justify-end"> | ||
<Box | ||
color="white" | ||
w={210} | ||
p={3} | ||
m={3} | ||
bg="primary.500" | ||
borderRadius={10} | ||
shadow="2xl" | ||
className="animate-bounce cursor-pointer select-none hover:scale-105 focus:outline-none active:bg-primary-700" | ||
onClick={() => updateServiceWorker(true)} | ||
> | ||
<VStack> | ||
<p className="text-lg font-bold">🎉 {t("UpgradeVersionTip.Title")}</p> | ||
<p className="tracking-wider">{t("UpgradeVersionTip.Description")}</p> | ||
</VStack> | ||
</Box> | ||
</div> | ||
), | ||
}); | ||
} | ||
}, [needRefresh]); | ||
|
||
return <></>; | ||
}; | ||
|
||
export default UpgradePrompt; |
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