2022-12-18 08:29:16 +09:00
|
|
|
<script setup lang="ts">
|
2023-01-05 17:47:58 +09:00
|
|
|
defineProps<{ show?: boolean }>()
|
2022-12-18 08:29:16 +09:00
|
|
|
|
|
|
|
const {
|
|
|
|
pushNotificationData,
|
|
|
|
saveEnabled,
|
|
|
|
undoChanges,
|
|
|
|
hiddenNotification,
|
|
|
|
isSubscribed,
|
|
|
|
isSupported,
|
|
|
|
notificationPermission,
|
|
|
|
updateSubscription,
|
|
|
|
subscribe,
|
|
|
|
unsubscribe,
|
|
|
|
} = usePushManager()
|
2023-01-02 04:24:22 +09:00
|
|
|
const { t } = useI18n()
|
2022-12-18 08:29:16 +09:00
|
|
|
|
2023-01-29 23:18:27 +09:00
|
|
|
const pwaEnabled = useAppConfig().pwaEnabled
|
2022-12-18 08:29:16 +09:00
|
|
|
|
2023-01-02 04:24:22 +09:00
|
|
|
let busy = $ref<boolean>(false)
|
|
|
|
let animateSave = $ref<boolean>(false)
|
|
|
|
let animateSubscription = $ref<boolean>(false)
|
|
|
|
let animateRemoveSubscription = $ref<boolean>(false)
|
|
|
|
let subscribeError = $ref<string>('')
|
|
|
|
let showSubscribeError = $ref<boolean>(false)
|
|
|
|
|
2023-03-31 04:01:24 +09:00
|
|
|
function hideNotification() {
|
2022-12-18 08:29:16 +09:00
|
|
|
const key = currentUser.value?.account?.acct
|
|
|
|
if (key)
|
|
|
|
hiddenNotification.value[key] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
const showWarning = $computed(() => {
|
|
|
|
if (!pwaEnabled)
|
|
|
|
return false
|
|
|
|
|
|
|
|
return isSupported
|
|
|
|
&& (!isSubscribed.value || !notificationPermission.value || notificationPermission.value === 'prompt')
|
|
|
|
&& !(hiddenNotification.value[currentUser.value?.account?.acct ?? ''] === true)
|
|
|
|
})
|
|
|
|
|
2023-03-31 04:01:24 +09:00
|
|
|
async function saveSettings() {
|
2022-12-18 08:29:16 +09:00
|
|
|
if (busy)
|
|
|
|
return
|
|
|
|
|
|
|
|
busy = true
|
|
|
|
await nextTick()
|
|
|
|
animateSave = true
|
|
|
|
|
|
|
|
try {
|
2023-03-19 21:12:20 +09:00
|
|
|
await updateSubscription()
|
2023-01-12 14:39:22 +09:00
|
|
|
}
|
|
|
|
catch (err) {
|
2022-12-18 08:29:16 +09:00
|
|
|
// todo: handle error
|
2023-01-12 14:39:22 +09:00
|
|
|
console.error(err)
|
2022-12-18 08:29:16 +09:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
busy = false
|
|
|
|
animateSave = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 04:01:24 +09:00
|
|
|
async function doSubscribe() {
|
2022-12-18 08:29:16 +09:00
|
|
|
if (busy)
|
|
|
|
return
|
|
|
|
|
|
|
|
busy = true
|
|
|
|
await nextTick()
|
|
|
|
animateSubscription = true
|
|
|
|
|
|
|
|
try {
|
2023-01-02 04:24:22 +09:00
|
|
|
const result = await subscribe()
|
|
|
|
if (result !== 'subscribed') {
|
2023-01-05 17:47:58 +09:00
|
|
|
subscribeError = t(`settings.notifications.push_notifications.subscription_error.${result === 'notification-denied' ? 'permission_denied' : 'request_error'}`)
|
2023-01-02 04:24:22 +09:00
|
|
|
showSubscribeError = true
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 14:39:22 +09:00
|
|
|
catch (err) {
|
2023-01-13 21:54:30 +09:00
|
|
|
if (err instanceof PushSubscriptionError) {
|
|
|
|
subscribeError = t(`settings.notifications.push_notifications.subscription_error.${err.code}`)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error(err)
|
|
|
|
subscribeError = t('settings.notifications.push_notifications.subscription_error.request_error')
|
|
|
|
}
|
2023-01-02 04:24:22 +09:00
|
|
|
showSubscribeError = true
|
2022-12-18 08:29:16 +09:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
busy = false
|
|
|
|
animateSubscription = false
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 04:01:24 +09:00
|
|
|
async function removeSubscription() {
|
2022-12-18 08:29:16 +09:00
|
|
|
if (busy)
|
|
|
|
return
|
|
|
|
|
|
|
|
busy = true
|
|
|
|
await nextTick()
|
|
|
|
animateRemoveSubscription = true
|
|
|
|
try {
|
|
|
|
await unsubscribe()
|
|
|
|
}
|
2023-01-12 14:39:22 +09:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-12-18 08:29:16 +09:00
|
|
|
finally {
|
|
|
|
busy = false
|
|
|
|
animateRemoveSubscription = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onActivated(() => (busy = false))
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-05 17:47:58 +09:00
|
|
|
<section v-if="pwaEnabled && (showWarning || show)" aria-labelledby="pn-s">
|
2022-12-18 08:29:16 +09:00
|
|
|
<Transition name="slide-down">
|
2023-01-05 17:47:58 +09: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 08:29:16 +09:00
|
|
|
<template v-if="isSupported">
|
|
|
|
<div v-if="isSubscribed" flex="~ col">
|
2023-01-05 17:47:58 +09: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 08:29:16 +09:00
|
|
|
<fieldset flex="~ col" gap-y-1 py-1>
|
2023-01-05 17:47:58 +09: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 08:29:16 +09:00
|
|
|
</fieldset>
|
|
|
|
<fieldset flex="~ col" gap-y-1 py-1>
|
2023-01-05 17:47:58 +09: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 08:29:16 +09:00
|
|
|
</fieldset>
|
2023-01-05 17:47:58 +09:00
|
|
|
<div flex="~ col" gap-y-4 gap-x-2 py-1 sm="~ justify-between flex-row">
|
2022-12-18 08:29:16 +09: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-14 01:00:32 +09: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 17:47:58 +09:00
|
|
|
{{ $t('settings.notifications.push_notifications.save_settings') }}
|
2022-12-18 08:29:16 +09: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-14 01:00:32 +09:00
|
|
|
<span aria-hidden="true" class="block i-material-symbols:undo-rounded" />
|
2023-01-05 17:47:58 +09:00
|
|
|
{{ $t('settings.notifications.push_notifications.undo_settings') }}
|
2022-12-18 08:29:16 +09: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-14 01:00:32 +09: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 17:47:58 +09:00
|
|
|
{{ $t('settings.notifications.push_notifications.unsubscribe') }}
|
2022-12-18 08:29:16 +09:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<template v-else>
|
|
|
|
<NotificationEnablePushNotification
|
|
|
|
:animate="animateSubscription"
|
|
|
|
:busy="busy"
|
|
|
|
@hide="hideNotification"
|
|
|
|
@subscribe="doSubscribe"
|
2023-01-02 04:24:22 +09:00
|
|
|
>
|
|
|
|
<template #error>
|
|
|
|
<Transition name="slide-down">
|
|
|
|
<NotificationSubscribePushNotificationError
|
|
|
|
v-model="showSubscribeError"
|
|
|
|
:message="subscribeError"
|
|
|
|
/>
|
|
|
|
</transition>
|
|
|
|
</template>
|
|
|
|
</NotificationEnablePushNotification>
|
2022-12-18 08:29:16 +09:00
|
|
|
</template>
|
|
|
|
</template>
|
2023-01-05 17:47:58 +09: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 08:29:16 +09:00
|
|
|
</div>
|
|
|
|
</Transition>
|
|
|
|
<NotificationEnablePushNotification
|
2023-01-05 17:47:58 +09:00
|
|
|
v-if="showWarning && !show"
|
|
|
|
closeable-header
|
2022-12-18 08:29:16 +09:00
|
|
|
px5
|
|
|
|
py4
|
|
|
|
:animate="animateSubscription"
|
|
|
|
:busy="busy"
|
|
|
|
@hide="hideNotification"
|
|
|
|
@subscribe="doSubscribe"
|
2023-01-02 04:24:22 +09:00
|
|
|
>
|
|
|
|
<template #error>
|
|
|
|
<Transition name="slide-down">
|
|
|
|
<NotificationSubscribePushNotificationError
|
|
|
|
v-model="showSubscribeError"
|
|
|
|
:message="subscribeError"
|
|
|
|
/>
|
|
|
|
</Transition>
|
|
|
|
</template>
|
|
|
|
</NotificationEnablePushNotification>
|
2023-01-05 17:47:58 +09:00
|
|
|
</section>
|
2022-12-18 08:29:16 +09:00
|
|
|
</template>
|