2023-03-09 10:13:10 +00:00
|
|
|
import { useMemo } from "react";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { EventKind, HexKey, Lists } from "@snort/nostr";
|
|
|
|
|
2023-03-09 10:13:10 +00:00
|
|
|
import { unwrap, findTag, chunks } from "Util";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { RequestBuilder } from "System";
|
|
|
|
import { FlatNoteStore, ReplaceableNoteStore } from "System/NoteCollection";
|
|
|
|
import useRequestBuilder from "Hooks/useRequestBuilder";
|
2023-03-09 10:13:10 +00:00
|
|
|
|
|
|
|
type BadgeAwards = {
|
|
|
|
pubkeys: string[];
|
|
|
|
ds: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function useProfileBadges(pubkey?: HexKey) {
|
|
|
|
const sub = useMemo(() => {
|
|
|
|
if (!pubkey) return null;
|
2023-03-28 14:34:01 +00:00
|
|
|
const b = new RequestBuilder(`badges:${pubkey.slice(0, 12)}`);
|
|
|
|
b.withFilter().kinds([EventKind.ProfileBadges]).tag("d", [Lists.Badges]).authors([pubkey]);
|
|
|
|
return b;
|
2023-03-09 10:13:10 +00:00
|
|
|
}, [pubkey]);
|
2023-03-28 14:34:01 +00:00
|
|
|
|
|
|
|
const profileBadges = useRequestBuilder<ReplaceableNoteStore>(ReplaceableNoteStore, sub);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
|
|
|
const profile = useMemo(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
if (profileBadges.data) {
|
2023-03-09 10:13:10 +00:00
|
|
|
return chunks(
|
2023-03-28 14:34:01 +00:00
|
|
|
profileBadges.data.tags.filter(t => t[0] === "a" || t[0] === "e"),
|
2023-03-09 10:13:10 +00:00
|
|
|
2
|
|
|
|
).reduce((acc, [a, e]) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[e[1]]: a[1],
|
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
return {};
|
2023-03-28 14:34:01 +00:00
|
|
|
}, [profileBadges]);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
|
|
|
const { ds, pubkeys } = useMemo(() => {
|
|
|
|
return Object.values(profile).reduce(
|
|
|
|
(acc: BadgeAwards, addr) => {
|
|
|
|
const [, pubkey, d] = (addr as string).split(":");
|
|
|
|
acc.pubkeys.push(pubkey);
|
|
|
|
if (d?.length > 0) {
|
|
|
|
acc.ds.push(d);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ pubkeys: [], ds: [] } as BadgeAwards
|
|
|
|
) as BadgeAwards;
|
|
|
|
}, [profile]);
|
|
|
|
|
|
|
|
const awardsSub = useMemo(() => {
|
|
|
|
const ids = Object.keys(profile);
|
|
|
|
if (!pubkey || ids.length === 0) return null;
|
2023-03-28 14:34:01 +00:00
|
|
|
const b = new RequestBuilder(`profile_awards:${pubkey.slice(0, 12)}`);
|
|
|
|
b.withFilter().kinds([EventKind.BadgeAward]).ids(ids);
|
|
|
|
b.withFilter().kinds([EventKind.Badge]).tag("d", ds).authors(pubkeys);
|
|
|
|
return b;
|
|
|
|
}, [profile, ds]);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const awards = useRequestBuilder<FlatNoteStore>(FlatNoteStore, awardsSub);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
|
|
|
const result = useMemo(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
if (awards.data) {
|
|
|
|
return awards.data
|
|
|
|
.map((award, _, arr) => {
|
|
|
|
const [, pubkey, d] =
|
|
|
|
award.tags
|
|
|
|
.find(t => t[0] === "a")
|
|
|
|
?.at(1)
|
|
|
|
?.split(":") ?? [];
|
|
|
|
const badge = arr.find(b => b.pubkey === pubkey && findTag(b, "d") === d);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
return {
|
|
|
|
award,
|
|
|
|
badge,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter(
|
|
|
|
({ award, badge }) =>
|
|
|
|
badge && award.pubkey === badge.pubkey && award.tags.find(t => t[0] === "p" && t[1] === pubkey)
|
|
|
|
)
|
|
|
|
.map(({ badge }) => unwrap(badge));
|
|
|
|
}
|
|
|
|
}, [pubkey, awards]);
|
2023-03-09 10:13:10 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
return result ?? [];
|
2023-03-09 10:13:10 +00:00
|
|
|
}
|