Skip to content

Commit

Permalink
show max 10 users in badge award
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Dec 4, 2023
1 parent d5c0f55 commit 22c10b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/views/badges/components/badge-award-card.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRef } from "react";
import { Card, Flex, Image, Link, LinkBox, Text } from "@chakra-ui/react";
import { memo, useRef } from "react";
import { Button, Card, Flex, Image, Link, LinkBox, Text, useDisclosure } from "@chakra-ui/react";
import { Link as RouterLink } from "react-router-dom";

import { getBadgeAwardBadge, getBadgeAwardPubkeys, getBadgeImage, getBadgeName } from "../../../helpers/nostr/badges";
Expand All @@ -12,15 +12,26 @@ import { UserLink } from "../../../components/user-link";
import Timestamp from "../../../components/timestamp";
import UserAvatarLink from "../../../components/user-avatar-link";

const UserCard = memo(({ pubkey }: { pubkey: string }) => (
<Flex gap="2" alignItems="center">
<UserAvatarLink pubkey={pubkey} size="sm" />
<UserLink pubkey={pubkey} fontWeight="bold" isTruncated />
</Flex>
));

export default function BadgeAwardCard({ award, showImage = true }: { award: NostrEvent; showImage?: boolean }) {
const badge = useReplaceableEvent(getBadgeAwardBadge(award));
const showAll = useDisclosure();

// if there is a parent intersection observer, register this card
const ref = useRef<HTMLDivElement | null>(null);
useRegisterIntersectionEntity(ref, getEventUID(award));

if (!badge) return null;

const awards = getBadgeAwardPubkeys(award);
const collapsed = !showAll.isOpen && awards.length > 10;

const naddr = getSharableEventAddress(badge);
return (
<Card as={LinkBox} p="2" variant="outline" gap="2" flexDirection={["column", null, "row"]} ref={ref}>
Expand All @@ -41,12 +52,14 @@ export default function BadgeAwardCard({ award, showImage = true }: { award: Nos
<Timestamp timestamp={award.created_at} ml="auto" />
</Flex>
<Flex gap="4" wrap="wrap">
{getBadgeAwardPubkeys(award).map(({ pubkey }) => (
<Flex key={pubkey} gap="2" alignItems="center">
<UserAvatarLink pubkey={pubkey} size="sm" />
<UserLink pubkey={pubkey} fontWeight="bold" isTruncated />
</Flex>
{(collapsed ? awards.slice(0, 10) : awards).map(({ pubkey }) => (
<UserCard key={pubkey} pubkey={pubkey} />
))}
{collapsed && (
<Button variant="ghost" onClick={showAll.onOpen}>
Show {awards.length - 10} more
</Button>
)}
</Flex>
</Flex>
</Card>
Expand Down
23 changes: 19 additions & 4 deletions src/views/badges/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,29 @@ import IntersectionObserverProvider from "../../providers/intersection-observer"
import { useTimelineCurserIntersectionCallback } from "../../hooks/use-timeline-cursor-intersection-callback";
import BadgeAwardCard from "./components/badge-award-card";
import { ErrorBoundary } from "../../components/error-boundary";
import useClientSideMuteFilter from "../../hooks/use-client-side-mute-filter";
import { useCallback } from "react";

function BadgesPage() {
const { filter, listId } = usePeopleListContext();
const muteFilter = useClientSideMuteFilter();
const eventFilter = useCallback(
(e) => {
if (muteFilter(e)) return false;
return true;
},
[muteFilter],
);
const readRelays = useReadRelayUrls();
const timeline = useTimelineLoader(`${listId}-lists`, readRelays, {
"#p": filter?.authors,
kinds: [Kind.BadgeAward],
});
const timeline = useTimelineLoader(
`${listId}-lists`,
readRelays,
{
"#p": filter?.authors,
kinds: [Kind.BadgeAward],
},
{ eventFilter },
);

const awards = useSubject(timeline.timeline);
const callback = useTimelineCurserIntersectionCallback(timeline);
Expand Down

0 comments on commit 22c10b2

Please sign in to comment.