mirror of
https://github.com/usebruno/bruno.git
synced 2025-01-12 08:58:15 +01:00
fix(#279): fixed codemirror crashes resulting in white screen
This commit is contained in:
parent
7297bb184e
commit
bfc03f5ae4
@ -3,10 +3,12 @@ import CodeEditor from 'components/CodeEditor';
|
|||||||
import { useTheme } from 'providers/Theme';
|
import { useTheme } from 'providers/Theme';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
|
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
|
||||||
|
import { getContentType, safeStringifyJSON, safeParseXML } from 'utils/common';
|
||||||
|
import { getCodeMirrorModeBasedOnContentType } from 'utils/common/codemirror';
|
||||||
|
|
||||||
import StyledWrapper from './StyledWrapper';
|
import StyledWrapper from './StyledWrapper';
|
||||||
|
|
||||||
const QueryResult = ({ item, collection, value, width, disableRunEventListener, mode }) => {
|
const QueryResult = ({ item, collection, data, width, disableRunEventListener, headers }) => {
|
||||||
const { storedTheme } = useTheme();
|
const { storedTheme } = useTheme();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
@ -17,17 +19,50 @@ const QueryResult = ({ item, collection, value, width, disableRunEventListener,
|
|||||||
dispatch(sendRequest(item, collection.uid));
|
dispatch(sendRequest(item, collection.uid));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const contentType = getContentType(headers);
|
||||||
|
const mode = getCodeMirrorModeBasedOnContentType(contentType);
|
||||||
|
|
||||||
|
const formatResponse = (data, mode) => {
|
||||||
|
if (!data) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode.includes('json')) {
|
||||||
|
return safeStringifyJSON(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode.includes('xml')) {
|
||||||
|
let parsed = safeParseXML(data, { collapseContent: true });
|
||||||
|
|
||||||
|
if (typeof parsed === 'string') {
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeStringifyJSON(parsed, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (['text', 'html'].includes(mode)) {
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeStringifyJSON(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// final fallback
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return safeStringifyJSON(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = formatResponse(data, mode);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}>
|
<StyledWrapper className="px-3 w-full" style={{ maxWidth: width }}>
|
||||||
<div className="h-full">
|
<div className="h-full">
|
||||||
<CodeEditor
|
<CodeEditor collection={collection} theme={storedTheme} onRun={onRun} value={value} mode={mode} readOnly />
|
||||||
collection={collection}
|
|
||||||
theme={storedTheme}
|
|
||||||
onRun={onRun}
|
|
||||||
value={value || ''}
|
|
||||||
mode={mode}
|
|
||||||
readOnly
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</StyledWrapper>
|
</StyledWrapper>
|
||||||
);
|
);
|
||||||
|
@ -2,7 +2,6 @@ import React from 'react';
|
|||||||
import find from 'lodash/find';
|
import find from 'lodash/find';
|
||||||
import classnames from 'classnames';
|
import classnames from 'classnames';
|
||||||
import { useDispatch, useSelector } from 'react-redux';
|
import { useDispatch, useSelector } from 'react-redux';
|
||||||
import { getContentType, formatResponse } from 'utils/common';
|
|
||||||
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
|
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
|
||||||
import QueryResult from './QueryResult';
|
import QueryResult from './QueryResult';
|
||||||
import Overlay from './Overlay';
|
import Overlay from './Overlay';
|
||||||
@ -41,8 +40,8 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
|
|||||||
item={item}
|
item={item}
|
||||||
collection={collection}
|
collection={collection}
|
||||||
width={rightPaneWidth}
|
width={rightPaneWidth}
|
||||||
value={response.data ? formatResponse(response) : ''}
|
data={response.data}
|
||||||
mode={getContentType(response.headers)}
|
headers={response.headers}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,8 @@ const ResponsePane = ({ rightPaneWidth, item, collection }) => {
|
|||||||
collection={collection}
|
collection={collection}
|
||||||
width={rightPaneWidth}
|
width={rightPaneWidth}
|
||||||
disableRunEventListener={true}
|
disableRunEventListener={true}
|
||||||
value={responseReceived && responseReceived.data ? safeStringifyJSON(responseReceived.data, true) : ''}
|
data={responseReceived.data}
|
||||||
|
headers={responseReceived.headers}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -42,3 +42,25 @@ export const defineCodeMirrorBrunoVariablesMode = (variables, mode) => {
|
|||||||
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || mode), variablesOverlay);
|
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || mode), variablesOverlay);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getCodeMirrorModeBasedOnContentType = (contentType) => {
|
||||||
|
if (!contentType || typeof contentType !== 'string') {
|
||||||
|
return 'application/text';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType.includes('json')) {
|
||||||
|
return 'application/ld+json';
|
||||||
|
} else if (contentType.includes('xml')) {
|
||||||
|
return 'application/xml';
|
||||||
|
} else if (contentType.includes('html')) {
|
||||||
|
return 'application/html';
|
||||||
|
} else if (contentType.includes('text')) {
|
||||||
|
return 'application/text';
|
||||||
|
} else if (contentType.includes('application/edn')) {
|
||||||
|
return 'application/xml';
|
||||||
|
} else if (mimeType.includes('yaml')) {
|
||||||
|
return 'application/yaml';
|
||||||
|
} else {
|
||||||
|
return 'application/text';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -51,6 +51,17 @@ export const safeStringifyJSON = (obj, indent = false) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const safeParseXML = (str) => {
|
||||||
|
if (!str || !str.length || typeof str !== 'string') {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return xmlFormat(str);
|
||||||
|
} catch (e) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
|
// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
|
||||||
export const normalizeFileName = (name) => {
|
export const normalizeFileName = (name) => {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
@ -80,16 +91,6 @@ export const getContentType = (headers) => {
|
|||||||
return contentType[0];
|
return contentType[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
};
|
};
|
||||||
|
|
||||||
export const formatResponse = (response) => {
|
|
||||||
let type = getContentType(response.headers);
|
|
||||||
if (type.includes('json')) {
|
|
||||||
return safeStringifyJSON(response.data, true);
|
|
||||||
}
|
|
||||||
if (type.includes('xml')) {
|
|
||||||
return xmlFormat(response.data, { collapseContent: true });
|
|
||||||
}
|
|
||||||
return response.data;
|
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user