mix some recent events into the for you feed

This commit is contained in:
Martti Malmi 2024-02-03 15:37:39 +02:00
parent 0716fb4752
commit e746109f5c
1 changed files with 48 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { TaggedNostrEvent } from "@snort/system"; import {EventKind, TaggedNostrEvent} from "@snort/system";
import { memo, useEffect, useMemo, useState } from "react"; import { memo, useEffect, useMemo, useState } from "react";
import { FormattedMessage } from "react-intl"; import { FormattedMessage } from "react-intl";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
@ -10,6 +10,7 @@ import { getForYouFeed } from "@/Db/getForYouFeed";
import useLogin from "@/Hooks/useLogin"; import useLogin from "@/Hooks/useLogin";
import messages from "@/Pages/messages"; import messages from "@/Pages/messages";
import { System } from "@/system"; import { System } from "@/system";
import useTimelineFeed, {TimelineFeedOptions, TimelineSubject} from "@/Feed/TimelineFeed";
const FollowsHint = () => { const FollowsHint = () => {
const { publicKey: pubKey, follows } = useLogin(); const { publicKey: pubKey, follows } = useLogin();
@ -44,6 +45,24 @@ export const ForYouTab = memo(function ForYouTab() {
const [displayAs, setDisplayAs] = useState<DisplayAs>(displayAsInitial); const [displayAs, setDisplayAs] = useState<DisplayAs>(displayAsInitial);
const { publicKey } = useLogin(); const { publicKey } = useLogin();
const login = useLogin();
const subject = useMemo(
() =>
({
type: "pubkey",
items: login.follows.item,
discriminator: login.publicKey?.slice(0, 12),
extra: rb => {
if (login.tags.item.length > 0) {
rb.withFilter().kinds([EventKind.TextNote]).tag("t", login.tags.item);
}
},
}) as TimelineSubject,
[login.follows.item, login.tags.item],
);
// also get "follows" feed so data is loaded
const latestFeed = useTimelineFeed(subject, { method: "TIME_RANGE" } as TimelineFeedOptions);
const getFeed = () => { const getFeed = () => {
if (!publicKey) { if (!publicKey) {
return []; return [];
@ -78,10 +97,37 @@ export const ForYouTab = memo(function ForYouTab() {
} }
}, []); }, []);
const combinedFeed = useMemo(() => {
// combine feeds: intermittently pick from both feeds
const seen = new Set<string>();
const combined = [];
let i = 0;
let j = 0;
while (i < notes.length || j < latestFeed.main?.length) {
if (i < notes.length) {
const ev = notes[i];
if (!seen.has(ev.id)) {
seen.add(ev.id);
combined.push(ev);
}
i++;
}
if (j < latestFeed.main?.length) {
const ev = latestFeed.main[j];
if (!seen.has(ev.id) && !ev.tags?.some((tag: string[]) => tag[0] === "e")) {
seen.add(ev.id);
combined.push(ev);
}
j++;
}
}
return combined;
}, [notes, latestFeed.main]);
const frags = useMemo(() => { const frags = useMemo(() => {
return [ return [
{ {
events: notes, events: combinedFeed,
refTime: Date.now(), refTime: Date.now(),
}, },
]; ];