2025-07-01 02:59:08 +01:00
|
|
|
<script setup lang="ts">
|
2025-08-14 21:48:38 +01:00
|
|
|
import { CheckIcon, MailIcon } from '@modrinth/assets'
|
2026-03-16 12:10:29 -07:00
|
|
|
import { ButtonStyled, defineMessages, injectModrinthClient, useVIntl } from '@modrinth/ui'
|
|
|
|
|
import { useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
|
|
|
import { computed, ref } from 'vue'
|
2025-07-01 02:59:08 +01:00
|
|
|
|
2026-03-16 12:10:29 -07:00
|
|
|
const { formatMessage } = useVIntl()
|
2025-08-14 21:48:38 +01:00
|
|
|
|
2026-03-16 12:10:29 -07:00
|
|
|
const messages = defineMessages({
|
|
|
|
|
tooltipSubscribe: {
|
|
|
|
|
id: 'ui.newsletter-button.tooltip',
|
|
|
|
|
defaultMessage: 'Subscribe to the Modrinth newsletter',
|
|
|
|
|
},
|
|
|
|
|
subscribe: {
|
|
|
|
|
id: 'ui.newsletter-button.subscribe',
|
|
|
|
|
defaultMessage: 'Subscribe',
|
|
|
|
|
},
|
|
|
|
|
subscribed: {
|
|
|
|
|
id: 'ui.newsletter-button.subscribed',
|
|
|
|
|
defaultMessage: 'Subscribed!',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const auth = (await useAuth()) as unknown as {
|
|
|
|
|
value: { user: { id: string; username: string; email: string; created: string } }
|
|
|
|
|
}
|
|
|
|
|
const client = injectModrinthClient()
|
|
|
|
|
const queryClient = useQueryClient()
|
2025-08-14 21:48:38 +01:00
|
|
|
const showSubscriptionConfirmation = ref(false)
|
2026-03-16 12:10:29 -07:00
|
|
|
|
|
|
|
|
const { data: showSubscribeButton, isSuccess } = useQuery({
|
|
|
|
|
queryKey: computed(() => ['newsletter', 'subscribed', auth.value?.user?.id]),
|
|
|
|
|
queryFn: async () => {
|
2025-08-14 21:48:38 +01:00
|
|
|
if (auth.value?.user) {
|
|
|
|
|
try {
|
2026-03-16 12:10:29 -07:00
|
|
|
const { subscribed } = await client.labrinth.auth_internal.getNewsletterStatus()
|
2025-08-14 21:48:38 +01:00
|
|
|
return !subscribed
|
|
|
|
|
} catch {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-03-16 12:10:29 -07:00
|
|
|
enabled: computed(() => !!auth.value?.user),
|
|
|
|
|
})
|
2025-07-01 02:59:08 +01:00
|
|
|
|
|
|
|
|
async function subscribe() {
|
2025-08-14 21:48:38 +01:00
|
|
|
try {
|
2026-03-16 12:10:29 -07:00
|
|
|
await client.labrinth.auth_internal.subscribeNewsletter()
|
2025-08-14 21:48:38 +01:00
|
|
|
showSubscriptionConfirmation.value = true
|
|
|
|
|
} catch {
|
|
|
|
|
// Ignored
|
|
|
|
|
} finally {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
showSubscriptionConfirmation.value = false
|
2026-03-16 12:10:29 -07:00
|
|
|
queryClient.setQueryData(['newsletter', 'subscribed', auth.value?.user?.id], false)
|
2025-08-14 21:48:38 +01:00
|
|
|
}, 2500)
|
|
|
|
|
}
|
2025-07-01 02:59:08 +01:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2026-03-16 12:10:29 -07:00
|
|
|
<ButtonStyled v-if="isSuccess && showSubscribeButton" color="brand" type="outlined">
|
|
|
|
|
<button v-tooltip="formatMessage(messages.tooltipSubscribe)" @click="subscribe">
|
|
|
|
|
<template v-if="!showSubscriptionConfirmation">
|
|
|
|
|
<MailIcon /> {{ formatMessage(messages.subscribe) }}
|
|
|
|
|
</template>
|
|
|
|
|
<template v-else> <CheckIcon /> {{ formatMessage(messages.subscribed) }} </template>
|
2025-08-14 21:48:38 +01:00
|
|
|
</button>
|
|
|
|
|
</ButtonStyled>
|
2025-07-01 02:59:08 +01:00
|
|
|
</template>
|