Skip to content

Commit

Permalink
improve event counters on recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
chakany committed Mar 24, 2024
1 parent 8aa3f84 commit 61a9c68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
23 changes: 11 additions & 12 deletions src/components/Recipe/TotalComments.svelte
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<script lang="ts">
import { ndk } from '$lib/nostr';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { onMount } from 'svelte';
import CommentIcon from 'phosphor-svelte/lib/ChatTeardropText';
export let event: NDKEvent;
let loading = true;
let totalLikeAmount: number = 0;
let totalCommentAmount: number = 0;
onMount(async () => {
const evs = await $ndk.fetchEvents({
kinds: [1],
'#a': [`${event.kind}:${event.author.hexpubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
});
evs.forEach(() => {
totalLikeAmount = totalLikeAmount + 1;
});
});
$: {
(async () => {
const evs = await $ndk.fetchEvents({
kinds: [1],
'#a': [`${event.kind}:${event.author.hexpubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
});
totalCommentAmount = evs.size // normal way
})()
}
loading = false;
</script>

<a href="#comments" class="flex gap-1.5">
<CommentIcon size={24} />
{#if loading}...{:else}{totalLikeAmount}{/if}
{#if loading}...{:else}{totalCommentAmount}{/if}
</a>
25 changes: 10 additions & 15 deletions src/components/Recipe/TotalLikes.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
<script lang="ts">
import { ndk, userPublickey } from '$lib/nostr';
import type { NDKEvent } from '@nostr-dev-kit/ndk';
import { onMount } from 'svelte';
import HeartIcon from 'phosphor-svelte/lib/Heart';
export let event: NDKEvent;
let loading = true;
let totalLikeAmount: number = 0;
let liked = false;
onMount(async () => {
const evs = await $ndk.fetchEvents({
kinds: [7],
'#a': [`${event.kind}:${event.author.hexpubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
});
evs.forEach((a) => {
if (a.content == '+') {
totalLikeAmount = totalLikeAmount + 1;
if (a.pubkey == $userPublickey) {
liked = true;
}
}
});
});
$: {
(async () => {
const evs = await $ndk.fetchEvents({
kinds: [7],
'#a': [`${event.kind}:${event.author.hexpubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
});
if (Array.from(evs).find((e) => e.pubkey == $userPublickey)) liked = true;
totalLikeAmount = evs.size;
})()
}
loading = false;
async function likePost() {
if (liked) return;
Expand Down
4 changes: 1 addition & 3 deletions src/components/Recipe/TotalZaps.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
let loading = true;
let totalZapAmount: number = 0;
let didSigs = new Map();
let zapped = false;
async function fetch() {
const evs = await $ndk.fetchEvents({
kinds: [9735],
'#a': [`${event.kind}:${event.author.hexpubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
'#a': [`${event.kind}:${event.author.pubkey}:${event.tags.find((e) => e[0] == 'd')?.[1]}`]
});
evs.forEach((a) => {
if (a.pubkey == $userPublickey) zapped = true;
let bolt11 = a.tags.find((e) => e[0] == 'bolt11')?.[1];
if (bolt11 && a.sig) {
if (!didSigs.has(a.sig)) {
Expand Down

0 comments on commit 61a9c68

Please sign in to comment.