Files
chat-with-gpt/app/src/components/pwa-notifications.tsx
T

38 lines
934 B
TypeScript
Raw Normal View History

2023-07-07 21:45:08 +02:00
import { Button, Notification } from "@mantine/core";
import { useCallback } from "react";
import { useRegisterSW } from "virtual:pwa-register/react";
2023-07-07 21:45:08 +02:00
export function InstallUpdateNotification() {
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 21:45:08 +02:00
const onClose = () => {
setOfflineReady(false);
setNeedRefresh(false);
};
2023-07-07 21:45:08 +02:00
const onUpdate = useCallback(async () => {
updateServiceWorker(true);
}, []);
2023-07-07 21:45:08 +02: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 21:45:08 +02:00
}