elk/components/account/AccountFollowButton.vue

115 lines
4.2 KiB
Vue
Raw Permalink Normal View History

2022-11-22 22:40:20 +00:00
<script setup lang="ts">
2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
2023-06-23 12:24:10 +00:00
import { toggleFollowAccount, useRelationship } from '~~/composables/masto/relationship'
2022-11-22 22:40:20 +00:00
const { account, command, context, ...props } = defineProps<{
2023-01-08 06:21:09 +00:00
account: mastodon.v1.Account
relationship?: mastodon.v1.Relationship
context?: 'followedBy' | 'following'
command?: boolean
2022-11-22 22:40:20 +00:00
}>()
const { t } = useI18n()
const isSelf = useSelfAccount(() => account)
const enable = computed(() => !isSelf.value && currentUser.value)
const relationship = computed(() => props.relationship || useRelationship(account).value)
const isLoading = computed(() => relationship.value === undefined)
2022-11-22 22:40:20 +00:00
const { client } = useMasto()
async function unblock() {
relationship.value!.blocking = false
try {
const newRel = await client.value.v1.accounts.$select(account.id).unblock()
Object.assign(relationship!, newRel)
}
catch (err) {
console.error(err)
// TODO error handling
relationship.value!.blocking = true
}
}
2022-12-19 14:29:12 +00:00
async function unmute() {
relationship.value!.muting = false
2022-12-19 14:29:12 +00:00
try {
const newRel = await client.value.v1.accounts.$select(account.id).unmute()
2022-12-19 14:29:12 +00:00
Object.assign(relationship!, newRel)
}
catch (err) {
console.error(err)
2022-12-19 14:29:12 +00:00
// TODO error handling
relationship.value!.muting = true
2022-12-19 14:29:12 +00:00
}
}
useCommand({
scope: 'Actions',
order: -2,
visible: () => command && enable,
name: () => `${relationship.value?.following ? t('account.unfollow') : t('account.follow')} ${getShortHandle(account)}`,
icon: 'i-ri:star-line',
onActivate: () => toggleFollowAccount(relationship.value!, account),
})
const buttonStyle = computed(() => {
if (relationship.value?.blocking)
return 'text-inverted bg-red border-red'
if (relationship.value?.muting)
2023-01-10 09:05:59 +00:00
return 'text-base bg-card border-base'
2022-12-19 14:29:12 +00:00
// If following, use a label style with a strong border for Mutuals
if (relationship.value ? relationship.value.following : context === 'following')
return `text-base ${relationship.value?.followedBy ? 'border-strong' : 'border-base'}`
// If loading, use a plain style
if (isLoading.value)
return 'text-base border-base'
// If not following, use a button style
return 'text-inverted bg-primary border-primary'
})
2022-11-22 22:40:20 +00:00
</script>
<template>
<button
v-if="enable"
2022-11-29 20:04:23 +00:00
gap-1 items-center group
border-1
rounded-full flex="~ gap2 center" font-500 min-w-30 h-fit px3 py1
:class="buttonStyle"
2022-12-19 14:29:12 +00:00
:hover="!relationship?.blocking && !relationship?.muting && relationship?.following ? 'border-red text-red' : 'bg-base border-primary text-primary'"
2023-06-23 12:24:10 +00:00
@click="relationship?.blocking ? unblock() : relationship?.muting ? unmute() : toggleFollowAccount(relationship!, account)"
>
<template v-if="isLoading">
<span i-svg-spinners-180-ring-with-bg />
2022-11-29 20:04:23 +00:00
</template>
<template v-else>
<template v-if="relationship?.blocking">
<span elk-group-hover="hidden">{{ $t('account.blocking') }}</span>
<span hidden elk-group-hover="inline">{{ $t('account.unblock') }}</span>
</template>
<template v-if="relationship?.muting">
<span elk-group-hover="hidden">{{ $t('account.muting') }}</span>
<span hidden elk-group-hover="inline">{{ $t('account.unmute') }}</span>
</template>
<template v-else-if="relationship ? relationship.following : context === 'following'">
<span elk-group-hover="hidden">{{ relationship?.followedBy ? $t('account.mutuals') : $t('account.following') }}</span>
<span hidden elk-group-hover="inline">{{ $t('account.unfollow') }}</span>
</template>
<template v-else-if="relationship?.requested">
<span elk-group-hover="hidden">{{ $t('account.follow_requested') }}</span>
<span hidden elk-group-hover="inline">{{ $t('account.withdraw_follow_request') }}</span>
</template>
<template v-else-if="relationship ? relationship.followedBy : context === 'followedBy'">
<span elk-group-hover="hidden">{{ $t('account.follows_you') }}</span>
<span hidden elk-group-hover="inline">{{ account.locked ? $t('account.request_follow') : $t('account.follow_back') }}</span>
</template>
<template v-else>
<span>{{ account.locked ? $t('account.request_follow') : $t('account.follow') }}</span>
</template>
2022-11-29 20:04:23 +00:00
</template>
2022-11-22 22:40:20 +00:00
</button>
</template>