mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-07-06 22:29:57 +02:00
Fix reaction counts when user react multiple times
This commit is contained in:
5
.changeset/silly-zoos-approve.md
Normal file
5
.changeset/silly-zoos-approve.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"nostrudel": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix reaction counts when user react multiple times
|
@ -65,7 +65,7 @@ export default function EventReactionButtons({ event, max }: { event: NostrEvent
|
|||||||
key={group.emoji}
|
key={group.emoji}
|
||||||
emoji={group.emoji}
|
emoji={group.emoji}
|
||||||
url={group.url}
|
url={group.url}
|
||||||
count={group.count}
|
count={group.pubkeys.length}
|
||||||
onClick={() => addReaction(group.emoji, group.url)}
|
onClick={() => addReaction(group.emoji, group.url)}
|
||||||
colorScheme={account && group.pubkeys.includes(account?.pubkey) ? "primary" : undefined}
|
colorScheme={account && group.pubkeys.includes(account?.pubkey) ? "primary" : undefined}
|
||||||
/>
|
/>
|
||||||
|
@ -17,7 +17,7 @@ export function groupReactions(reactions: NostrEvent[]) {
|
|||||||
groups[emoji].pubkeys.push(reactionEvent.pubkey);
|
groups[emoji].pubkeys.push(reactionEvent.pubkey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Array.from(Object.values(groups)).sort((a, b) => b.count - a.count);
|
return Array.from(Object.values(groups)).sort((a, b) => b.pubkeys.length - a.pubkeys.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function draftEventReaction(reacted: NostrEvent, emoji = "+", url?: string) {
|
export function draftEventReaction(reacted: NostrEvent, emoji = "+", url?: string) {
|
||||||
|
@ -4,7 +4,7 @@ import { Card, IconButton, Text, useToast } from "@chakra-ui/react";
|
|||||||
import { useCurrentAccount } from "../../../hooks/use-current-account";
|
import { useCurrentAccount } from "../../../hooks/use-current-account";
|
||||||
import useEventReactions from "../../../hooks/use-event-reactions";
|
import useEventReactions from "../../../hooks/use-event-reactions";
|
||||||
import { useSigningContext } from "../../../providers/signing-provider";
|
import { useSigningContext } from "../../../providers/signing-provider";
|
||||||
import { draftEventReaction } from "../../../helpers/nostr/reactions";
|
import { draftEventReaction, groupReactions } from "../../../helpers/nostr/reactions";
|
||||||
import clientRelaysService from "../../../services/client-relays";
|
import clientRelaysService from "../../../services/client-relays";
|
||||||
import { getCommunityRelays } from "../../../helpers/nostr/communities";
|
import { getCommunityRelays } from "../../../helpers/nostr/communities";
|
||||||
import { unique } from "../../../helpers/array";
|
import { unique } from "../../../helpers/array";
|
||||||
@ -18,16 +18,10 @@ export default function PostVoteButtons({ event, community }: { event: NostrEven
|
|||||||
const reactions = useEventReactions(event.id);
|
const reactions = useEventReactions(event.id);
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
|
|
||||||
const voteReactions = useMemo(() => {
|
const grouped = useMemo(() => groupReactions(reactions ?? []), [reactions]);
|
||||||
return reactions?.filter((r) => r.content === "+" || r.content === "-") ?? [];
|
const up = grouped.find((r) => r.emoji === "+");
|
||||||
}, [reactions]);
|
const down = grouped.find((r) => r.emoji === "-");
|
||||||
const vote = useMemo(() => {
|
const vote = (up?.pubkeys.length ?? 0) - (down?.pubkeys.length ?? 0);
|
||||||
return voteReactions.reduce((t, r) => {
|
|
||||||
if (r.content === "+") return t + 1;
|
|
||||||
else if (r.content === "-") return t - 1;
|
|
||||||
return t;
|
|
||||||
}, 0);
|
|
||||||
}, [voteReactions]);
|
|
||||||
|
|
||||||
const myVote = reactions?.find((e) => e.pubkey === account?.pubkey);
|
const myVote = reactions?.find((e) => e.pubkey === account?.pubkey);
|
||||||
|
|
||||||
@ -67,7 +61,7 @@ export default function PostVoteButtons({ event, community }: { event: NostrEven
|
|||||||
isDisabled={!account || !!myVote}
|
isDisabled={!account || !!myVote}
|
||||||
colorScheme={myVote ? "primary" : "gray"}
|
colorScheme={myVote ? "primary" : "gray"}
|
||||||
/>
|
/>
|
||||||
{voteReactions.length > 0 && <Text my="1">{vote}</Text>}
|
{(up || down) && <Text my="1">{vote}</Text>}
|
||||||
<IconButton
|
<IconButton
|
||||||
aria-label="down vote"
|
aria-label="down vote"
|
||||||
title="down vote"
|
title="down vote"
|
||||||
|
Reference in New Issue
Block a user