import React, { useState, useRef, forwardRef } from 'react'; import get from 'lodash/get'; import range from 'lodash/range'; import classnames from 'classnames'; import { IconChevronRight, IconDots } from '@tabler/icons'; import { useSelector, useDispatch } from 'react-redux'; import { addTab, focusTab } from 'providers/ReduxStore/slices/tabs'; import { collectionFolderClicked } from 'providers/ReduxStore/slices/collections'; import Dropdown from 'components/Dropdown'; import NewRequest from 'components/Sidebar/NewRequest'; import RequestMethod from './RequestMethod'; import RenameCollectionItem from './RenameCollectionItem'; import DeleteCollectionItem from './DeleteCollectionItem'; import { isItemARequest, isItemAFolder, itemIsOpenedInTabs } from 'utils/tabs'; import StyledWrapper from './StyledWrapper'; const CollectionItem = ({item, collection}) => { const tabs = useSelector((state) => state.tabs.tabs); const activeTabUid = useSelector((state) => state.tabs.activeTabUid); const dispatch = useDispatch(); const [renameItemModalOpen, setRenameItemModalOpen] = useState(false); const [deleteItemModalOpen, setDeleteItemModalOpen] = useState(false); const [newRequestModalOpen, setNewRequestModalOpen] = useState(false); const dropdownTippyRef = useRef(); const MenuIcon = forwardRef((props, ref) => { return (
); }); const iconClassName = classnames({ 'rotate-90': item.collapsed }); const itemRowClassName = classnames('flex collection-item-name items-center', { 'item-focused-in-tab': item.uid == activeTabUid }); const handleClick = (event) => { let tippyEl = get(dropdownTippyRef, 'current.reference'); if(tippyEl && tippyEl.contains && tippyEl.contains(event.target)) { return; } if(event && event.target && event.target.className === 'dropdown-item') { return; } if(isItemARequest(item)) { if(itemIsOpenedInTabs(item, tabs)) { dispatch(focusTab({ uid: item.uid })); } else { dispatch(addTab({ uid: item.uid, collectionUid: collection.uid })); } } else { dispatch(collectionFolderClicked({ itemUid: item.uid, collectionUid: collection.uid })); } }; let indents = range(item.depth); const onDropdownCreate = (ref) => dropdownTippyRef.current = ref; const isFolder = isItemAFolder(item); return ( {renameItemModalOpen && setRenameItemModalOpen(false)}/>} {deleteItemModalOpen && setDeleteItemModalOpen(false)}/>} {newRequestModalOpen && setNewRequestModalOpen(false)}/>}
{indents && indents.length ? indents.map((i) => { return (
 {/* Indent */}
); }) : null}
{isFolder ? ( ) : null}
{item.name}
} placement='bottom-start'> {isFolder && ( <>
{ dropdownTippyRef.current.hide(); setNewRequestModalOpen(true); }}> New Request
{ dropdownTippyRef.current.hide(); }}> New Folder
)}
{ dropdownTippyRef.current.hide(); setRenameItemModalOpen(true); }}> Rename
{ dropdownTippyRef.current.hide(); setDeleteItemModalOpen(true); }}> Delete
{item.collapsed ? (
{item.items && item.items.length ? item.items.map((i) => { return }) : null}
) : null}
); }; export default CollectionItem;