import React, { useState, useRef } from 'react'; import find from 'lodash/find'; import filter from 'lodash/filter'; import classnames from 'classnames'; import { IconChevronRight, IconChevronLeft } from '@tabler/icons'; import { useSelector, useDispatch } from 'react-redux'; import { focusTab, reorderTabs } from 'providers/ReduxStore/slices/tabs'; import NewRequest from 'components/Sidebar/NewRequest'; import CollectionToolBar from './CollectionToolBar'; import RequestTab from './RequestTab'; import StyledWrapper from './StyledWrapper'; import DraggableTab from './DraggableTab'; const RequestTabs = () => { const dispatch = useDispatch(); const tabsRef = useRef(); const [newRequestModalOpen, setNewRequestModalOpen] = useState(false); const tabs = useSelector((state) => state.tabs.tabs); const activeTabUid = useSelector((state) => state.tabs.activeTabUid); const collections = useSelector((state) => state.collections.collections); const leftSidebarWidth = useSelector((state) => state.app.leftSidebarWidth); const sidebarCollapsed = useSelector((state) => state.app.sidebarCollapsed); const screenWidth = useSelector((state) => state.app.screenWidth); const getTabClassname = (tab, index) => { return classnames('request-tab select-none', { active: tab.uid === activeTabUid, 'last-tab': tabs && tabs.length && index === tabs.length - 1 }); }; const handleClick = (tab) => { dispatch( focusTab({ uid: tab.uid }) ); }; const createNewTab = () => setNewRequestModalOpen(true); 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 effectiveSidebarWidth = sidebarCollapsed ? 0 : leftSidebarWidth; const maxTablistWidth = screenWidth - effectiveSidebarWidth - 150; const tabsWidth = collectionRequestTabs.length * 150 + 34; // 34: (+)icon const showChevrons = maxTablistWidth < tabsWidth; const leftSlide = () => { tabsRef.current.scrollBy({ left: -120, behavior: 'smooth' }); }; // todo: bring new tab to focus if its not in focus // tabsRef.current.scrollLeft const rightSlide = () => { tabsRef.current.scrollBy({ left: 120, behavior: 'smooth' }); }; const getRootClassname = () => { return classnames({ 'has-chevrons': showChevrons }); }; // Todo: Must support ephemeral requests return ( {newRequestModalOpen && ( setNewRequestModalOpen(false)} /> )} {collectionRequestTabs && collectionRequestTabs.length ? ( <>
    {showChevrons ? (
  • ) : null} {/* Moved to post mvp */} {/*
  • */}
    {collectionRequestTabs && collectionRequestTabs.length ? collectionRequestTabs.map((tab, index) => { return ( { dispatch(reorderTabs({ sourceUid: source, targetUid: target })); }} className={getTabClassname(tab, index)} onClick={() => handleClick(tab)} > ); }) : null}
    {showChevrons ? (
  • ) : null}
  • {/* Moved to post mvp */} {/*
  • */}
) : null}
); }; export default RequestTabs;