feat: avoid follow button flash on hover card (#308)

This commit is contained in:
patak 2022-12-03 06:21:53 +01:00 committed by GitHub
parent 73f1f1d151
commit 3d395a02ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View file

@ -1,23 +1,25 @@
<script setup lang="ts"> <script setup lang="ts">
import type { Account } from 'masto' import type { Account, Relationship } from 'masto'
const { account, command } = defineProps<{ const { account, command, ...props } = defineProps<{
account: Account account: Account
relationship?: Relationship
command?: boolean command?: boolean
}>() }>()
const isSelf = $computed(() => currentUser.value?.account.id === account.id) const isSelf = $computed(() => currentUser.value?.account.id === account.id)
const enable = $computed(() => !isSelf && currentUser.value) const enable = $computed(() => !isSelf && currentUser.value)
let relationship = $(useRelationship(account)) const relationship = $$(props.relationship) ?? useRelationship(account)
async function toggleFollow() { async function toggleFollow() {
relationship!.following = !relationship!.following const rel = relationship.value
rel!.following = !rel!.following
try { try {
relationship = await useMasto().accounts[relationship!.following ? 'follow' : 'unfollow'](account.id) relationship.value = await useMasto().accounts[rel!.following ? 'follow' : 'unfollow'](account.id)
} }
catch { catch {
// TODO error handling // TODO error handling
relationship!.following = !relationship!.following rel!.following = !rel!.following
} }
} }
@ -27,19 +29,21 @@ useCommand({
scope: 'Actions', scope: 'Actions',
order: -2, order: -2,
visible: () => command && enable, visible: () => command && enable,
name: () => `${relationship?.following ? t('account.unfollow') : t('account.follow')} ${getShortHandle(account)}`, name: () => `${relationship.value?.following ? t('account.unfollow') : t('account.follow')} ${getShortHandle(account)}`,
icon: 'i-ri:star-line', icon: 'i-ri:star-line',
onActivate: () => toggleFollow(), onActivate: () => toggleFollow(),
}) })
const buttonStyle = $computed(() => { const buttonStyle = $computed(() => {
const rel = relationship.value
// Skeleton while loading, avoid primary color flash // Skeleton while loading, avoid primary color flash
if (!relationship) if (!rel)
return 'text-inverted' return 'text-inverted'
// If following, use a label style with a strong border for Mutuals // If following, use a label style with a strong border for Mutuals
if (relationship.following) if (rel.following)
return `text-base ${relationship.followedBy ? 'border-strong' : 'border-base'}` return `text-base ${rel.followedBy ? 'border-strong' : 'border-base'}`
// If not following, use a button style // If not following, use a button style
return 'text-inverted bg-primary border-primary' return 'text-inverted bg-primary border-primary'

View file

@ -1,18 +1,20 @@
<script setup lang="ts"> <script setup lang="ts">
import type { Account } from 'masto' import type { Account } from 'masto'
defineProps<{ const { account } = defineProps<{
account: Account account: Account
}>() }>()
const relationship = $(useRelationship(account))
</script> </script>
<template> <template>
<div flex="~ col gap2" rounded min-w-90 max-w-120 z-100 overflow-hidden p-4> <div v-show="relationship" flex="~ col gap2" rounded min-w-90 max-w-120 z-100 overflow-hidden p-4>
<div flex="~ gap2" items-center> <div flex="~ gap2" items-center>
<NuxtLink :to="getAccountRoute(account)" flex-auto rounded-full hover:bg-active transition-100 pr5 mr-a> <NuxtLink :to="getAccountRoute(account)" flex-auto rounded-full hover:bg-active transition-100 pr5 mr-a>
<AccountInfo :account="account" /> <AccountInfo :account="account" />
</NuxtLink> </NuxtLink>
<AccountFollowButton text-sm :account="account" /> <AccountFollowButton text-sm :account="account" :relationship="relationship" />
</div> </div>
<ContentRich text-4 text-secondary :content="account.note" :emojis="account.emojis" /> <ContentRich text-4 text-secondary :content="account.note" :emojis="account.emojis" />
<AccountPostsFollowers text-sm :account="account" /> <AccountPostsFollowers text-sm :account="account" />