mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-29 15:44:13 +00:00
86 lines
2.5 KiB
JavaScript
86 lines
2.5 KiB
JavaScript
import CodeEditor from 'components/CodeEditor/index';
|
|
import { get } from 'lodash';
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
import { sendRequest } from 'providers/ReduxStore/slices/collections/actions';
|
|
import { Document, Page } from 'react-pdf';
|
|
import { useState } from 'react';
|
|
import 'pdfjs-dist/build/pdf.worker';
|
|
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
|
|
import 'react-pdf/dist/esm/Page/TextLayer.css';
|
|
|
|
const QueryResultPreview = ({
|
|
previewTab,
|
|
allowedPreviewModes,
|
|
data,
|
|
dataBuffer,
|
|
formattedData,
|
|
item,
|
|
contentType,
|
|
collection,
|
|
mode,
|
|
disableRunEventListener,
|
|
storedTheme
|
|
}) => {
|
|
const preferences = useSelector((state) => state.app.preferences);
|
|
const dispatch = useDispatch();
|
|
|
|
const [numPages, setNumPages] = useState(null);
|
|
function onDocumentLoadSuccess({ numPages }) {
|
|
setNumPages(numPages);
|
|
}
|
|
// Fail safe, so we don't render anything with an invalid tab
|
|
if (!allowedPreviewModes.includes(previewTab)) {
|
|
return null;
|
|
}
|
|
|
|
const onRun = () => {
|
|
if (disableRunEventListener) {
|
|
return;
|
|
}
|
|
dispatch(sendRequest(item, collection.uid));
|
|
};
|
|
|
|
switch (previewTab) {
|
|
case 'preview-web': {
|
|
const webViewSrc = data.replace('<head>', `<head><base href="${item.requestSent?.url || ''}">`);
|
|
return (
|
|
<webview
|
|
src={`data:text/html; charset=utf-8,${encodeURIComponent(webViewSrc)}`}
|
|
webpreferences="disableDialogs=true, javascript=yes"
|
|
className="h-full bg-white"
|
|
/>
|
|
);
|
|
}
|
|
case 'preview-image': {
|
|
return <img src={`data:${contentType.replace(/\;(.*)/, '')};base64,${dataBuffer}`} className="mx-auto" />;
|
|
}
|
|
case 'preview-pdf': {
|
|
return (
|
|
<div className="preview-pdf" style={{ height: '100%', overflow: 'auto', maxHeight: 'calc(100vh - 220px)' }}>
|
|
<Document file={`data:application/pdf;base64,${dataBuffer}`} onLoadSuccess={onDocumentLoadSuccess}>
|
|
{Array.from(new Array(numPages), (el, index) => (
|
|
<Page key={`page_${index + 1}`} pageNumber={index + 1} renderAnnotationLayer={false} />
|
|
))}
|
|
</Document>
|
|
</div>
|
|
);
|
|
}
|
|
default:
|
|
case 'raw': {
|
|
return (
|
|
<CodeEditor
|
|
collection={collection}
|
|
font={get(preferences, 'font.codeFont', 'default')}
|
|
theme={storedTheme}
|
|
onRun={onRun}
|
|
value={formattedData}
|
|
mode={mode}
|
|
readOnly
|
|
/>
|
|
);
|
|
}
|
|
}
|
|
};
|
|
|
|
export default QueryResultPreview;
|