2023-07-07 19:45:08 +00:00
|
|
|
import { Button, Notification } from "@mantine/core";
|
|
|
|
import { useCallback } from "react";
|
2023-07-09 22:42:06 +00:00
|
|
|
import { useRegisterSW } from "virtual:pwa-register/react";
|
2023-07-07 19:45:08 +00:00
|
|
|
|
|
|
|
export function InstallUpdateNotification() {
|
2023-07-09 22:42:06 +00:00
|
|
|
const {
|
|
|
|
offlineReady: [_, setOfflineReady],
|
|
|
|
needRefresh: [needRefresh, setNeedRefresh],
|
|
|
|
updateServiceWorker,
|
|
|
|
} = useRegisterSW({
|
|
|
|
onRegistered(r) {
|
|
|
|
console.log("SW Registered:", r);
|
|
|
|
},
|
|
|
|
onRegisterError(error) {
|
|
|
|
console.log("SW registration error", error);
|
|
|
|
},
|
|
|
|
});
|
2023-07-07 19:45:08 +00:00
|
|
|
|
2023-07-09 22:42:06 +00:00
|
|
|
const onClose = () => {
|
|
|
|
setOfflineReady(false);
|
|
|
|
setNeedRefresh(false);
|
|
|
|
};
|
2023-07-07 19:45:08 +00:00
|
|
|
|
2023-07-09 22:42:06 +00:00
|
|
|
const onUpdate = useCallback(async () => {
|
|
|
|
updateServiceWorker(true);
|
|
|
|
}, []);
|
2023-07-07 19:45:08 +00:00
|
|
|
|
2023-07-09 22:42:06 +00:00
|
|
|
return needRefresh ? (
|
|
|
|
<Notification title="Update available!" onClose={onClose}>
|
|
|
|
Click{" "}
|
|
|
|
<Button compact onClick={onUpdate}>
|
|
|
|
Update now
|
|
|
|
</Button>{" "}
|
|
|
|
to get the latest version.
|
|
|
|
</Notification>
|
|
|
|
) : null;
|
2023-07-07 19:45:08 +00:00
|
|
|
}
|