Remove unused post selectors (#27712)

* Remove makeGetPostIdsAroundPost and makeGetPostsAroundPost

* Remove formatPostInChannel and the one place it is unnecessarily used

* Remove makeGetPostsInChannel

* Update comment for getPostsInCurrentChannel
This commit is contained in:
Harrison Healey 2024-08-06 16:03:47 -04:00 committed by GitHub
parent 56c84df916
commit 2e004bb7bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 1158 deletions

View File

@ -14,7 +14,6 @@ import type {GlobalState} from '@mattermost/types/store';
import type {Team} from '@mattermost/types/teams';
import type {UserProfile} from '@mattermost/types/users';
import type {
IDMappedObjects,
RelationOneToOne,
RelationOneToMany,
} from '@mattermost/types/utilities';
@ -113,24 +112,6 @@ export function getPostIdsInCurrentChannel(state: GlobalState): Array<Post['id']
return getPostIdsInChannel(state, state.entities.channels.currentChannelId);
}
export type PostWithFormatData = Post & {
isFirstReply: boolean;
isLastReply: boolean;
previousPostIsComment: boolean;
commentedOnPost?: Post;
consecutivePostByUser: boolean;
replyCount: number;
isCommentMention: boolean;
highlight: boolean;
};
// getPostsInCurrentChannel returns the posts loaded at the bottom of the channel. It does not include older posts
// such as those loaded by viewing a thread or a permalink.
export const getPostsInCurrentChannel: (state: GlobalState) => PostWithFormatData[] | undefined | null = (() => {
const getPostsInChannel = makeGetPostsInChannel();
return (state: GlobalState) => getPostsInChannel(state, state.entities.channels.currentChannelId, -1);
})();
export function makeGetPostIdsForThread(): (state: GlobalState, postId: Post['id']) => Array<Post['id']> {
const getPostsForThread = makeGetPostsForThread();
@ -170,119 +151,6 @@ export function makeGetPostsChunkAroundPost(): (state: GlobalState, postId: Post
);
}
export function makeGetPostIdsAroundPost(): (state: GlobalState, postId: Post['id'], channelId: Channel['id'], a?: {
postsBeforeCount?: number;
postsAfterCount?: number;
}) => Array<Post['id']> | undefined | null {
const getPostsChunkAroundPost = makeGetPostsChunkAroundPost();
return createIdsSelector(
'makeGetPostIdsAroundPost',
(state: GlobalState, postId: string, channelId: string) => getPostsChunkAroundPost(state, postId, channelId),
(state: GlobalState, postId) => postId,
(state: GlobalState, postId, channelId, options) => options && options.postsBeforeCount,
(state: GlobalState, postId, channelId, options) => options && options.postsAfterCount,
(postsChunk, postId, postsBeforeCount = Posts.POST_CHUNK_SIZE / 2, postsAfterCount = Posts.POST_CHUNK_SIZE / 2) => {
if (!postsChunk || !postsChunk.order) {
return null;
}
const postIds = postsChunk.order;
const index = postIds.indexOf(postId);
// Remember that posts that come after the post have a smaller index
const minPostIndex = postsAfterCount === -1 ? 0 : Math.max(index - postsAfterCount, 0);
const maxPostIndex = postsBeforeCount === -1 ? postIds.length : Math.min(index + postsBeforeCount + 1, postIds.length); // Needs the extra 1 to include the focused post
return postIds.slice(minPostIndex, maxPostIndex);
},
);
}
function formatPostInChannel(post: Post, previousPost: Post | undefined | null, index: number, allPosts: IDMappedObjects<Post>, postsInThread: RelationOneToMany<Post, Post>, postIds: Array<Post['id']>, currentUser: UserProfile, focusedPostId: Post['id']): PostWithFormatData {
let isFirstReply = false;
let isLastReply = false;
let highlight = false;
let commentedOnPost: Post| undefined;
if (post.id === focusedPostId) {
highlight = true;
}
if (post.root_id) {
if (previousPost && previousPost.root_id !== post.root_id) {
// Post is the first reply in a list of consecutive replies
isFirstReply = true;
if (previousPost && previousPost.id !== post.root_id) {
commentedOnPost = allPosts[post.root_id];
}
}
if (index - 1 < 0 || allPosts[postIds[index - 1]].root_id !== post.root_id) {
// Post is the last reply in a list of consecutive replies
isLastReply = true;
}
}
let previousPostIsComment = false;
if (previousPost && previousPost.root_id) {
previousPostIsComment = true;
}
const postFromWebhook = Boolean(post.props && post.props.from_webhook);
const prevPostFromWebhook = Boolean(previousPost && previousPost.props && previousPost.props.from_webhook);
let consecutivePostByUser = false;
if (previousPost &&
previousPost.user_id === post.user_id &&
post.create_at - previousPost.create_at <= Posts.POST_COLLAPSE_TIMEOUT &&
!postFromWebhook && !prevPostFromWebhook &&
!isSystemMessage(post) && !isSystemMessage(previousPost)) {
// The last post and this post were made by the same user within some time
consecutivePostByUser = true;
}
let threadRepliedToByCurrentUser = false;
let replyCount = 0;
let isCommentMention = false;
if (currentUser) {
const rootId = post.root_id || post.id;
const threadIds = postsInThread[rootId] || [];
for (const pid of threadIds) {
const p = allPosts[pid];
if (!p) {
continue;
}
if (p.user_id === currentUser.id) {
threadRepliedToByCurrentUser = true;
}
if (!isPostEphemeral(p)) {
replyCount += 1;
}
}
const rootPost = allPosts[rootId];
isCommentMention = isPostCommentMention({post, currentUser, threadRepliedToByCurrentUser, rootPost});
}
return {
...post,
isFirstReply,
isLastReply,
previousPostIsComment,
commentedOnPost,
consecutivePostByUser,
replyCount,
isCommentMention,
highlight,
};
}
function isPostInteractable(post: Post | undefined) {
return post &&
!post.delete_at &&
@ -337,81 +205,34 @@ export function getLatestPostToEdit(state: GlobalState, channelId: string, rootI
export const getLatestReplyablePostId: (state: GlobalState) => Post['id'] = (state) => getLatestInteractablePostId(state, getCurrentChannelId(state));
// makeGetPostsInChannel creates a selector that returns up to the given number of posts loaded at the bottom of the
// given channel. It does not include older posts such as those loaded by viewing a thread or a permalink.
export function makeGetPostsInChannel(): (state: GlobalState, channelId: Channel['id'], numPosts: number) => PostWithFormatData[] | undefined | null {
return createSelector(
'makeGetPostsInChannel',
getAllPosts,
getPostsInThread,
(state: GlobalState, channelId: Channel['id']) => getPostIdsInChannel(state, channelId),
getCurrentUser,
shouldShowJoinLeaveMessages,
(state: GlobalState, channelId: Channel['id'], numPosts: number) => numPosts || Posts.POST_CHUNK_SIZE,
(allPosts, postsInThread, allPostIds, currentUser, showJoinLeave, numPosts) => {
if (!allPostIds) {
return null;
// getPostsInCurrentChannel returns an array of all recent posts loaded at the bottom of the given channel.
// It does not include older posts such as those loaded by viewing a thread or a permalink.
export const getPostsInCurrentChannel: (state: GlobalState) => Post[] | undefined | null = createSelector(
'getPostsInCurrentChannel',
getAllPosts,
getPostIdsInCurrentChannel,
getCurrentUser,
shouldShowJoinLeaveMessages,
(allPosts, postIds, currentUser, showJoinLeave) => {
if (!postIds) {
return null;
}
const posts: Post[] = [];
for (let i = 0; i < postIds.length; i++) {
const post = allPosts[postIds[i]];
if (!post || shouldFilterJoinLeavePost(post, showJoinLeave, currentUser ? currentUser.username : '')) {
continue;
}
const posts: PostWithFormatData[] = [];
posts.push(post);
}
const postIds = numPosts === -1 ? allPostIds : allPostIds.slice(0, numPosts);
for (let i = 0; i < postIds.length; i++) {
const post = allPosts[postIds[i]];
if (!post || shouldFilterJoinLeavePost(post, showJoinLeave, currentUser ? currentUser.username : '')) {
continue;
}
const previousPost = allPosts[postIds[i + 1]] || null;
posts.push(formatPostInChannel(post, previousPost, i, allPosts, postsInThread, postIds, currentUser, ''));
}
return posts;
},
);
}
export function makeGetPostsAroundPost(): (state: GlobalState, postId: Post['id'], channelId: Channel['id']) => PostWithFormatData[] | undefined | null {
const getPostIdsAroundPost = makeGetPostIdsAroundPost();
const options = {
postsBeforeCount: -1, // Where this is used in the web app, view state is used to determine how far back to display
postsAfterCount: Posts.POST_CHUNK_SIZE / 2,
};
return createSelector(
'makeGetPostsAroundPost',
(state: GlobalState, focusedPostId: string, channelId: string) => getPostIdsAroundPost(state, focusedPostId, channelId, options),
getAllPosts,
getPostsInThread,
(state: GlobalState, focusedPostId) => focusedPostId,
getCurrentUser,
shouldShowJoinLeaveMessages,
(postIds, allPosts, postsInThread, focusedPostId, currentUser, showJoinLeave) => {
if (!postIds || !currentUser) {
return null;
}
const posts: PostWithFormatData[] = [];
for (let i = 0; i < postIds.length; i++) {
const post = allPosts[postIds[i]];
if (!post || shouldFilterJoinLeavePost(post, showJoinLeave, currentUser.username)) {
continue;
}
const previousPost = allPosts[postIds[i + 1]] || null;
const formattedPost = formatPostInChannel(post, previousPost, i, allPosts, postsInThread, postIds, currentUser, focusedPostId);
posts.push(formattedPost);
}
return posts;
},
);
}
return posts;
},
);
// Returns a function that creates a creates a selector that will get the posts for a given thread.
// That selector will take a props object (containing a rootId field) as its