2022-12-18 02:29:16 +03:00
|
|
|
<script setup lang="ts">
|
2023-01-05 11:47:58 +03:00
|
|
|
defineProps<{ show?: boolean }>()
|
2022-12-18 02:29:16 +03:00
|
|
|
|
|
|
|
const {
|
|
|
|
pushNotificationData,
|
|
|
|
saveEnabled,
|
|
|
|
undoChanges,
|
|
|
|
hiddenNotification,
|
|
|
|
isSubscribed,
|
|
|
|
isSupported,
|
|
|
|
notificationPermission,
|
|
|
|
updateSubscription,
|
|
|
|
subscribe,
|
|
|
|
unsubscribe,
|
|
|
|
} = usePushManager()
|
2023-01-01 22:24:22 +03:00
|
|
|
const { t } = useI18n()
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2023-01-29 17:18:27 +03:00
|
|
|
const pwaEnabled = useAppConfig().pwaEnabled
|
2022-12-18 02:29:16 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const busy = ref<boolean>(false)
|
|
|
|
const animateSave = ref<boolean>(false)
|
|
|
|
const animateSubscription = ref<boolean>(false)
|
|
|
|
const animateRemoveSubscription = ref<boolean>(false)
|
|
|
|
const subscribeError = ref<string>('')
|
|
|
|
const showSubscribeError = ref<boolean>(false)
|
2023-01-01 22:24:22 +03:00
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
function hideNotification() {
|
2022-12-18 02:29:16 +03:00
|
|
|
const key = currentUser.value?.account?.acct
|
|
|
|
if (key)
|
|
|
|
hiddenNotification.value[key] = true
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const showWarning = computed(() => {
|
2022-12-18 02:29:16 +03:00
|
|
|
if (!pwaEnabled)
|
|
|
|
return false
|
|
|
|
|
|
|
|
return isSupported
|
2024-03-05 17:48:58 +03:00
|
|
|
&& (!isSubscribed.value || !notificationPermission.value || notificationPermission.value === 'prompt')
|
|
|
|
&& !(hiddenNotification.value[currentUser.value?.account?.acct ?? ''])
|
2022-12-18 02:29:16 +03:00
|
|
|
})
|
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
async function saveSettings() {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (busy.value)
|
2022-12-18 02:29:16 +03:00
|
|
|
return
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
await nextTick()
|
2024-02-21 18:20:08 +03:00
|
|
|
animateSave.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
|
|
|
|
try {
|
2023-03-19 15:12:20 +03:00
|
|
|
await updateSubscription()
|
2023-01-12 08:39:22 +03:00
|
|
|
}
|
|
|
|
catch (err) {
|
2022-12-18 02:29:16 +03:00
|
|
|
// todo: handle error
|
2023-01-12 08:39:22 +03:00
|
|
|
console.error(err)
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
finally {
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = false
|
|
|
|
animateSave.value = false
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
async function doSubscribe() {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (busy.value)
|
2022-12-18 02:29:16 +03:00
|
|
|
return
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
await nextTick()
|
2024-02-21 18:20:08 +03:00
|
|
|
animateSubscription.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
|
|
|
|
try {
|
2023-01-01 22:24:22 +03:00
|
|
|
const result = await subscribe()
|
|
|
|
if (result !== 'subscribed') {
|
2024-02-21 18:20:08 +03:00
|
|
|
subscribeError.value = t(`settings.notifications.push_notifications.subscription_error.${result === 'notification-denied' ? 'permission_denied' : 'request_error'}`)
|
|
|
|
showSubscribeError.value = true
|
2023-01-01 22:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
2023-01-12 08:39:22 +03:00
|
|
|
catch (err) {
|
2023-01-13 15:54:30 +03:00
|
|
|
if (err instanceof PushSubscriptionError) {
|
2024-02-21 18:20:08 +03:00
|
|
|
subscribeError.value = t(`settings.notifications.push_notifications.subscription_error.${err.code}`)
|
2023-01-13 15:54:30 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error(err)
|
2024-02-21 18:20:08 +03:00
|
|
|
subscribeError.value = t('settings.notifications.push_notifications.subscription_error.request_error')
|
2023-01-13 15:54:30 +03:00
|
|
|
}
|
2024-02-21 18:20:08 +03:00
|
|
|
showSubscribeError.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
finally {
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = false
|
|
|
|
animateSubscription.value = false
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 22:01:24 +03:00
|
|
|
async function removeSubscription() {
|
2024-02-21 18:20:08 +03:00
|
|
|
if (busy.value)
|
2022-12-18 02:29:16 +03:00
|
|
|
return
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
await nextTick()
|
2024-02-21 18:20:08 +03:00
|
|
|
animateRemoveSubscription.value = true
|
2022-12-18 02:29:16 +03:00
|
|
|
try {
|
|
|
|
await unsubscribe()
|
|
|
|
}
|
2023-01-12 08:39:22 +03:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-12-18 02:29:16 +03:00
|
|
|
finally {
|
2024-02-21 18:20:08 +03:00
|
|
|
busy.value = false
|
|
|
|
animateRemoveSubscription.value = false
|
2022-12-18 02:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-21 18:20:08 +03:00
|
|
|
onActivated(() => (busy.value = false))
|
2022-12-18 02:29:16 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-05 11:47:58 +03:00
|
|
|
<section v-if="pwaEnabled && (showWarning || show)" aria-labelledby="pn-s">
|
2022-12-18 02:29:16 +03:00
|
|
|
<Transition name="slide-down">
|
2023-01-05 11:47:58 +03:00
|
|
|
<div v-if="show" flex="~ col" border="b base">
|
|
|
|
<h3 id="pn-settings" px6 py4 mt2 font-bold text-xl flex="~ gap-1" items-center>
|
|
|
|
{{ $t('settings.notifications.push_notifications.label') }}
|
|
|
|
</h3>
|
2022-12-18 02:29:16 +03:00
|
|
|
<template v-if="isSupported">
|
|
|
|
<div v-if="isSubscribed" flex="~ col">
|
2023-01-05 11:47:58 +03:00
|
|
|
<form flex="~ col" gap-y-2 px6 pb4 @submit.prevent="saveSettings">
|
|
|
|
<p id="pn-instructions" text-sm pb2 aria-hidden="true">
|
|
|
|
{{ $t('settings.notifications.push_notifications.instructions') }}
|
|
|
|
</p>
|
2022-12-18 02:29:16 +03:00
|
|
|
<fieldset flex="~ col" gap-y-1 py-1>
|
2023-01-05 11:47:58 +03:00
|
|
|
<legend>{{ $t('settings.notifications.push_notifications.alerts.title') }}</legend>
|
|
|
|
<CommonCheckbox v-model="pushNotificationData.follow" hover :label="$t('settings.notifications.push_notifications.alerts.follow')" />
|
|
|
|
<CommonCheckbox v-model="pushNotificationData.favourite" hover :label="$t('settings.notifications.push_notifications.alerts.favourite')" />
|
|
|
|
<CommonCheckbox v-model="pushNotificationData.reblog" hover :label="$t('settings.notifications.push_notifications.alerts.reblog')" />
|
|
|
|
<CommonCheckbox v-model="pushNotificationData.mention" hover :label="$t('settings.notifications.push_notifications.alerts.mention')" />
|
|
|
|
<CommonCheckbox v-model="pushNotificationData.poll" hover :label="$t('settings.notifications.push_notifications.alerts.poll')" />
|
2022-12-18 02:29:16 +03:00
|
|
|
</fieldset>
|
|
|
|
<fieldset flex="~ col" gap-y-1 py-1>
|
2023-01-05 11:47:58 +03:00
|
|
|
<legend>{{ $t('settings.notifications.push_notifications.policy.title') }}</legend>
|
|
|
|
<CommonRadio v-model="pushNotificationData.policy" hover value="all" :label="$t('settings.notifications.push_notifications.policy.all')" />
|
|
|
|
<CommonRadio v-model="pushNotificationData.policy" hover value="followed" :label="$t('settings.notifications.push_notifications.policy.followed')" />
|
|
|
|
<CommonRadio v-model="pushNotificationData.policy" hover value="follower" :label="$t('settings.notifications.push_notifications.policy.follower')" />
|
|
|
|
<CommonRadio v-model="pushNotificationData.policy" hover value="none" :label="$t('settings.notifications.push_notifications.policy.none')" />
|
2022-12-18 02:29:16 +03:00
|
|
|
</fieldset>
|
2023-01-05 11:47:58 +03:00
|
|
|
<div flex="~ col" gap-y-4 gap-x-2 py-1 sm="~ justify-between flex-row">
|
2022-12-18 02:29:16 +03:00
|
|
|
<button
|
|
|
|
btn-solid font-bold py2 full-w sm-wa flex="~ gap2 center"
|
|
|
|
:class="busy || !saveEnabled ? 'border-transparent' : null"
|
|
|
|
:disabled="busy || !saveEnabled"
|
|
|
|
>
|
2023-01-13 19:00:32 +03:00
|
|
|
<span v-if="busy && animateSave" aria-hidden="true" block animate-spin preserve-3d>
|
|
|
|
<span block i-ri:loader-2-fill aria-hidden="true" />
|
|
|
|
</span>
|
|
|
|
<span v-else block aria-hidden="true" i-ri:save-2-fill />
|
2023-01-05 11:47:58 +03:00
|
|
|
{{ $t('settings.notifications.push_notifications.save_settings') }}
|
2022-12-18 02:29:16 +03:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
btn-outline font-bold py2 full-w sm-wa flex="~ gap2 center"
|
|
|
|
type="button"
|
|
|
|
:class="busy || !saveEnabled ? 'border-transparent' : null"
|
|
|
|
:disabled="busy || !saveEnabled"
|
|
|
|
@click="undoChanges"
|
|
|
|
>
|
2023-01-13 19:00:32 +03:00
|
|
|
<span aria-hidden="true" class="block i-material-symbols:undo-rounded" />
|
2023-01-05 11:47:58 +03:00
|
|
|
{{ $t('settings.notifications.push_notifications.undo_settings') }}
|
2022-12-18 02:29:16 +03:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<form flex="~ col" mt-4 @submit.prevent="removeSubscription">
|
|
|
|
<span border="b base 2px" class="bg-$c-text-secondary" />
|
|
|
|
<button
|
|
|
|
btn-outline rounded-full font-bold py-4 flex="~ gap2 center" m5
|
|
|
|
:class="busy ? 'border-transparent' : null"
|
|
|
|
:disabled="busy"
|
|
|
|
>
|
2023-01-13 19:00:32 +03:00
|
|
|
<span v-if="busy && animateRemoveSubscription" aria-hidden="true" block animate-spin preserve-3d>
|
|
|
|
<span block i-ri:loader-2-fill aria-hidden="true" />
|
|
|
|
</span>
|
|
|
|
<span v-else block aria-hidden="true" i-material-symbols:cancel-rounded />
|
2023-01-05 11:47:58 +03:00
|
|
|
{{ $t('settings.notifications.push_notifications.unsubscribe') }}
|
2022-12-18 02:29:16 +03:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<template v-else>
|
|
|
|
<NotificationEnablePushNotification
|
|
|
|
:animate="animateSubscription"
|
|
|
|
:busy="busy"
|
|
|
|
@hide="hideNotification"
|
|
|
|
@subscribe="doSubscribe"
|
2023-01-01 22:24:22 +03:00
|
|
|
>
|
|
|
|
<template #error>
|
|
|
|
<Transition name="slide-down">
|
|
|
|
<NotificationSubscribePushNotificationError
|
|
|
|
v-model="showSubscribeError"
|
|
|
|
:message="subscribeError"
|
|
|
|
/>
|
|
|
|
</transition>
|
|
|
|
</template>
|
|
|
|
</NotificationEnablePushNotification>
|
2022-12-18 02:29:16 +03:00
|
|
|
</template>
|
|
|
|
</template>
|
2023-01-05 11:47:58 +03:00
|
|
|
<div v-else px6 pb4 role="alert" aria-labelledby="n-unsupported">
|
|
|
|
<p id="n-unsupported">
|
|
|
|
{{ $t('settings.notifications.push_notifications.unsupported') }}
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-12-18 02:29:16 +03:00
|
|
|
</div>
|
|
|
|
</Transition>
|
|
|
|
<NotificationEnablePushNotification
|
2023-01-05 11:47:58 +03:00
|
|
|
v-if="showWarning && !show"
|
|
|
|
closeable-header
|
2022-12-18 02:29:16 +03:00
|
|
|
px5
|
|
|
|
py4
|
|
|
|
:animate="animateSubscription"
|
|
|
|
:busy="busy"
|
|
|
|
@hide="hideNotification"
|
|
|
|
@subscribe="doSubscribe"
|
2023-01-01 22:24:22 +03:00
|
|
|
>
|
|
|
|
<template #error>
|
|
|
|
<Transition name="slide-down">
|
|
|
|
<NotificationSubscribePushNotificationError
|
|
|
|
v-model="showSubscribeError"
|
|
|
|
:message="subscribeError"
|
|
|
|
/>
|
|
|
|
</Transition>
|
|
|
|
</template>
|
|
|
|
</NotificationEnablePushNotification>
|
2023-01-05 11:47:58 +03:00
|
|
|
</section>
|
2022-12-18 02:29:16 +03:00
|
|
|
</template>
|