Widgets
This commit is contained in:
@ -1,30 +1,8 @@
|
||||
import { useMemo } from "react";
|
||||
import {
|
||||
EventKind,
|
||||
NostrEvent,
|
||||
RequestBuilder,
|
||||
NoteCollection,
|
||||
ReplaceableNoteStore,
|
||||
NostrLink,
|
||||
parseZap,
|
||||
} from "@snort/system";
|
||||
import { RequestBuilder, ReplaceableNoteStore, NostrLink } from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { unwrap } from "@snort/shared";
|
||||
import { GOAL } from "const";
|
||||
import { System } from "index";
|
||||
|
||||
export function useZaps(goal: NostrEvent, leaveOpen = false) {
|
||||
const sub = useMemo(() => {
|
||||
const b = new RequestBuilder(`goal-zaps:${goal.id.slice(0, 12)}`);
|
||||
b.withOptions({ leaveOpen });
|
||||
b.withFilter().kinds([EventKind.ZapReceipt]).tag("e", [goal.id]).since(goal.created_at);
|
||||
return b;
|
||||
}, [goal, leaveOpen]);
|
||||
|
||||
const { data } = useRequestBuilder(NoteCollection, sub);
|
||||
|
||||
return data?.map(ev => parseZap(ev, System.ProfileLoader.Cache)).filter(z => z && z.valid) ?? [];
|
||||
}
|
||||
|
||||
export function useZapGoal(host: string, link?: NostrLink, leaveOpen = false) {
|
||||
const sub = useMemo(() => {
|
||||
|
@ -1,10 +1,12 @@
|
||||
import { NostrLink, RequestBuilder, EventKind, NoteCollection } from "@snort/system";
|
||||
import { useRequestBuilder } from "@snort/system-react";
|
||||
import { unixNow } from "@snort/shared";
|
||||
import { useMemo } from "react";
|
||||
import { SnortContext, useRequestBuilder } from "@snort/system-react";
|
||||
import { unixNow, unwrap } from "@snort/shared";
|
||||
import { useContext, useEffect, useMemo } from "react";
|
||||
import { LIVE_STREAM_CHAT, WEEK } from "const";
|
||||
import { findTag } from "utils";
|
||||
|
||||
export function useLiveChatFeed(link: NostrLink, eZaps?: Array<string>) {
|
||||
const system = useContext(SnortContext);
|
||||
const since = useMemo(() => unixNow() - WEEK, [link.id]);
|
||||
const sub = useMemo(() => {
|
||||
const rb = new RequestBuilder(`live:${link.id}:${link.author}`);
|
||||
@ -44,6 +46,12 @@ export function useLiveChatFeed(link: NostrLink, eZaps?: Array<string>) {
|
||||
return rb;
|
||||
}, [etags]);
|
||||
|
||||
useEffect(() => {
|
||||
const pubkeys = [...new Set(zaps.flatMap(a => [a.pubkey, unwrap(findTag(a, "p"))]))];
|
||||
system.ProfileLoader.TrackMetadata(pubkeys);
|
||||
return () => system.ProfileLoader.UntrackMetadata(pubkeys);
|
||||
}, [zaps]);
|
||||
|
||||
const reactionsSub = useRequestBuilder(NoteCollection, esub);
|
||||
|
||||
const reactions = reactionsSub.data ?? [];
|
||||
|
30
src/hooks/stream-link.ts
Normal file
30
src/hooks/stream-link.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { fetchNip05Pubkey, hexToBech32 } from "@snort/shared";
|
||||
import { NostrLink, tryParseNostrLink, NostrPrefix } from "@snort/system";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useParams } from "react-router-dom";
|
||||
|
||||
export function useStreamLink() {
|
||||
const params = useParams();
|
||||
const [link, setLink] = useState<NostrLink>();
|
||||
|
||||
useEffect(() => {
|
||||
if (params.id) {
|
||||
const parsedLink = tryParseNostrLink(params.id);
|
||||
if (parsedLink) {
|
||||
setLink(parsedLink);
|
||||
} else {
|
||||
const [handle, domain] = (params.id.includes("@") ? params.id : `${params.id}@zap.stream`).split("@");
|
||||
fetchNip05Pubkey(handle, domain).then(d => {
|
||||
if (d) {
|
||||
setLink({
|
||||
id: d,
|
||||
type: NostrPrefix.PublicKey,
|
||||
encode: () => hexToBech32(NostrPrefix.PublicKey, d),
|
||||
} as NostrLink);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [params.id]);
|
||||
return link;
|
||||
}
|
40
src/hooks/zaps.ts
Normal file
40
src/hooks/zaps.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { unwrap } from "@snort/shared";
|
||||
import { NostrLink, RequestBuilder, NostrPrefix, EventKind, NoteCollection, parseZap } from "@snort/system";
|
||||
import { SnortContext, useRequestBuilder } from "@snort/system-react";
|
||||
import { System } from "index";
|
||||
import { useContext, useMemo, useEffect } from "react";
|
||||
import { findTag } from "utils";
|
||||
|
||||
export function useZaps(link?: NostrLink, leaveOpen = false) {
|
||||
const system = useContext(SnortContext);
|
||||
const sub = useMemo(() => {
|
||||
if (link) {
|
||||
const b = new RequestBuilder(`zaps:${link.id}`);
|
||||
b.withOptions({ leaveOpen });
|
||||
if (link.type === NostrPrefix.Event || link.type === NostrPrefix.Note) {
|
||||
b.withFilter().kinds([EventKind.ZapReceipt]).tag("e", [link.id]);
|
||||
} else if (link.type === NostrPrefix.Address) {
|
||||
b.withFilter()
|
||||
.kinds([EventKind.ZapReceipt])
|
||||
.tag("a", [`${link.kind}:${link.author}:${link.id}`]);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
return null;
|
||||
}, [link, leaveOpen]);
|
||||
|
||||
const { data: zaps } = useRequestBuilder(NoteCollection, sub);
|
||||
|
||||
useEffect(() => {
|
||||
const pubkeys = zaps ? [...new Set(zaps.flatMap(a => [a.pubkey, unwrap(findTag(a, "p"))]))] : [];
|
||||
system.ProfileLoader.TrackMetadata(pubkeys);
|
||||
return () => system.ProfileLoader.UntrackMetadata(pubkeys);
|
||||
}, [zaps]);
|
||||
|
||||
return (
|
||||
[...(zaps ?? [])]
|
||||
.sort((a, b) => (b.created_at > a.created_at ? 1 : -1))
|
||||
.map(ev => parseZap(ev, System.ProfileLoader.Cache))
|
||||
.filter(z => z && z.valid) ?? []
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user