Fix MM55258 (#7729)

* Fix MM55258

* Address feedback

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Daniel Espino García 2024-01-15 10:19:33 +01:00 committed by GitHub
parent 8bd9cac9f5
commit 9ea38cfb3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,6 +29,12 @@ const styles = StyleSheet.create({
},
});
function getMaximumLineLength(code: string) {
return code.split('\n').reduce((prev, v) => Math.max(prev, v.length), 0);
}
const MAXIMUM_CODE_LINE_LENGTH = 300;
const Highlighter = ({code, language, textStyle, selectable = false}: SyntaxHiglightProps) => {
const theme = useTheme();
const style = codeTheme[theme.codeTheme] || github;
@ -38,6 +44,8 @@ const Highlighter = ({code, language, textStyle, selectable = false}: SyntaxHigl
{backgroundColor: style.hljs.background || theme.centerChannelBg},
],
[theme, selectable, style]);
const maximumLineLength = useMemo(() => getMaximumLineLength(code), [code]);
const languageToUse = maximumLineLength > MAXIMUM_CODE_LINE_LENGTH ? 'text' : language;
const nativeRenderer = useCallback(({rows, stylesheet}: rendererProps) => {
const digits = rows.length.toString().length;
@ -65,7 +73,7 @@ const Highlighter = ({code, language, textStyle, selectable = false}: SyntaxHigl
return (
<SyntaxHighlighter
style={style}
language={language}
language={languageToUse}
horizontal={true}
showLineNumbers={true}
renderer={nativeRenderer}