Files
hereconnect/apps/frontend/src/components/create/CreateMessageDeliveryOptionWebPush.vue
T

121 lines
4.1 KiB
Vue
Raw Normal View History

2024-11-21 16:11:23 +02:00
<template>
2024-11-25 22:58:10 +02:00
<CreateMessageDeliveryOption :isLoading :label :method v-model="model" />
2024-11-21 16:11:23 +02:00
2024-11-27 11:02:29 +02:00
<!-- Инструкции для iOS -->
<div
v-if="showRequireStandaloneInstructionsForIOS"
class="bg-yellow-100 text-yellow-800 p-4 rounded my-4"
>
<p class="mb-2">Чтобы включить уведомления, добавьте это приложение на главный экран:</p>
<ul class="list-disc pl-5">
<li class="mb-1">
Нажмите
<strong class="font-semibold">
"<img
src="@/assets/icons/apple-share.svg?url"
alt="Поделиться"
class="h-7 -mt-2 inline-block"
/>"
</strong>
внизу экрана
</li>
<li class="mb-1">
Выберите
<strong class="inline-flex items-baseline font-semibold">
<span class="me-1">"На экран «Домой»</span>
<img
src="@/assets/icons/apple-system-plus.svg?url"
alt=""
class="h-4 inline-block self-center"
/>"
</strong>
</li>
<li>Откройте приложение с главного экрана и повторите попытку.</li>
</ul>
</div>
2024-11-21 16:11:23 +02:00
<!-- Ошибка разрешений -->
2024-11-27 11:02:29 +02:00
<p v-else-if="isError && model === method" class="text-red-500 mb-2">
{{ error }}.<br />
<button @click="retry()" class="text-blue-500 underline">Повторить</button>
2024-11-21 16:11:23 +02:00
</p>
</template>
<script setup lang="ts">
import { type QRCodeDocument } from '@/types/DBDocumentTypes'
import { type DeliveryOption, DELIVERY_METHOD_WEB_PUSH } from '@/constants/deliveryOptions'
import { computed } from 'vue'
import { useQuery } from '@tanstack/vue-query'
import { getVapidPublicKey, saveWebPushSubscription } from '@/api/webPush'
2024-11-27 11:02:29 +02:00
// Проверки iOS и запуска из главного экрана
const isIOS = computed(
() => /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window && window.MSStream),
)
const isStandalone = computed(() => 'standalone' in window.navigator && window.navigator.standalone)
2024-11-21 16:11:23 +02:00
2024-11-27 11:02:29 +02:00
// Пропсы
type DeliveryOptionWebPush = Extract<DeliveryOption, { method: typeof DELIVERY_METHOD_WEB_PUSH }>
const props = defineProps<{
2024-11-21 16:11:23 +02:00
label: DeliveryOptionWebPush['label']
method: DeliveryOptionWebPush['method']
}>()
const emit = defineEmits<{
(e: 'change'): void
}>()
const model = defineModel<null | QRCodeDocument['messageDeliveryMethod']>()
// Функция запроса разрешений и подписки
async function requestNotificationPermission() {
2024-11-21 16:11:23 +02:00
if (!('Notification' in window)) {
2024-11-27 11:02:29 +02:00
throw new Error(
isIOS.value && !isStandalone.value
? 'Добавьте приложение на главный экран, чтобы использовать уведомления.'
: 'Уведомления не поддерживаются этим браузером.',
)
2024-11-21 16:11:23 +02:00
}
try {
const applicationServerKeyPromise = getVapidPublicKey()
2024-11-21 16:11:23 +02:00
const permission = await Notification.requestPermission()
if (permission !== 'granted') {
throw new Error('Разрешение на уведомления не предоставлено.')
2024-11-21 16:11:23 +02:00
}
const registration = await navigator.serviceWorker.ready
2024-11-25 22:58:10 +02:00
2024-11-21 16:11:23 +02:00
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: await applicationServerKeyPromise,
2024-11-21 16:11:23 +02:00
})
const webPushSubscriptionDoc = await saveWebPushSubscription(subscription)
emit('change')
return webPushSubscriptionDoc
2024-11-21 16:11:23 +02:00
} catch (err) {
throw new Error('Ошибка при создании Push-подписки: ' + (err as Error).message)
2024-11-21 16:11:23 +02:00
}
}
2024-11-27 11:02:29 +02:00
// Хуки для состояния запроса
const {
isLoading,
isError,
error,
refetch: retry,
} = useQuery({
enabled: computed(() => model.value === DELIVERY_METHOD_WEB_PUSH),
queryKey: ['createPushSubscription'],
queryFn: () => requestNotificationPermission(),
retry: false,
})
2024-11-27 11:02:29 +02:00
const showRequireStandaloneInstructionsForIOS = computed(
() => isError.value && isIOS.value && !isStandalone.value && model.value === props.method,
)
2024-11-21 16:11:23 +02:00
</script>