import React from 'react'; import find from 'lodash/find'; import filter from 'lodash/filter'; import classnames from 'classnames'; import { IconHome2 } from '@tabler/icons'; import { useSelector, useDispatch } from 'react-redux'; import { focusTab, closeTab } from 'providers/ReduxStore/slices/tabs'; import { findItemInCollection } from 'utils/collections'; import CollectionToolBar from './CollectionToolBar'; import StyledWrapper from './StyledWrapper'; const RequestTabs = () => { const tabs = useSelector((state) => state.tabs.tabs); const activeTabUid = useSelector((state) => state.tabs.activeTabUid); const collections = useSelector((state) => state.collections.collections); const dispatch = useDispatch(); const getTabClassname = (tab, index) => { return classnames("request-tab select-none", { 'active': tab.uid === activeTabUid, 'last-tab': tabs && tabs.length && (index === tabs.length - 1) }); }; const getMethodColor = (method) => { let color = ''; switch(method) { case 'GET': { color = 'rgb(5, 150, 105)'; break; } case 'POST': { color = '#8e44ad'; break; } } return color; }; const handleClick = (tab) => { dispatch(focusTab({ uid: tab.uid })); }; const handleCloseClick = (event, tab) => { event.stopPropagation(); event.preventDefault(); dispatch(closeTab({ tabUid: tab.uid })) }; const createNewTab = () => { // todo }; if(!activeTabUid) { return null; } const activeTab = find(tabs, (t) => t.uid === activeTabUid); if(!activeTab) { return ( Something went wrong! ); } const activeCollection = find(collections, (c) => c.uid === activeTab.collectionUid); const collectionRequestTabs = filter(tabs, (t) => t.collectionUid === activeTab.collectionUid); const item = findItemInCollection(activeCollection, activeTab.uid); if(!item || !item.uid) { return ( Request not found! ); } const getRequestName = (tab) => { const item = findItemInCollection(activeCollection, tab.uid); return item ? item.name : ''; } const getRequestMethod = (tab) => { const item = findItemInCollection(activeCollection, tab.uid); return item ? item.request.method : ''; } return ( {collectionRequestTabs && collectionRequestTabs.length ? ( <> {collectionRequestTabs && collectionRequestTabs.length ? collectionRequestTabs.map((tab, index) => { return handleClick(tab)}> {getRequestMethod(tab)} {getRequestName(tab)} handleCloseClick(e, tab)}> {!tab.hasChanges ? ( ) : ( ) } }) : null} > ) : null} ); }; export default RequestTabs;