bug: use bounded until

This commit is contained in:
Kieran 2023-01-17 17:46:58 +00:00
parent e7d24b8c7d
commit 077b1d02b2
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 11 additions and 9 deletions

View File

@ -16,7 +16,7 @@ export interface TimelineProps {
* A list of notes by pubkeys
*/
export default function Timeline({ global, pubkeys, postsOnly = false }: TimelineProps) {
const { main, others, loadMore } = useTimelineFeed(pubkeys, global);
const { main, others, loadMore, until } = useTimelineFeed(pubkeys, global);
const mainFeed = useMemo(() => {
return main?.sort((a, b) => b.created_at - a.created_at)?.filter(a => postsOnly ? !a.tags.some(b => b[0] === "e") : true);
@ -37,7 +37,7 @@ export default function Timeline({ global, pubkeys, postsOnly = false }: Timelin
return (
<>
{mainFeed.map(eventElement)}
{mainFeed.length > 0 ? <LoadMore onLoadMore={loadMore} /> : null}
{mainFeed.length > 0 ? <LoadMore key={until} onLoadMore={loadMore} /> : null}
</>
);
}

View File

@ -5,8 +5,7 @@ import { Subscriptions } from "../nostr/Subscriptions";
import useSubscription from "./Subscription";
export default function useTimelineFeed(pubKeys: HexKey | Array<HexKey>, global: boolean = false) {
const TimeRange = 60 * 60; // 1 hr
const [since, setSince] = useState<number>(Math.floor(new Date().getTime() / 1000) - TimeRange);
const [until, setUntil] = useState<number>();
const [trackingEvents, setTrackingEvent] = useState<u256[]>([]);
const subTab = global ? "global" : "follows";
@ -23,11 +22,11 @@ export default function useTimelineFeed(pubKeys: HexKey | Array<HexKey>, global:
sub.Id = `timeline:${subTab}`;
sub.Authors = global ? undefined : new Set(pubKeys);
sub.Kinds = new Set([EventKind.TextNote, EventKind.Repost]);
sub.Since = since;
sub.Until = since + TimeRange;
sub.Limit = 20;
sub.Until = until;
return sub;
}, [pubKeys, global, since]);
}, [pubKeys, global, until]);
const main = useSubscription(sub, { leaveOpen: true });
@ -62,7 +61,10 @@ export default function useTimelineFeed(pubKeys: HexKey | Array<HexKey>, global:
main: main.notes,
others: others.notes,
loadMore: () => {
setSince(s => s - TimeRange);
}
let now = Math.floor(new Date().getTime() / 1000);
let oldest = main.notes.reduce((acc, v) => acc = v.created_at < acc ? v.created_at : acc, now);
setUntil(oldest);
},
until
};
}