Skip to content

Commit d454327

Browse files
authored
MM-69280: Filter undefined blocks before sorting updated time/by properties (#213)
* MM-69280: Filter undefined blocks before sorting updated time/by properties * Add getLatestUpdatedBlock test for undefined content and comment
1 parent fcf6aa4 commit d454327

4 files changed

Lines changed: 58 additions & 20 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
import {createCard} from '../blocks/card'
5+
import {createCommentBlock} from '../blocks/commentBlock'
6+
7+
import {definedBlocksForLatestUpdate, getLatestUpdatedBlock} from './latestUpdatedBlock'
8+
9+
describe('properties/latestUpdatedBlock', () => {
10+
test('excludes undefined content and comment blocks before sorting', () => {
11+
const card = createCard()
12+
card.updateAt = Date.parse('10 Jun 2021 16:22:00')
13+
14+
expect(definedBlocksForLatestUpdate(card, undefined, undefined)).toEqual([card])
15+
})
16+
17+
test('returns the card when content and comment are undefined', () => {
18+
const card = createCard()
19+
card.updateAt = Date.parse('10 Jun 2021 16:22:00')
20+
21+
expect(getLatestUpdatedBlock(card, undefined, undefined)).toBe(card)
22+
})
23+
24+
test('returns the block with the most recent updateAt', () => {
25+
const card = createCard()
26+
card.updateAt = Date.parse('10 Jun 2021 16:22:00')
27+
28+
const comment = createCommentBlock()
29+
comment.updateAt = Date.parse('15 Jun 2021 16:22:00')
30+
31+
expect(getLatestUpdatedBlock(card, undefined, comment)).toBe(comment)
32+
})
33+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
import {Block} from '../blocks/block'
5+
6+
export function definedBlocksForLatestUpdate(card: Block, lastContent?: Block, lastComment?: Block): Block[] {
7+
return [card, lastContent, lastComment].filter((block): block is Block => Boolean(block))
8+
}
9+
10+
export function getLatestUpdatedBlock(card: Block, lastContent?: Block, lastComment?: Block): Block {
11+
const allBlocks = definedBlocksForLatestUpdate(card, lastContent, lastComment)
12+
return allBlocks.sort((a, b) => b.updateAt - a.updateAt)[0] ?? card
13+
}

webapp/src/properties/updatedBy/updatedBy.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44

55
import React from 'react'
66

7-
import {Block} from '../../blocks/block'
87
import {useAppSelector} from '../../store/hooks'
98
import {getLastCardContent} from '../../store/contents'
109
import {getLastCardComment} from '../../store/comments'
10+
import {getLatestUpdatedBlock} from '../latestUpdatedBlock'
1111
import Person from '../person/person'
1212

1313
import {PropertyProps} from '../types'
1414

1515
const LastModifiedBy = (props: PropertyProps): JSX.Element => {
16-
const lastContent = useAppSelector(getLastCardContent(props.card.id || '')) as Block
17-
const lastComment = useAppSelector(getLastCardComment(props.card.id)) as Block
16+
const lastContent = useAppSelector(getLastCardContent(props.card.id || ''))
17+
const lastComment = useAppSelector(getLastCardComment(props.card.id))
1818

19-
let latestBlock: Block = props.card
20-
if (props.board) {
21-
const allBlocks: Block[] = [props.card, lastContent, lastComment]
22-
const sortedBlocks = allBlocks.sort((a, b) => b.updateAt - a.updateAt)
23-
24-
latestBlock = sortedBlocks.length > 0 ? sortedBlocks[0] : latestBlock
25-
}
19+
const latestBlock = props.board ?
20+
getLatestUpdatedBlock(props.card, lastContent, lastComment) :
21+
props.card
2622

2723
return (
2824
<Person

webapp/src/properties/updatedTime/updatedTime.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,23 @@ import React from 'react'
66

77
import {useIntl} from 'react-intl'
88

9-
import {Block} from '../../blocks/block'
109
import {Utils} from '../../utils'
1110
import {useAppSelector} from '../../store/hooks'
1211
import {getLastCardContent} from '../../store/contents'
1312
import {getLastCardComment} from '../../store/comments'
13+
import {getLatestUpdatedBlock} from '../latestUpdatedBlock'
1414
import './updatedTime.scss'
1515

1616
import {PropertyProps} from '../types'
1717

1818
const UpdatedTime = (props: PropertyProps): JSX.Element => {
1919
const intl = useIntl()
20-
const lastContent = useAppSelector(getLastCardContent(props.card.id || '')) as Block
21-
const lastComment = useAppSelector(getLastCardComment(props.card.id)) as Block
20+
const lastContent = useAppSelector(getLastCardContent(props.card.id || ''))
21+
const lastComment = useAppSelector(getLastCardComment(props.card.id))
2222

23-
let latestBlock: Block = props.card
24-
if (props.card) {
25-
const allBlocks = [props.card, lastContent, lastComment]
26-
const sortedBlocks = allBlocks.sort((a, b) => b.updateAt - a.updateAt)
27-
28-
latestBlock = sortedBlocks.length > 0 ? sortedBlocks[0] : latestBlock
29-
}
23+
const latestBlock = props.card ?
24+
getLatestUpdatedBlock(props.card, lastContent, lastComment) :
25+
props.card
3026

3127
return (
3228
<div className={`UpdatedTime ${props.property.valueClassName(true)}`}>

0 commit comments

Comments
 (0)