Line numbers counter func added to utils (#1112)

* #run_acceptance_tests

* added new line to the end of the file

* #run_acceptance_tests

* linter fix

* more linter issues fix
This commit is contained in:
AmitUp9 2022-05-25 13:44:50 +03:00 committed by GitHub
parent 11e8b5eb65
commit 569a687fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import styles from "./EntrySections.module.sass"; import styles from "./EntrySections.module.sass";
import React, { useEffect, useState } from "react"; import React, { useCallback, useEffect, useMemo, useState } from "react";
import { SyntaxHighlighter } from "../../UI/SyntaxHighlighter/index"; import { SyntaxHighlighter } from "../../UI/SyntaxHighlighter/index";
import CollapsibleContainer from "../../UI/CollapsibleContainer"; import CollapsibleContainer from "../../UI/CollapsibleContainer";
import FancyTextDisplay from "../../UI/FancyTextDisplay"; import FancyTextDisplay from "../../UI/FancyTextDisplay";
@ -8,6 +8,7 @@ import Checkbox from "../../UI/Checkbox";
import ProtobufDecoder from "protobuf-decoder"; import ProtobufDecoder from "protobuf-decoder";
import { default as jsonBeautify } from "json-beautify"; import { default as jsonBeautify } from "json-beautify";
import { default as xmlBeautify } from "xml-formatter"; import { default as xmlBeautify } from "xml-formatter";
import { Utils } from "../../../helpers/Utils"
interface EntryViewLineProps { interface EntryViewLineProps {
label: string; label: string;
@ -101,6 +102,12 @@ export const EntrySectionContainer: React.FC<EntrySectionContainerProps> = ({ ti
</CollapsibleContainer> </CollapsibleContainer>
} }
const MAXIMUM_BYTES_TO_FORMAT = 1000000; // The maximum of chars to highlight in body, in case the response can be megabytes
const jsonLikeFormats = ['json', 'yaml', 'yml'];
const xmlLikeFormats = ['xml', 'html'];
const protobufFormats = ['application/grpc'];
const supportedFormats = jsonLikeFormats.concat(xmlLikeFormats, protobufFormats);
interface EntryBodySectionProps { interface EntryBodySectionProps {
title: string, title: string,
content: any, content: any,
@ -118,12 +125,6 @@ export const EntryBodySection: React.FC<EntryBodySectionProps> = ({
contentType, contentType,
selector, selector,
}) => { }) => {
const MAXIMUM_BYTES_TO_FORMAT = 1000000; // The maximum of chars to highlight in body, in case the response can be megabytes
const jsonLikeFormats = ['json', 'yaml', 'yml'];
const xmlLikeFormats = ['xml', 'html'];
const protobufFormats = ['application/grpc'];
const supportedFormats = jsonLikeFormats.concat(xmlLikeFormats, protobufFormats);
const [isPretty, setIsPretty] = useState(true); const [isPretty, setIsPretty] = useState(true);
const [showLineNumbers, setShowLineNumbers] = useState(false); const [showLineNumbers, setShowLineNumbers] = useState(false);
const [decodeBase64, setDecodeBase64] = useState(true); const [decodeBase64, setDecodeBase64] = useState(true);
@ -135,10 +136,10 @@ export const EntryBodySection: React.FC<EntryBodySectionProps> = ({
useEffect(() => { useEffect(() => {
(isLineNumbersGreaterThenOne && isPretty) && setShowLineNumbers(true); (isLineNumbersGreaterThenOne && isPretty) && setShowLineNumbers(true);
!isLineNumbersGreaterThenOne && setShowLineNumbers(false); !isLineNumbersGreaterThenOne && setShowLineNumbers(false);
}, [isLineNumbersGreaterThenOne, isPretty]) }, [isLineNumbersGreaterThenOne, isPretty])
const formatTextBody = (body: any): string => { const formatTextBody = useCallback((body: any): string => {
if (!decodeBase64) return body; if (!decodeBase64) return body;
const chunk = body.slice(0, MAXIMUM_BYTES_TO_FORMAT); const chunk = body.slice(0, MAXIMUM_BYTES_TO_FORMAT);
@ -174,7 +175,14 @@ export const EntryBodySection: React.FC<EntryBodySectionProps> = ({
} }
} }
return bodyBuf; return bodyBuf;
} }, [isPretty, contentType, isDecodeGrpc, decodeBase64, isBase64Encoding])
const formattedText = useMemo(() => formatTextBody(content), [formatTextBody, content]);
useEffect(() => {
const lineNumbers = Utils.lineNumbersInString(formattedText);
setIsLineNumbersGreaterThenOne(lineNumbers > 1);
}, [isPretty, content, showLineNumbers, formattedText]);
return <React.Fragment> return <React.Fragment>
{content && content?.length > 0 && <EntrySectionContainer {content && content?.length > 0 && <EntrySectionContainer
@ -201,9 +209,8 @@ export const EntryBodySection: React.FC<EntryBodySectionProps> = ({
</div> </div>
<SyntaxHighlighter <SyntaxHighlighter
code={formatTextBody(content)} code={formattedText}
showLineNumbers={showLineNumbers} showLineNumbers={showLineNumbers}
setIsLineNumbersGreaterThenOne={setIsLineNumbersGreaterThenOne}
/> />
</EntrySectionContainer>} </EntrySectionContainer>}

View File

@ -27,20 +27,17 @@ interface Props {
code: string; code: string;
showLineNumbers?: boolean; showLineNumbers?: boolean;
language?: string; language?: string;
setIsLineNumbersGreaterThenOne?: (flag: boolean) => void;
} }
export const SyntaxHighlighter: React.FC<Props> = ({ export const SyntaxHighlighter: React.FC<Props> = ({
code, code,
showLineNumbers = false, showLineNumbers = false,
language = null, language = null,
setIsLineNumbersGreaterThenOne
}) => { }) => {
const [markers, setMarkers] = useState([]) const [markers, setMarkers] = useState([])
useEffect(() => { useEffect(() => {
const newMarkers = code.split("\n").map((item, i) => { const newMarkers = code.split("\n").map((item, i) => {
setIsLineNumbersGreaterThenOne(i > 1 ? true : false);
return { return {
line: i + 1, line: i + 1,
className: styles.hljsMarkerLine className: styles.hljsMarkerLine

View File

@ -3,4 +3,5 @@ const IP_ADDRESS_REGEX = /([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})(:([0-9]{
export class Utils { export class Utils {
static isIpAddress = (address: string): boolean => IP_ADDRESS_REGEX.test(address) static isIpAddress = (address: string): boolean => IP_ADDRESS_REGEX.test(address)
} static lineNumbersInString = (code:string): number => code.split("\n").length;
}