Skip to content

Commit

Permalink
fetch missing posts in channel if we found chunk with next_post_id
Browse files Browse the repository at this point in the history
Changelog: fixed
  • Loading branch information
Boruus committed Nov 27, 2024
1 parent feafc16 commit fce43a0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion webapp/channels/src/components/post_view/post_list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {Dispatch} from 'redux';

import {markChannelAsRead} from 'mattermost-redux/actions/channels';
import {RequestStatus} from 'mattermost-redux/constants';
import {getRecentPostsChunkInChannel, makeGetPostsChunkAroundPost, getUnreadPostsChunk, getPost, isPostsChunkIncludingUnreadsPosts, getLimitedViews} from 'mattermost-redux/selectors/entities/posts';
import {getRecentPostsChunkInChannel, makeGetPostsChunkAroundPost, getUnreadPostsChunk, getPost, isPostsChunkIncludingUnreadsPosts, getLimitedViews, getNotRecentPostsChunkInChannel} from 'mattermost-redux/selectors/entities/posts';
import {memoizeResult} from 'mattermost-redux/utils/helpers';
import {makePreparePostIdsForPostList} from 'mattermost-redux/utils/post_list';

Expand Down Expand Up @@ -50,6 +50,7 @@ function makeMapStateToProps() {
let latestPostTimeStamp = 0;
let postIds: string[] | undefined;
let chunk;
let notRecentPostsChunk;
let atLatestPost = false;
let atOldestPost = false;
let formattedPostIds: string[] | undefined;
Expand All @@ -68,6 +69,7 @@ function makeMapStateToProps() {
chunk = getUnreadPostsChunk(state, channelId, unreadChunkTimeStamp);
} else {
chunk = getRecentPostsChunkInChannel(state, channelId);
notRecentPostsChunk = getNotRecentPostsChunkInChannel(state, channelId);
}

if (chunk) {
Expand All @@ -76,6 +78,13 @@ function makeMapStateToProps() {
atOldestPost = Boolean(chunk.oldest);
}

let latestPostNoChunkId;

if (notRecentPostsChunk) {
const postIdsNoChunk = notRecentPostsChunk?.order;
latestPostNoChunkId = memoizedGetLatestPostId(postIdsNoChunk);
}

let shouldHideNewMessageIndicator = false;
if (unreadChunkTimeStamp != null) {
shouldHideNewMessageIndicator = shouldStartFromBottomWhenUnread && !isPostsChunkIncludingUnreadsPosts(state, chunk!, unreadChunkTimeStamp);
Expand All @@ -93,6 +102,7 @@ function makeMapStateToProps() {
}

return {
latestPostNoChunkId,
lastViewedAt,
isFirstLoad: isFirstLoad(state, channelId),
formattedPostIds,
Expand Down Expand Up @@ -124,3 +134,4 @@ function mapDispatchToProps(dispatch: Dispatch) {
}

export default connect(makeMapStateToProps, mapDispatchToProps)(PostList);

11 changes: 11 additions & 0 deletions webapp/channels/src/components/post_view/post_list/post_list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export interface Props {

lastViewedAt: number;

latestPostNoChunkId?: string;

toggleShouldStartFromBottomWhenUnread: () => void;
shouldStartFromBottomWhenUnread: boolean;
hasInaccessiblePosts: boolean;
Expand Down Expand Up @@ -204,6 +206,9 @@ export default class PostList extends React.PureComponent<Props, State> {
if (this.props.channelId !== prevProps.channelId) {
this.postsOnLoad(this.props.channelId);
}
if (this.props.latestPostNoChunkId !== prevProps.latestPostNoChunkId && this.props.latestPostNoChunkId) {
this.loadMissingPosts();
}
if (this.props.postListIds != null && prevProps.postListIds == null) {
markAndMeasureChannelSwitchEnd(true);
}
Expand All @@ -213,6 +218,12 @@ export default class PostList extends React.PureComponent<Props, State> {
this.mounted = false;
}

loadMissingPosts = async () => {
if (this.props.latestPostNoChunkId) {
await this.props.actions.loadPostsAround(this.props.channelId, this.props.latestPostNoChunkId);
}
};

postsOnLoad = async (channelId: string) => {
const {focusedPostId, isFirstLoad, latestPostTimeStamp, isPrefetchingInProcess, actions} = this.props;
if (focusedPostId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ export function getRecentPostsChunkInChannel(state: GlobalState, channelId: Chan
return postsForChannel.find((block) => block.recent);
}

export function getNotRecentPostsChunkInChannel(state: GlobalState, channelId: Channel['id']): PostOrderBlock | null | undefined {
const postsForChannel = state.entities.posts.postsInChannel[channelId];

if (!postsForChannel) {
return null;
}

return postsForChannel.find((block) => !block.recent);
}

export function getOldestPostsChunkInChannel(state: GlobalState, channelId: Channel['id']): PostOrderBlock | null | undefined {
const postsForChannel = state.entities.posts.postsInChannel[channelId];

Expand Down

0 comments on commit fce43a0

Please sign in to comment.