-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Account for markdown using debounced comment counter #15501
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,62 @@ | ||
import React from 'react'; | ||
import React, {PureComponent} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {debounce} from 'lodash'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, in the rest of the codebase I see that we use UnderscoreJS's |
||
import CONST from '../CONST'; | ||
import * as ReportUtils from '../libs/ReportUtils'; | ||
import Text from './Text'; | ||
import styles from '../styles/styles'; | ||
|
||
const propTypes = { | ||
/** The current length of the comment */ | ||
commentLength: PropTypes.number.isRequired, | ||
/** Text Comment */ | ||
comment: PropTypes.string.isRequired, | ||
|
||
/** Update UI on parent when comment length is exceeded */ | ||
onExceededMaxCommentLength: PropTypes.func.isRequired, | ||
}; | ||
|
||
const ExceededCommentLength = (props) => { | ||
if (props.commentLength <= CONST.MAX_COMMENT_LENGTH) { | ||
return null; | ||
class ExceededCommentLength extends PureComponent { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
commentLength: 0, | ||
}; | ||
|
||
// By debouncing, we defer the calculation until there is a break in typing | ||
this.updateCommentLength = debounce(this.updateCommentLength.bind(this), CONST.TIMING.COMMENT_LENGTH_DEBOUNCE_TIME); | ||
} | ||
|
||
return ( | ||
<Text style={[styles.textMicro, styles.textDanger, styles.chatItemComposeSecondaryRow, styles.mlAuto, styles.pl2]}> | ||
{`${props.commentLength}/${CONST.MAX_COMMENT_LENGTH}`} | ||
</Text> | ||
); | ||
}; | ||
componentDidMount() { | ||
this.updateCommentLength(); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (prevProps.comment === this.props.comment) { | ||
return; | ||
} | ||
|
||
this.updateCommentLength(); | ||
} | ||
|
||
updateCommentLength() { | ||
const commentLength = ReportUtils.getCommentLength(this.props.comment); | ||
this.setState({commentLength}); | ||
this.props.onExceededMaxCommentLength(commentLength > CONST.MAX_COMMENT_LENGTH); | ||
} | ||
|
||
render() { | ||
if (this.state.commentLength <= CONST.MAX_COMMENT_LENGTH) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Text style={[styles.textMicro, styles.textDanger, styles.chatItemComposeSecondaryRow, styles.mlAuto, styles.pl2]}> | ||
{`${this.state.commentLength}/${CONST.MAX_COMMENT_LENGTH}`} | ||
</Text> | ||
); | ||
} | ||
} | ||
|
||
ExceededCommentLength.propTypes = propTypes; | ||
ExceededCommentLength.displayName = 'ExceededCommentLength'; | ||
|
||
export default ExceededCommentLength; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually I like to include the unit for time constants (e.g. append
_MS
) to make it clear, but we already don't do it for the other constants above, so not blocking on this.