diff --git a/commafeed-client/src/app/entries/thunks.ts b/commafeed-client/src/app/entries/thunks.ts index afd094d1..cab7d378 100644 --- a/commafeed-client/src/app/entries/thunks.ts +++ b/commafeed-client/src/app/entries/thunks.ts @@ -230,7 +230,7 @@ export const selectPreviousEntry = createAppAsyncThunk( ) export const selectNextEntry = createAppAsyncThunk( "entries/entry/selectNext", - ( + async ( arg: { expand: boolean markAsRead: boolean @@ -239,12 +239,20 @@ export const selectNextEntry = createAppAsyncThunk( thunkApi ) => { const state = thunkApi.getState() - const { entries } = state.entries + const { entries, hasMore, loading } = state.entries const nextIndex = entries.findIndex(e => e.id === state.entries.selectedEntryId) + 1 - if (nextIndex < entries.length) { + + // load more entries if needed + // this can happen if the last entry is too large to fit on the screen and the infinite loader doesn't trigger + if (nextIndex >= entries.length && hasMore && !loading) { + await thunkApi.dispatch(loadMoreEntries()) + } + + const entriesAfterLoading = thunkApi.getState().entries.entries + if (nextIndex < entriesAfterLoading.length) { thunkApi.dispatch( selectEntry({ - entry: entries[nextIndex], + entry: entriesAfterLoading[nextIndex], expand: arg.expand, markAsRead: arg.markAsRead, scrollToEntry: arg.scrollToEntry, diff --git a/commafeed-client/src/components/content/FeedEntries.tsx b/commafeed-client/src/components/content/FeedEntries.tsx index 20d8ecd4..76bb909b 100644 --- a/commafeed-client/src/components/content/FeedEntries.tsx +++ b/commafeed-client/src/components/content/FeedEntries.tsx @@ -128,36 +128,28 @@ export function FeedEntries() { }, [dispatch, entries, viewMode, scrollMarks, scrollingToEntry]) useMousetrap("r", async () => await dispatch(reloadEntries())) - useMousetrap("j", async () => { - // load more entries if needed - // this can happen if the last entry is too large to fit on the screen and the infinite loader doesn't trigger - if (hasMore && !loading && selectedEntry === entries[entries.length - 1]) { - await dispatch(loadMoreEntries()) - } - - await dispatch( - selectNextEntry({ - expand: true, - markAsRead: true, - scrollToEntry: true, - }) - ) - }) - useMousetrap("n", async () => { - // load more entries if needed - // this can happen if the last entry is too large to fit on the screen and the infinite loader doesn't trigger - if (hasMore && !loading && selectedEntry === entries[entries.length - 1]) { - await dispatch(loadMoreEntries()) - } - - await dispatch( - selectNextEntry({ - expand: false, - markAsRead: false, - scrollToEntry: true, - }) - ) - }) + useMousetrap( + "j", + async () => + await dispatch( + selectNextEntry({ + expand: true, + markAsRead: true, + scrollToEntry: true, + }) + ) + ) + useMousetrap( + "n", + async () => + await dispatch( + selectNextEntry({ + expand: false, + markAsRead: false, + scrollToEntry: true, + }) + ) + ) useMousetrap( "k", async () =>