flockstr/app/(app)/explore/_sections/FeaturedLists.tsx

75 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-10-14 12:09:44 -04:00
"use client";
2023-10-16 13:01:46 -04:00
import Link from "next/link";
2023-10-14 12:09:44 -04:00
import {
Section,
SectionHeader,
SectionTitle,
SectionContent,
} from "@/containers/PageSection";
import { Button } from "@/components/ui/button";
import { RiArrowRightLine } from "react-icons/ri";
2023-10-16 13:01:46 -04:00
import useEvents from "@/lib/hooks/useEvents";
import { Event } from "nostr-tools";
import KindLoading from "@/components/KindCard/loading";
import { nip19 } from "nostr-tools";
import { getTagValues, getTagsValues } from "@/lib/nostr/utils";
import { NOTABLE_ACCOUNTS } from "@/constants";
import { type NDKKind } from "@nostr-dev-kit/ndk";
import { uniqBy } from "ramda";
import ListCard from "@/components/ListCard";
2023-10-14 12:09:44 -04:00
export default function FeaturedLists() {
2023-10-16 13:01:46 -04:00
const { events } = useEvents({
filter: {
kinds: [30001 as NDKKind],
authors: NOTABLE_ACCOUNTS.map((a) => nip19.decode(a).data.toString()),
2023-10-18 15:39:43 -04:00
limit: 60,
2023-10-14 12:09:44 -04:00
},
2023-10-16 13:01:46 -04:00
});
2023-10-18 22:11:00 -04:00
const uniq = uniqBy((e) => getTagValues("title", e.tags), events);
2023-10-18 22:09:45 -04:00
const processedEvents = uniqBy((e) => getTagValues("d", e.tags), uniq)
2023-10-18 15:23:38 -04:00
.filter(
(a) =>
!!getTagValues("image", a.tags) ?? !!getTagValues("picture", a.tags),
)
2023-10-16 13:01:46 -04:00
.sort((a, b) => {
const aTitle =
getTagValues("title", a.tags) ??
getTagValues("description", a.tags) ??
a.content;
const bTitle =
getTagValues("title", b.tags) ??
getTagValues("description", b.tags) ??
b.content;
const aTitleLength = aTitle?.split(" ").length ?? 0;
const bTitleLength = bTitle?.split(" ").length ?? 0;
if (aTitleLength && bTitleLength) {
if (aTitleLength < bTitleLength) {
return 1;
} else return -1;
}
if (bTitleLength) return 1;
return -1;
2023-10-18 15:43:46 -04:00
});
2023-10-16 13:01:46 -04:00
2023-10-14 12:09:44 -04:00
return (
<Section>
<SectionHeader>
<div className="center gap-x-2">
<SectionTitle>Featured Lists</SectionTitle>
</div>
<Button variant={"ghost"}>
View all <RiArrowRightLine className="ml-1 h-4 w-4" />
</Button>
</SectionHeader>
<SectionContent className="sm:md-feed-cols relative flex flex-col gap-3">
2023-10-16 13:01:46 -04:00
{processedEvents.map((e) => (
2023-10-18 21:55:45 -04:00
<Link key={e.id} href={`/list/${e.encode()}`}>
2023-10-17 09:06:09 -04:00
<ListCard key={e.id} event={e} />
</Link>
2023-10-14 12:09:44 -04:00
))}
</SectionContent>
</Section>
);
}