Skip to content

Commit

Permalink
Feat/support for multiple preview modes of same response type (usebru…
Browse files Browse the repository at this point in the history
…no#2606)

* pr review changes
* collection root object in export json

* import environment updates

* tests run execution order fix for collection runs

* support for multiple preview modes of same type
  • Loading branch information
lohxt1 authored Sep 16, 2024
1 parent 238c790 commit 4f7cefe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const QueryResultPreview = ({
setNumPages(numPages);
}
// Fail safe, so we don't render anything with an invalid tab
if (!allowedPreviewModes.includes(previewTab)) {
if (!allowedPreviewModes.find((previewMode) => previewMode?.uid == previewTab?.uid)) {
return null;
}

Expand All @@ -40,7 +40,7 @@ const QueryResultPreview = ({
dispatch(sendRequest(item, collection.uid));
};

switch (previewTab) {
switch (previewTab?.mode) {
case 'preview-web': {
const webViewSrc = data.replace('<head>', `<head><base href="${item.requestSent?.url || ''}">`);
return (
Expand Down
24 changes: 14 additions & 10 deletions packages/bruno-app/src/components/ResponsePane/QueryResult/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useState } from 'react';
import { useMemo } from 'react';
import { useEffect } from 'react';
import { useTheme } from 'providers/Theme/index';
import { uuid } from 'utils/common/index';

const formatResponse = (data, mode, filter) => {
if (data === undefined) {
Expand Down Expand Up @@ -66,18 +67,18 @@ const QueryResult = ({ item, collection, data, dataBuffer, width, disableRunEven

const allowedPreviewModes = useMemo(() => {
// Always show raw
const allowedPreviewModes = ['raw'];
const allowedPreviewModes = [{ mode: 'raw', name: 'Raw', uid: uuid() }];

if (mode.includes('html') && typeof data === 'string') {
allowedPreviewModes.unshift('preview-web');
allowedPreviewModes.unshift({ mode: 'preview-web', name: 'Web', uid: uuid() });
} else if (mode.includes('image')) {
allowedPreviewModes.unshift('preview-image');
allowedPreviewModes.unshift({ mode: 'preview-image', name: 'Image', uid: uuid() });
} else if (contentType.includes('pdf')) {
allowedPreviewModes.unshift('preview-pdf');
allowedPreviewModes.unshift({ mode: 'preview-pdf', name: 'PDF', uid: uuid() });
} else if (contentType.includes('audio')) {
allowedPreviewModes.unshift('preview-audio');
allowedPreviewModes.unshift({ mode: 'preview-audio', name: 'Audio', uid: uuid() });
} else if (contentType.includes('video')) {
allowedPreviewModes.unshift('preview-video');
allowedPreviewModes.unshift({ mode: 'preview-video', name: 'Video', uid: uuid() });
}

return allowedPreviewModes;
Expand All @@ -86,7 +87,7 @@ const QueryResult = ({ item, collection, data, dataBuffer, width, disableRunEven
const [previewTab, setPreviewTab] = useState(allowedPreviewModes[0]);
// Ensure the active Tab is always allowed
useEffect(() => {
if (!allowedPreviewModes.includes(previewTab)) {
if (!allowedPreviewModes.find((previewMode) => previewMode?.uid == previewTab?.uid)) {
setPreviewTab(allowedPreviewModes[0]);
}
}, [previewTab, allowedPreviewModes]);
Expand All @@ -98,12 +99,15 @@ const QueryResult = ({ item, collection, data, dataBuffer, width, disableRunEven

return allowedPreviewModes.map((previewMode) => (
<div
className={classnames('select-none capitalize', previewMode === previewTab ? 'active' : 'cursor-pointer')}
className={classnames(
'select-none capitalize',
previewMode?.uid === previewTab?.uid ? 'active' : 'cursor-pointer'
)}
role="tab"
onClick={() => setPreviewTab(previewMode)}
key={previewMode}
key={previewMode?.uid}
>
{previewMode.replace(/-(.*)/, ' ')}
{previewMode?.name}
</div>
));
}, [allowedPreviewModes, previewTab]);
Expand Down

0 comments on commit 4f7cefe

Please sign in to comment.