Files
pgtune/src/composables/useUpdateApp.js
T
2026-09-08 23:28:53 +03:00

56 lines
1.4 KiB
JavaScript

import { useRegisterSW } from 'virtual:pwa-register/vue'
import { watch } from 'vue'
const intervalMS = import.meta.env.DEV ? 5 * 60 * 1000 : 60 * 60 * 1000
export function useUpdateApp() {
const { needRefresh, updateServiceWorker } = useRegisterSW({
onRegistered(r) {
r &&
setInterval(() => {
r.update()
}, intervalMS)
},
})
const toast = useToast()
async function close(e) {
needRefresh.value = false
e?.stopPropagation()
}
watch(
needRefresh,
(n) => {
if (import.meta.env.DEV) console.log('NeedRefresh:', n)
if (n) {
toast.add({
title: 'Обновление',
description:
'Доступна более новая версия приложения. Нажмите кнопку “Обновить”, чтобы установить его.',
duration: 0,
icon: 'lucide:cloud-download',
close: false,
actions: [
{
icon: 'lucide:arrow-big-up-dash',
label: 'Обновить',
onClick: updateServiceWorker,
color: 'error',
},
{
icon: 'lucide:x',
label: 'Отложить',
onClick: close,
variant: 'outline',
},
],
onEscapeKeyDown: close,
})
}
},
{ immediate: true },
)
}