Custom JSON formatter that works with template syntax (#128)

This commit is contained in:
Gregory Schier
2024-10-21 22:17:14 +00:00
committed by GitHub
parent aa7f18a16f
commit e216214085
17 changed files with 414 additions and 117 deletions

View File

@@ -5,8 +5,8 @@ import { createGlobalState } from 'react-use';
import { useCopy } from '../../hooks/useCopy';
import { useDebouncedValue } from '../../hooks/useDebouncedValue';
import { useFilterResponse } from '../../hooks/useFilterResponse';
import { useFormatText } from '../../hooks/useFormatText';
import { useToggle } from '../../hooks/useToggle';
import { tryFormatJson, tryFormatXml } from '../../lib/formatters';
import { CopyButton } from '../CopyButton';
import { Banner } from '../core/Banner';
import { Button } from '../core/Button';
@@ -117,6 +117,8 @@ export function TextViewer({
toggleSearch,
]);
const formattedBody = useFormatText({ text, language, pretty });
if (!showLargeResponse && text.length > LARGE_RESPONSE_BYTES) {
return (
<Banner color="primary" className="h-full flex flex-col gap-3">
@@ -140,12 +142,9 @@ export function TextViewer({
);
}
const formattedBody =
pretty && language === 'json'
? tryFormatJson(text)
: pretty && (language === 'xml' || language === 'html')
? tryFormatXml(text)
: text;
if (formattedBody.isFetching) {
return null;
}
let body;
if (isSearching && filterText?.length > 0) {
@@ -155,7 +154,7 @@ export function TextViewer({
body = filteredResponse.data != null ? filteredResponse.data : '';
}
} else {
body = formattedBody;
body = formattedBody.data;
}
return (