import React, { useState, useEffect } from 'react'; import { JSONTree, NoContent, Tabs } from 'UI'; import { Button } from 'antd'; import cn from 'classnames'; import AnimatedSVG, { ICONS } from 'Shared/AnimatedSVG/AnimatedSVG'; import stl from './fetchDetails.module.css'; import Headers from './components/Headers'; import { useTranslation } from 'react-i18next'; const HEADERS = 'HEADERS'; const REQUEST = 'REQUEST'; const RESPONSE = 'RESPONSE'; const TABS = [HEADERS, REQUEST, RESPONSE].map((tab) => ({ text: tab, key: tab, })); const FetchDetails = ({ resource, nextClick, prevClick, first = false, last = false, }) => { const [activeTab, setActiveTab] = useState(REQUEST); const [tabs, setTabs] = useState([]); const { t } = useTranslation(); const onTabClick = (tab) => setActiveTab(tab); const checkTabs = () => { // Could add filtering logic here if needed // const _tabs = TABS.filter(t => { // if (t.key === REQUEST && !!resource.payload) { // return true; // } // if (t.key === RESPONSE && !!resource.response) { // return true; // } // return false; // }); const _tabs = TABS; setTabs(_tabs); setActiveTab(_tabs.length > 0 ? _tabs[0].key : null); }; useEffect(() => { checkTabs(); }, [resource.index]); const renderActiveTab = (tab) => { const { payload, response = resource.body } = resource; let jsonPayload; let jsonResponse; let requestHeaders; let responseHeaders; try { jsonPayload = typeof payload === 'string' ? JSON.parse(payload) : payload; requestHeaders = jsonPayload.headers; jsonPayload.body = typeof jsonPayload.body === 'string' ? JSON.parse(jsonPayload.body) : jsonPayload.body; delete jsonPayload.headers; } catch (e) {} try { jsonResponse = typeof response === 'string' ? JSON.parse(response) : response; responseHeaders = jsonResponse.headers; jsonResponse.body = typeof jsonResponse.body === 'string' ? JSON.parse(jsonResponse.body) : jsonResponse.body; delete jsonResponse.headers; } catch (e) {} switch (tab) { case REQUEST: return (
{t('Body is Empty')}
} size="small" show={!payload} >
{jsonPayload === undefined ? (
{payload}
) : ( )}
); case RESPONSE: return (
{t('Body is Empty')}
} size="small" show={!response} >
{jsonResponse === undefined ? (
{response}
) : ( )}
); case HEADERS: return ( ); default: return null; } }; const { method, url, duration } = resource; return (
{t('URL')}
{url}
{t('Method')}
{method}
{t('Duration')}
{parseInt(duration)} {t('ms')}
{renderActiveTab(activeTab)}
); }; export default FetchDetails;