This commit is contained in:
Martti Malmi 2023-08-08 14:00:22 +03:00
parent 74d65bd8ea
commit 8836de42bd
2 changed files with 37 additions and 19 deletions

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { Event, Filter } from 'nostr-tools';
import PubSub from '@/nostr/PubSub';
import PubSub, { Unsubscribe } from '@/nostr/PubSub';
import SortedEventMap from '@/utils/SortedEventMap';
const useSubscribe = (ops: {
@ -11,28 +11,40 @@ const useSubscribe = (ops: {
enabled?: boolean;
}) => {
const [sortedEvents] = useState(new SortedEventMap());
const [loadMoreUnsubscribe, setLoadMoreUnsubscribe] = useState<Unsubscribe | null>(null);
const { filter, enabled = true, sinceLastOpened = false, mergeSubscriptions = true } = ops;
const [events, setEvents] = useState<Event[]>([]);
// TODO save into SortedMap
const handleEvent = (event: Event) => {
if (sortedEvents.has(event.id)) return;
sortedEvents.add(event);
setEvents(sortedEvents.events());
};
useEffect(() => {
if (!enabled || !filter) return;
filter.limit = filter.limit || 10;
return PubSub.subscribe(
filter,
(event: any) => {
if (sortedEvents.has(event.id)) return;
sortedEvents.add(event);
setEvents(sortedEvents.events());
},
sinceLastOpened,
mergeSubscriptions,
);
return PubSub.subscribe(filter, handleEvent, sinceLastOpened, mergeSubscriptions);
}, [ops]);
const loadMore = () => {
// TODO
};
// Using useCallback to memoize the loadMore function.
const loadMore = useCallback(() => {
const until = sortedEvents.last()?.created_at;
if (!until) return;
// If there's a previous subscription from loadMore, unsubscribe from it
if (loadMoreUnsubscribe) {
loadMoreUnsubscribe();
setLoadMoreUnsubscribe(null); // Reset the stored unsubscribe function
}
const newFilter = Object.assign({}, filter, { until, limit: 50 });
console.log('load more until', new Date(until * 1000), 'filter', newFilter);
// Subscribe with the new filter and store the returned unsubscribe function
const unsubscribe = PubSub.subscribe(newFilter, handleEvent, false, false);
setLoadMoreUnsubscribe(unsubscribe);
}, [filter, loadMoreUnsubscribe, sortedEvents]); // Dependencies for the useCallback
return { events, loadMore };
};

View File

@ -116,14 +116,18 @@ class Profile extends View {
return (
<Feed
key={`posts${this.state.hexPub}`}
filterOptions={[{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [1] } }]}
filterOptions={[
{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [1], limit: 5 } },
]}
/>
);
} else if (this.props.tab === 'likes') {
return (
<Feed
key={`likes${this.state.hexPub}`}
filterOptions={[{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [7] } }]}
filterOptions={[
{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [7], limit: 5 } },
]}
/>
);
} else if (this.props.tab === 'media') {
@ -135,7 +139,9 @@ class Profile extends View {
{this.getNotification()}
<Feed
key={`posts${this.state.hexPub}`}
filterOptions={[{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [1] } }]}
filterOptions={[
{ name: 'likes', filter: { authors: [this.state.hexPub], kinds: [1], limit: 5 } },
]}
/>
</div>
);