bruno/renderer/components/ResponsePane/index.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-03-22 13:48:20 +01:00
import React from 'react';
import find from 'lodash/find';
import classnames from 'classnames';
2022-03-22 13:48:20 +01:00
import { useSelector, useDispatch } from 'react-redux';
import { updateResponsePaneTab } from 'providers/ReduxStore/slices/tabs';
2022-01-01 18:02:27 +01:00
import QueryResult from './QueryResult';
2022-01-28 18:33:10 +01:00
import Overlay from './Overlay';
2022-03-14 22:19:14 +01:00
import Placeholder from './Placeholder';
2022-01-01 18:02:27 +01:00
import ResponseHeaders from './ResponseHeaders';
import StatusCode from './StatusCode';
import ResponseTime from './ResponseTime';
import ResponseSize from './ResponseSize';
import StyledWrapper from './StyledWrapper';
2022-03-22 13:48:20 +01:00
const ResponsePane = ({rightPaneWidth, item, isLoading}) => {
const dispatch = useDispatch();
const tabs = useSelector((state) => state.tabs.tabs);
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
2022-03-22 13:48:20 +01:00
const selectTab = (tab) => {
dispatch(updateResponsePaneTab({
uid: item.uid,
responsePaneTab: tab
}))
};
2022-03-22 13:48:20 +01:00
const response = item.response || {};
const getTabPanel = (tab) => {
switch(tab) {
case 'response': {
return (
<QueryResult
width={rightPaneWidth}
value={response.data ? JSON.stringify(response.data, null, 2) : ''}
/>
);
}
case 'headers': {
2021-12-31 18:55:53 +01:00
return (
2022-01-01 19:42:38 +01:00
<ResponseHeaders headers={response.headers}/>
2021-12-31 18:55:53 +01:00
);
}
default: {
return <div>404 | Not found</div>;
}
}
2022-03-22 13:48:20 +01:00
};
2022-01-28 18:33:10 +01:00
if(isLoading) {
return (
<StyledWrapper className="flex h-full relative">
<Overlay />
</StyledWrapper>
);
}
2022-03-14 22:19:14 +01:00
if(response.state !== 'success') {
return (
<StyledWrapper className="flex h-full relative">
<Placeholder />
</StyledWrapper>
);
}
2022-03-22 13:48:20 +01:00
if(!activeTabUid) {
return (
<div>Something went wrong</div>
);
}
const focusedTab = find(tabs, (t) => t.uid === activeTabUid);
if(!focusedTab || !focusedTab.uid || !focusedTab.responsePaneTab) {
return (
<div className="pb-4 px-4">An error occured!</div>
);
}
const getTabClassname = (tabName) => {
return classnames(`tab select-none ${tabName}`, {
'active': tabName === focusedTab.responsePaneTab
});
};
return (
2022-01-28 18:33:10 +01:00
<StyledWrapper className="flex flex-col h-full relative">
2022-03-22 13:48:20 +01:00
<div className="flex items-center px-3 tabs" role="tablist">
<div className={getTabClassname('response')} role="tab" onClick={() => selectTab('response')}>Response</div>
<div className={getTabClassname('headers')} role="tab" onClick={() => selectTab('headers')}>Headers</div>
2022-01-01 19:42:38 +01:00
{!isLoading ? (
<div className="flex flex-grow justify-end items-center">
<StatusCode status={response.status}/>
2022-01-01 20:18:12 +01:00
<ResponseTime duration={response.duration}/>
2022-01-01 20:39:54 +01:00
<ResponseSize size={response.size}/>
2022-01-01 19:42:38 +01:00
</div>
) : null }
</div>
2022-03-22 13:48:20 +01:00
<section className="flex flex-grow mt-5">
{getTabPanel(focusedTab.responsePaneTab)}
</section>
</StyledWrapper>
)
};
export default ResponsePane;