From 11e8b5eb657fb13a29e08bddb2bc4ddcdc85c9f6 Mon Sep 17 00:00:00 2001 From: AmitUp9 <96980485+AmitUp9@users.noreply.github.com> Date: Tue, 24 May 2022 15:07:40 +0300 Subject: [PATCH] TRA_4437- disable line numbers checkbox when only one line in content (#1109) * number of lines state added * added useEffect to update to showLineNubers state dynamically * small cr fixes --- .../EntryDetailed/EntrySections.tsx | 30 +++++++++++------ .../components/UI/SyntaxHighlighter/index.tsx | 32 ++++++++++++------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/ui-common/src/components/TrafficViewer/EntryDetailed/EntrySections.tsx b/ui-common/src/components/TrafficViewer/EntryDetailed/EntrySections.tsx index d43bda4d1..cd2da6dba 100644 --- a/ui-common/src/components/TrafficViewer/EntryDetailed/EntrySections.tsx +++ b/ui-common/src/components/TrafficViewer/EntryDetailed/EntrySections.tsx @@ -1,5 +1,5 @@ import styles from "./EntrySections.module.sass"; -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { SyntaxHighlighter } from "../../UI/SyntaxHighlighter/index"; import CollapsibleContainer from "../../UI/CollapsibleContainer"; import FancyTextDisplay from "../../UI/FancyTextDisplay"; @@ -125,12 +125,18 @@ export const EntryBodySection: React.FC = ({ const supportedFormats = jsonLikeFormats.concat(xmlLikeFormats, protobufFormats); const [isPretty, setIsPretty] = useState(true); - const [showLineNumbers, setShowLineNumbers] = useState(true); + const [showLineNumbers, setShowLineNumbers] = useState(false); const [decodeBase64, setDecodeBase64] = useState(true); const isBase64Encoding = encoding === 'base64'; const supportsPrettying = supportedFormats.some(format => contentType?.indexOf(format) > -1); const [isDecodeGrpc, setIsDecodeGrpc] = useState(true); + const [isLineNumbersGreaterThenOne, setIsLineNumbersGreaterThenOne] = useState(true); + + useEffect(() => { + (isLineNumbersGreaterThenOne && isPretty) && setShowLineNumbers(true); + !isLineNumbersGreaterThenOne && setShowLineNumbers(false); + }, [isLineNumbersGreaterThenOne, isPretty]) const formatTextBody = (body: any): string => { if (!decodeBase64) return body; @@ -158,9 +164,11 @@ export const EntryBodySection: React.FC = ({ return jsonBeautify(protobufDecoded, null, 2, 80); } } catch (error) { - if (String(error).includes("More than one message in")){ - if(isDecodeGrpc) - setIsDecodeGrpc(false); + if (String(error).includes("More than one message in")) { + if (isDecodeGrpc) + setIsDecodeGrpc(false); + } else if (String(error).includes("Failed to parse")) { + console.warn(error); } else { console.error(error); } @@ -181,19 +189,21 @@ export const EntryBodySection: React.FC = ({ {supportsPrettying && Pretty}
- { setShowLineNumbers(!showLineNumbers) }} /> + { setShowLineNumbers(!showLineNumbers) }} disabled={!isLineNumbersGreaterThenOne || !decodeBase64} />
- Line numbers - {isBase64Encoding &&
+ Line numbers + + {isBase64Encoding &&
{ setDecodeBase64(!decodeBase64) }} />
} {isBase64Encoding && Decode Base64} {!isDecodeGrpc && More than one message in protobuf payload is not supported} -
+ - } diff --git a/ui-common/src/components/UI/SyntaxHighlighter/index.tsx b/ui-common/src/components/UI/SyntaxHighlighter/index.tsx index fa1fefff2..d0cc0d13c 100644 --- a/ui-common/src/components/UI/SyntaxHighlighter/index.tsx +++ b/ui-common/src/components/UI/SyntaxHighlighter/index.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import Lowlight from 'react-lowlight' import 'highlight.js/styles/atom-one-light.css' import styles from './index.module.sass'; @@ -27,21 +27,29 @@ interface Props { code: string; showLineNumbers?: boolean; language?: string; + setIsLineNumbersGreaterThenOne?: (flag: boolean) => void; } export const SyntaxHighlighter: React.FC = ({ - code, - showLineNumbers = false, - language = null - }) => { - const markers = showLineNumbers ? code.split("\n").map((item, i) => { - return { - line: i + 1, - className: styles.hljsMarkerLine - } - }) : []; + code, + showLineNumbers = false, + language = null, + setIsLineNumbersGreaterThenOne +}) => { + const [markers, setMarkers] = useState([]) - return
; + useEffect(() => { + const newMarkers = code.split("\n").map((item, i) => { + setIsLineNumbersGreaterThenOne(i > 1 ? true : false); + return { + line: i + 1, + className: styles.hljsMarkerLine + } + }); + setMarkers(showLineNumbers ? newMarkers : []); + }, [showLineNumbers, code]) + + return
; }; export default SyntaxHighlighter;