import React, { useState, useRef, forwardRef, useEffect } from 'react'; 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 NewFolder from 'components/Sidebar/NewFolder'; import RequestMethod from './RequestMethod'; import RenameCollectionItem from './RenameCollectionItem'; import DeleteCollectionItem from './DeleteCollectionItem'; import { isItemARequest, isItemAFolder, itemIsOpenedInTabs } from 'utils/tabs'; import { doesRequestMatchSearchText, doesFolderHaveItemsMatchSearchText } from 'utils/collections/search'; import StyledWrapper from './StyledWrapper'; const CollectionItem = ({item, collection, searchText}) => { const tabs = useSelector((state) => state.tabs.tabs); const activeTabUid = useSelector((state) => state.tabs.activeTabUid); const isDragging = useSelector((state) => state.app.isDragging); const dispatch = useDispatch(); const [renameItemModalOpen, setRenameItemModalOpen] = useState(false); const [deleteItemModalOpen, setDeleteItemModalOpen] = useState(false); const [newRequestModalOpen, setNewRequestModalOpen] = useState(false); const [newFolderModalOpen, setNewFolderModalOpen] = useState(false); const [itemIsCollapsed, setItemisCollapsed] = useState(item.collapsed); useEffect(() => { if (searchText && searchText.length) { setItemisCollapsed(false); } else { setItemisCollapsed(item.collapsed); } }, [searchText, item]); const dropdownTippyRef = useRef(); const MenuIcon = forwardRef((props, ref) => { return (
); }); const iconClassName = classnames({ 'rotate-90': !itemIsCollapsed }); const itemRowClassName = classnames('flex collection-item-name items-center', { 'item-focused-in-tab': item.uid == activeTabUid }); const handleClick = (event) => { 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); const className = classnames('flex flex-col w-full', { 'is-dragging': isDragging }); if(searchText && searchText.length) { if(isItemARequest(item)) { if(!doesRequestMatchSearchText(item, searchText)) { return null; } } else { if (!doesFolderHaveItemsMatchSearchText(item, searchText)) { return null; }; } } return ( {renameItemModalOpen && setRenameItemModalOpen(false)}/>} {deleteItemModalOpen && setDeleteItemModalOpen(false)}/>} {newRequestModalOpen && setNewRequestModalOpen(false)}/>} {newFolderModalOpen && setNewFolderModalOpen(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(); setNewFolderModalOpen(true); }}> New Folder
)}
{ dropdownTippyRef.current.hide(); setRenameItemModalOpen(true); }}> Rename
{ dropdownTippyRef.current.hide(); setDeleteItemModalOpen(true); }}> Delete
{!itemIsCollapsed ? (
{item.items && item.items.length ? item.items.map((i) => { return }) : null}
) : null}
); }; export default CollectionItem;