From 58c55f17b6357f10acacc8b90484b94e48204b0b Mon Sep 17 00:00:00 2001 From: verbiricha Date: Tue, 1 Aug 2023 07:58:12 +0200 Subject: [PATCH] refactor: cache since, dont dedupe by host --- src/hooks/live-chat.tsx | 9 +++++---- src/hooks/live-streams.ts | 34 ++++++++++++++-------------------- src/pages/root.tsx | 27 +++++++++++++-------------- src/utils.ts | 19 ------------------- 4 files changed, 32 insertions(+), 57 deletions(-) diff --git a/src/hooks/live-chat.tsx b/src/hooks/live-chat.tsx index f18a64c..8723648 100644 --- a/src/hooks/live-chat.tsx +++ b/src/hooks/live-chat.tsx @@ -11,9 +11,10 @@ import { useMemo } from "react"; import { LIVE_STREAM_CHAT } from "const"; export function useLiveChatFeed(link: NostrLink, eZaps?: Array) { - const since = useMemo(() => - unixNow() - (60 * 60 * 24 * 7), // 7-days of zaps - [link.id]); + const since = useMemo( + () => unixNow() - 60 * 60 * 24 * 7, // 7-days of zaps + [link.id], + ); const sub = useMemo(() => { const rb = new RequestBuilder(`live:${link.id}:${link.author}`); rb.withOptions({ @@ -57,7 +58,7 @@ export function useLiveChatFeed(link: NostrLink, eZaps?: Array) { const reactionsSub = useRequestBuilder( System, FlatNoteStore, - esub + esub, ); const reactions = reactionsSub.data ?? []; diff --git a/src/hooks/live-streams.ts b/src/hooks/live-streams.ts index aee8303..712cd06 100644 --- a/src/hooks/live-streams.ts +++ b/src/hooks/live-streams.ts @@ -6,26 +6,22 @@ import { useRequestBuilder } from "@snort/system-react"; import { unixNow } from "@snort/shared"; import { LIVE_STREAM } from "const"; import { System, StreamState } from "index"; -import { findTag, dedupeByHost } from "utils"; +import { findTag } from "utils"; export function useStreamsFeed(tag?: string) { + const since = useMemo(() => unixNow() - 86400, [tag]); const rb = useMemo(() => { const rb = new RequestBuilder(tag ? `streams:${tag}` : "streams"); rb.withOptions({ leaveOpen: true, }); if (tag) { - rb.withFilter() - .kinds([LIVE_STREAM]) - .tag("t", [tag]) - .since(unixNow() - 86400); + rb.withFilter().kinds([LIVE_STREAM]).tag("t", [tag]).since(since); } else { - rb.withFilter() - .kinds([LIVE_STREAM]) - .since(unixNow() - 86400); + rb.withFilter().kinds([LIVE_STREAM]).since(since); } return rb; - }, [tag]); + }, [tag, since]); const feed = useRequestBuilder(System, NoteCollection, rb); const feedSorted = useMemo(() => { @@ -45,19 +41,17 @@ export function useStreamsFeed(tag?: string) { return []; }, [feed.data]); - const live = dedupeByHost( - feedSorted.filter((a) => findTag(a, "status") === StreamState.Live), + const live = feedSorted.filter( + (a) => findTag(a, "status") === StreamState.Live, ); - const planned = dedupeByHost( - feedSorted.filter((a) => findTag(a, "status") === StreamState.Planned), - ); - const ended = dedupeByHost( - feedSorted.filter((a) => { - const hasEnded = findTag(a, "status") === StreamState.Ended; - const recording = findTag(a, "recording"); - return hasEnded && recording?.length > 0; - }), + const planned = feedSorted.filter( + (a) => findTag(a, "status") === StreamState.Planned, ); + const ended = feedSorted.filter((a) => { + const hasEnded = findTag(a, "status") === StreamState.Ended; + const recording = findTag(a, "recording"); + return hasEnded && recording?.length > 0; + }); return { live, planned, ended }; } diff --git a/src/pages/root.tsx b/src/pages/root.tsx index 260e8fb..9ed2a91 100644 --- a/src/pages/root.tsx +++ b/src/pages/root.tsx @@ -3,25 +3,26 @@ import type { NostrEvent } from "@snort/system"; import { VideoTile } from "element/video-tile"; import { useLogin } from "hooks/login"; -import { getHost, getTagValues, dedupeByHost } from "utils"; +import { getHost, getTagValues } from "utils"; import { useStreamsFeed } from "hooks/live-streams"; export function RootPage() { const login = useLogin(); const { live, planned, ended } = useStreamsFeed(); - const mutedHosts = getTagValues(login?.muted.tags ?? [], "p"); + const mutedHosts = new Set(getTagValues(login?.muted.tags ?? [], "p")); const followsHost = (ev: NostrEvent) => { return login?.follows.tags?.find((t) => t.at(1) === getHost(ev)); }; const hashtags = getTagValues(login?.follows.tags ?? [], "t"); - const following = dedupeByHost(live.filter(followsHost)); - const liveNow = dedupeByHost(live.filter((e) => !following.includes(e))); + const following = live.filter(followsHost); + const liveNow = live.filter((e) => !following.includes(e)); const hasFollowingLive = following.length > 0; const plannedEvents = planned - .filter((e) => !mutedHosts.includes(getHost(e))) + .filter((e) => !mutedHosts.has(getHost(e))) .filter(followsHost); + const endedEvents = ended.filter((e) => !mutedHosts.has(getHost(e))); return (
@@ -35,7 +36,7 @@ export function RootPage() { {!hasFollowingLive && (
{live - .filter((e) => !mutedHosts.includes(getHost(e))) + .filter((e) => !mutedHosts.has(getHost(e))) .map((e) => ( ))} @@ -46,7 +47,7 @@ export function RootPage() {

#{t}

{live - .filter((e) => !mutedHosts.includes(getHost(e))) + .filter((e) => !mutedHosts.has(getHost(e))) .filter((e) => { const evTags = getTagValues(e.tags, "t"); return evTags.includes(t); @@ -62,7 +63,7 @@ export function RootPage() {

Live

{liveNow - .filter((e) => !mutedHosts.includes(getHost(e))) + .filter((e) => !mutedHosts.has(getHost(e))) .map((e) => ( ))} @@ -79,15 +80,13 @@ export function RootPage() {
)} - {ended.length > 0 && ( + {endedEvents.length > 0 && ( <>

Ended

- {ended - .filter((e) => !mutedHosts.includes(getHost(e))) - .map((e) => ( - - ))} + {endedEvents.map((e) => ( + + ))}
)} diff --git a/src/utils.ts b/src/utils.ts index 6546fcc..6d915b5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -94,22 +94,3 @@ export async function openFile(): Promise { export function getTagValues(tags: Array, tag: string) { return tags.filter((t) => t.at(0) === tag).map((t) => t.at(1)); } - -export function dedupeByHost(events: Array) { - const { result } = events.reduce( - ({ result, seen }, ev) => { - const host = getHost(ev); - if (seen.has(host)) { - return { result, seen }; - } - result.push(ev); - seen.add(host); - return { result, seen }; - }, - { - result: [], - seen: new Set(), - }, - ); - return result; -}