Skip to content

Commit afbd808

Browse files
authored
MM-65815: Fix search results causing blank page when leaving channel (mattermost#33962)
* getSearchResults: handle posts removed from the store * remove unused posts caching * add unit tests for getSearchResults selector Tests verify that the selector properly filters out posts that don't exist in the posts state when referenced in search results.
1 parent 84a2d2e commit afbd808

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

webapp/channels/src/components/search_results/index.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,15 @@ function makeMapStateToProps() {
3030
let results: Post[];
3131
let fileResults: FileSearchResultItem[];
3232
let files: FileSearchResultItem[] = [];
33-
let posts: Post[];
3433
const addDateSeparatorsForSearchResults = makeAddDateSeparatorsForSearchResults();
3534

3635
return function mapStateToProps(state: GlobalState, ownProps: OwnProps) {
3736
const newResults = getSearchResults(state);
3837

39-
// Cache posts and channels
38+
// Cache results
4039
if (newResults && newResults !== results) {
4140
results = newResults;
4241

43-
posts = [];
44-
results.forEach((post) => {
45-
if (!post) {
46-
return;
47-
}
48-
49-
posts.push(post);
50-
});
51-
5242
if (ownProps.isPinnedPosts) {
5343
results = results.sort((postA: Post | FileSearchResultItem, postB: Post | FileSearchResultItem) => postB.create_at - postA.create_at);
5444
}

webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,3 +1438,80 @@ describe('makeGetProfilesForThread', () => {
14381438
expect(getProfilesForThread(state, '1001')).toEqual([]);
14391439
});
14401440
});
1441+
1442+
describe('getSearchResults', () => {
1443+
it('should return empty array when no search results', () => {
1444+
const state = {
1445+
entities: {
1446+
posts: {
1447+
posts: {},
1448+
},
1449+
search: {
1450+
results: [],
1451+
},
1452+
},
1453+
} as unknown as GlobalState;
1454+
1455+
const results = Selectors.getSearchResults(state);
1456+
expect(results).toEqual([]);
1457+
});
1458+
1459+
it('should return posts for valid search result IDs', () => {
1460+
const post1 = p({id: 'post1', message: 'Hello'});
1461+
const post2 = p({id: 'post2', message: 'World'});
1462+
1463+
const state = {
1464+
entities: {
1465+
posts: {
1466+
posts: {
1467+
post1,
1468+
post2,
1469+
},
1470+
},
1471+
search: {
1472+
results: ['post1', 'post2'],
1473+
},
1474+
},
1475+
} as unknown as GlobalState;
1476+
1477+
const results = Selectors.getSearchResults(state);
1478+
expect(results).toEqual([post1, post2]);
1479+
});
1480+
1481+
it('should filter out non-existent posts from search results', () => {
1482+
const post1 = p({id: 'post1', message: 'Hello'});
1483+
1484+
const state = {
1485+
entities: {
1486+
posts: {
1487+
posts: {
1488+
post1,
1489+
},
1490+
},
1491+
search: {
1492+
results: ['post1', 'non_existent_post', 'another_missing_post'],
1493+
},
1494+
},
1495+
} as unknown as GlobalState;
1496+
1497+
const results = Selectors.getSearchResults(state);
1498+
expect(results).toEqual([post1]);
1499+
expect(results).toHaveLength(1);
1500+
});
1501+
1502+
it('should return empty array when all search result posts are non-existent', () => {
1503+
const state = {
1504+
entities: {
1505+
posts: {
1506+
posts: {},
1507+
},
1508+
search: {
1509+
results: ['non_existent_post1', 'non_existent_post2'],
1510+
},
1511+
},
1512+
} as unknown as GlobalState;
1513+
1514+
const results = Selectors.getSearchResults(state);
1515+
expect(results).toEqual([]);
1516+
});
1517+
});

webapp/channels/src/packages/mattermost-redux/src/selectors/entities/posts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ export const getSearchResults: (state: GlobalState) => Post[] = createSelector(
336336
return [];
337337
}
338338

339-
return postIds.map((id) => posts[id]);
339+
// Filter out posts that may no longer exist
340+
return postIds.map((id) => posts[id]).filter((post) => post);
340341
},
341342
);
342343

0 commit comments

Comments
 (0)