diff options
author | 2023-03-01 22:41:11 +0100 | |
---|---|---|
committer | 2023-03-01 23:24:17 +0100 | |
commit | 400654b6b1c8a2f39482f7c60b41ec363d882af2 (patch) | |
tree | 47177611f8441fd8bbe642495078b401d438ead9 | |
parent | 3351b70c1a1683fb088b3097f442efc8b36530f1 (diff) | |
download | it-tools-400654b6b1c8a2f39482f7c60b41ec363d882af2.tar.gz it-tools-400654b6b1c8a2f39482f7c60b41ec363d882af2.tar.zst it-tools-400654b6b1c8a2f39482f7c60b41ec363d882af2.zip |
chore(pwa): close update notification on update
-rw-r--r-- | src/components/ReloadPrompt.tsx | 76 |
1 files changed, 48 insertions, 28 deletions
diff --git a/src/components/ReloadPrompt.tsx b/src/components/ReloadPrompt.tsx index 9b36ea8..39ced27 100644 --- a/src/components/ReloadPrompt.tsx +++ b/src/components/ReloadPrompt.tsx @@ -1,32 +1,52 @@ +/* eslint-disable vue/one-component-per-file */ import { useRegisterSW } from 'virtual:pwa-register/vue'; -import { NButton, useNotification } from 'naive-ui'; -import { h, type Component } from 'vue'; +import { useNotification, type NotificationReactive } from 'naive-ui'; +import { defineComponent } from 'vue'; import { whenever } from '@vueuse/core'; -export default function () { - const notification = useNotification(); - - const { needRefresh, updateServiceWorker } = useRegisterSW(); - - whenever( - needRefresh, - () => { - notification.create({ - title: 'A new version is out!', - content: 'Reload the page to refresh the cache and get the newest version of it-tools', - closable: true, - onClose: () => { - needRefresh.value = false; - return true; - }, - action: () => - h( - NButton as Component, - { onClick: updateServiceWorker, type: 'primary', secondary: true }, - { default: () => 'Reload' }, +export default defineComponent({ + setup() { + const notificationBuilder = useNotification(); + + const { needRefresh, offlineReady, updateServiceWorker } = useRegisterSW(); + + let notification: NotificationReactive | null = null; + + const onUpdateClicked = () => { + if (notification) { + notification.action = () => ( + <n-button loading type="primary" secondary> + Reloading + </n-button> + ); + } + + updateServiceWorker(); + }; + + whenever( + needRefresh, + () => { + notification = notificationBuilder.create({ + title: 'A new version is out!', + content: 'Update to get the latest version of it-tools', + closable: true, + onClose: () => { + needRefresh.value = false; + return true; + }, + action: () => ( + <n-button onClick={onUpdateClicked} type="primary" secondary> + Reload + </n-button> ), - }); - }, - { immediate: true }, - ); -} + }); + }, + { immediate: true }, + ); + + whenever(offlineReady, () => notification?.destroy(), { immediate: true }); + + return () => ''; + }, +}); |