add PWA update from change version
This commit is contained in:
55
src/lib/common/hooks/useUpdateCheck.svelte.js
Normal file
55
src/lib/common/hooks/useUpdateCheck.svelte.js
Normal file
@ -0,0 +1,55 @@
|
||||
// lib/common/hooks/useUpdateCheck.svelte.js
|
||||
let needRefresh = $state(false);
|
||||
let updateServiceWorker = () => {};
|
||||
|
||||
try {
|
||||
const { useRegisterSW } = await import('virtual:pwa-register/svelte');
|
||||
|
||||
const sw = useRegisterSW({
|
||||
onRegistered(registration) {
|
||||
console.log('✅ Service Worker зарегистрирован:', registration);
|
||||
|
||||
if (registration) {
|
||||
setInterval(() => {
|
||||
registration.update();
|
||||
}, 30 * 60 * 1000);
|
||||
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
registration.update();
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
onRegisterError(error) {
|
||||
console.error('❌ Ошибка регистрации SW:', error);
|
||||
}
|
||||
});
|
||||
|
||||
if (sw?.needRefresh) {
|
||||
$effect(() => {
|
||||
needRefresh = sw.needRefresh[0];
|
||||
});
|
||||
updateServiceWorker = sw.updateServiceWorker || (() => {});
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('⚠️ PWA update check не инициализирован (возможно, dev-режим):', error);
|
||||
needRefresh = false;
|
||||
}
|
||||
|
||||
async function doUpdate() {
|
||||
try {
|
||||
await updateServiceWorker(true);
|
||||
window.location.reload();
|
||||
} catch (error) {
|
||||
console.error('Ошибка обновления:', error);
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
|
||||
export function useUpdateCheck() {
|
||||
return {
|
||||
get needRefresh() { return needRefresh; },
|
||||
doUpdate
|
||||
};
|
||||
}
|
||||
139
src/lib/common/ui/UpdateBanner.svelte
Normal file
139
src/lib/common/ui/UpdateBanner.svelte
Normal file
@ -0,0 +1,139 @@
|
||||
<!-- lib/common/ui/UpdateBanner.svelte -->
|
||||
<script>
|
||||
let { onUpdate, onDismiss } = $props();
|
||||
let dismissed = $state(false);
|
||||
|
||||
function handleUpdate() {
|
||||
onUpdate?.();
|
||||
}
|
||||
|
||||
function handleDismiss() {
|
||||
dismissed = true;
|
||||
onDismiss?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if !dismissed}
|
||||
<div class="update-banner">
|
||||
<div class="banner-content">
|
||||
<div class="banner-icon">🔄</div>
|
||||
<div class="banner-text">
|
||||
<p class="banner-title">Доступна новая версия</p>
|
||||
<p class="banner-description">Обновите приложение для получения новых функций</p>
|
||||
</div>
|
||||
<div class="banner-actions">
|
||||
<button class="btn-dismiss" onclick={handleDismiss}>Позже</button>
|
||||
<button class="btn-update" onclick={handleUpdate}>Обновить</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.update-banner {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 10000;
|
||||
padding: 12px 16px;
|
||||
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
|
||||
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
|
||||
animation: slide-down 0.3s ease-out;
|
||||
}
|
||||
|
||||
@keyframes slide-down {
|
||||
from {
|
||||
transform: translateY(-100%);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.banner-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.banner-icon {
|
||||
font-size: 24px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.banner-text {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.banner-title {
|
||||
margin: 0 0 4px;
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.banner-description {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.banner-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.btn-dismiss,
|
||||
.btn-update {
|
||||
padding: 8px 16px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn-dismiss {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-dismiss:hover {
|
||||
background: rgba(255, 255, 255, 0.25);
|
||||
}
|
||||
|
||||
.btn-update {
|
||||
background: #fff;
|
||||
color: #6366f1;
|
||||
}
|
||||
|
||||
.btn-update:hover {
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.banner-content {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.banner-text {
|
||||
flex-basis: calc(100% - 48px);
|
||||
}
|
||||
|
||||
.banner-actions {
|
||||
width: 100%;
|
||||
justify-content: flex-end;
|
||||
margin-top: 8px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@ -1,4 +1,4 @@
|
||||
<!-- svelte-ignore a11y_click_1vents_have_key_events a11y_no_static_element_interactions a11y_label_has_associated_control slot_element_deprecated svelte_self_deprecated -->
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions a11y_label_has_associated_control slot_element_deprecated svelte_self_deprecated -->
|
||||
<!-- lib/pages/print/PrintUI.svelte -->
|
||||
<script>
|
||||
import { QUALITIES, COLOR_MODES } from '$lib/common/utils/pricing.util.js';
|
||||
|
||||
Reference in New Issue
Block a user