Skip to content

Commit

Permalink
Use FeedRange event for home paging
Browse files Browse the repository at this point in the history
  • Loading branch information
moysa committed Sep 20, 2024
1 parent 5f09fd6 commit fe87e6b
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/contexts/HomeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
NostrEOSE,
NostrEvent,
NostrEventContent,
NostrFeedRange,
NostrMentionContent,
NostrNoteActionsContent,
NostrNoteContent,
Expand All @@ -31,6 +32,7 @@ import { parseBolt11 } from "../utils";
import { useAccountContext } from "./AccountContext";
import { useSettingsContext } from "./SettingsContext";
import { useLocation } from "@solidjs/router";
import { FeedRange } from "../pages/FeedQueryTest";

type HomeContextStore = {
notes: PrimalNote[],
Expand Down Expand Up @@ -391,25 +393,34 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
if (store.isFetching) {
return;
}
const lastNote = store.notes[store.notes.length - 1];

if (!lastNote) {
return;
}

updateStore('lastNote', () => ({ ...lastNote }));

const spec = mainTopic || store.selectedFeed?.spec || '';

const noteData = lastNote.repost ?
lastNote.repost.note :
lastNote.post;

const until = noteData.created_at;
const until = store.page.since || 0;

if (until > 0) {
fetchNotes(spec, `${APP_ID}`, until);
}

// const lastNote = store.notes[store.notes.length - 1];

// if (!lastNote) {
// return;
// }

// updateStore('lastNote', () => ({ ...lastNote }));

// const spec = mainTopic || store.selectedFeed?.spec || '';

// const noteData = lastNote.repost ?
// lastNote.repost.note :
// lastNote.post;

// const until = noteData.created_at;

// if (until > 0) {
// fetchNotes(spec, `${APP_ID}`, until);
// }
};

// const fetchNextPage = () => {
Expand Down Expand Up @@ -489,6 +500,22 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
};

const updatePage = (content: NostrEventContent, scope?: 'future') => {
if (content.kind === Kind.FeedRange) {
const feedRange: FeedRange = JSON.parse(content.content || '{}');

if (scope) {
updateStore(scope, 'page', 'since', () => feedRange.since);
updateStore(scope, 'page', 'until', () => feedRange.until);
updateStore(scope, 'page', 'sortBy', () => feedRange.order_by);
return;
}

updateStore('page', 'since', () => feedRange.since);
updateStore('page', 'until', () => feedRange.until);
updateStore('page', 'sortBy', () => feedRange.order_by);

return;
}
if (content.kind === Kind.Metadata) {
const user = content as NostrUserContent;

Expand Down Expand Up @@ -519,7 +546,7 @@ export const HomeProvider = (props: { children: ContextChildren }) => {

const scopeNotes = store[scope].notes;

const isaAlreadyIn = message.kind === Kind.Text &&
const isAlreadyIn = message.kind === Kind.Text &&
scopeNotes &&
scopeNotes.find(n => n.post.noteId === messageId);

Expand All @@ -529,7 +556,7 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
// store.future.notes[0]?.post?.noteId === messageId :
// store.future.notes[0]?.repost?.note.noteId === messageId;

if (isFirstNote || isaAlreadyIn || isAlreadyReposted) return;
if (isFirstNote || isAlreadyIn || isAlreadyReposted) return;

updateStore(scope, 'page', 'messages',
(msgs) => [ ...msgs, { ...message }]
Expand All @@ -538,13 +565,16 @@ export const HomeProvider = (props: { children: ContextChildren }) => {
return;
}

const isLastNote = message.kind === Kind.Text ?
store.lastNote?.post?.noteId === messageId :
store.lastNote?.repost?.note.noteId === messageId;
const scopeNotes = store.notes;

const isAlreadyIn = message.kind === Kind.Text &&
scopeNotes &&
scopeNotes.find(n => n.post.noteId === messageId);


let isAlreadyReposted = isRepostInCollection(store.page.messages, message);

if (isLastNote || isAlreadyReposted) return;
if (isAlreadyIn || isAlreadyReposted) return;

updateStore('page', 'messages',
(msgs) => [ ...msgs, { ...message }]
Expand Down

0 comments on commit fe87e6b

Please sign in to comment.