Files
minstrel/web/src/lib/components/LikeButton.svelte
T

56 lines
1.8 KiB
Svelte

<script lang="ts">
import { Heart } from 'lucide-svelte';
import { useQueryClient } from '@tanstack/svelte-query';
import { createLikedIdsQuery, likeEntity, unlikeEntity, type EntityKind } from '#lib/api/likes.js';
let {
entityType,
entityId,
size = 'md'
}: { entityType: EntityKind; entityId: string; size?: 'sm' | 'md' | 'lg' } = $props();
// Lucide's heart, as on Android (shared/widgets/LikeButton.kt): outlined
// when not liked, the same heart filled in the accent when liked, so the
// toggle reads as a fill change rather than a glyph swap.
const box = $derived(size === 'sm' ? 'h-7 w-7' : size === 'lg' ? 'h-10 w-10' : 'h-8 w-8');
const px = $derived(size === 'sm' ? 16 : size === 'lg' ? 26 : 18);
const SET_KEY = {
track: 'track_ids',
album: 'album_ids',
artist: 'artist_ids'
} as const;
const queryClient = useQueryClient();
const queryStore = $derived(createLikedIdsQuery());
const query = $derived($queryStore);
const liked = $derived.by(() => {
const data = query?.data;
if (!data) return false;
return data[SET_KEY[entityType]].includes(entityId);
});
async function onClick(e: MouseEvent) {
e.stopPropagation();
e.preventDefault();
if (liked) {
await unlikeEntity(queryClient, entityType, entityId);
} else {
await likeEntity(queryClient, entityType, entityId);
}
}
</script>
<button
type="button"
aria-label={liked ? 'Unlike' : 'Like'}
aria-pressed={liked}
onclick={onClick}
class="inline-flex shrink-0 items-center justify-center rounded-full transition-colors {box} {liked
? 'text-accent-fg'
: 'text-text-secondary hover:bg-surface-hover hover:text-text-primary'}"
>
<Heart size={px} strokeWidth={1.75} fill={liked ? 'currentColor' : 'none'} aria-hidden="true" />
</button>