elk/components/account/AccountFollowButton.vue

117 lines
3.8 KiB
Vue
Raw Normal View History

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