bruno/renderer/components/Sidebar/Collections/Collection/index.js

107 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2022-10-01 14:47:33 +02:00
import React, { useState, forwardRef, useRef, useEffect } from 'react';
2021-12-03 20:37:38 +01:00
import classnames from 'classnames';
2022-03-13 13:13:21 +01:00
import { IconChevronRight, IconDots } from '@tabler/icons';
import Dropdown from 'components/Dropdown';
2022-03-22 13:48:20 +01:00
import { collectionClicked, removeCollection } from 'providers/ReduxStore/slices/collections';
import { useDispatch } from 'react-redux';
import NewRequest from 'components/Sidebar/NewRequest';
2022-03-13 18:31:16 +01:00
import NewFolder from 'components/Sidebar/NewFolder';
2022-03-13 13:13:21 +01:00
import CollectionItem from './CollectionItem';
2022-10-01 14:47:33 +02:00
import { doesCollectionHaveItemsMatchingSearchText } from 'utils/collections/search';
2021-12-03 20:37:38 +01:00
import StyledWrapper from './StyledWrapper';
2022-10-01 14:47:33 +02:00
const Collection = ({collection, searchText}) => {
2022-03-13 18:31:16 +01:00
const [showNewFolderModal, setShowNewFolderModal] = useState(false);
const [showNewRequestModal, setShowNewRequestModal] = useState(false);
2022-10-01 14:47:33 +02:00
const [collectionIsCollapsed, setCollectionIsCollapsed] = useState(collection.collapsed);
const dispatch = useDispatch();
2022-03-13 13:13:21 +01:00
2022-10-01 14:47:33 +02:00
useEffect(() => {
if (searchText && searchText.length) {
setCollectionIsCollapsed(false);
} else {
setCollectionIsCollapsed(collection.collapsed);
}
}, [searchText, collection]);
2022-01-01 10:07:34 +01:00
const menuDropdownTippyRef = useRef();
const onMenuDropdownCreate = (ref) => menuDropdownTippyRef.current = ref;
const MenuIcon = forwardRef((props, ref) => {
return (
2022-03-22 13:48:20 +01:00
<div ref={ref} className="pr-2">
2022-01-01 10:07:34 +01:00
<IconDots size={22}/>
</div>
);
});
2021-12-03 20:37:38 +01:00
const iconClassName = classnames({
2022-10-01 14:47:33 +02:00
'rotate-90': !collectionIsCollapsed
2021-12-03 20:37:38 +01:00
});
2022-01-01 10:07:34 +01:00
const handleClick = (event) => {
dispatch(collectionClicked(collection.uid));
2021-12-03 20:37:38 +01:00
};
2022-10-01 14:47:33 +02:00
if(searchText && searchText.length) {
if(!doesCollectionHaveItemsMatchingSearchText(collection, searchText)) {
return null;
}
}
2021-12-03 20:37:38 +01:00
return (
<StyledWrapper className="flex flex-col">
2022-03-19 10:53:20 +01:00
{showNewRequestModal && <NewRequest collection={collection} onClose={() => setShowNewRequestModal(false)}/>}
{showNewFolderModal && <NewFolder collection={collection} onClose={() => setShowNewFolderModal(false)}/>}
2022-03-22 13:48:20 +01:00
<div className="flex py-1 collection-name items-center">
<div className="flex flex-grow items-center" onClick={handleClick}>
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{width:16, color: 'rgb(160 160 160)'}}/>
<div className="ml-1">{collection.name}</div>
</div>
<div className='collection-actions'>
<Dropdown
onCreate={onMenuDropdownCreate}
icon={<MenuIcon />}
placement='bottom-start'
>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
setShowNewRequestModal(true)
}}>
2022-03-13 18:31:16 +01:00
New Request
2022-01-01 10:07:34 +01:00
</div>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
2022-03-13 18:31:16 +01:00
setShowNewFolderModal(true)
}}>
2022-03-13 18:31:16 +01:00
New Folder
2022-01-01 10:07:34 +01:00
</div>
2022-03-22 13:48:20 +01:00
<div className="dropdown-item" onClick={(e) => {
dispatch(removeCollection(collection.uid));
menuDropdownTippyRef.current.hide();
}}>
Remove
</div>
2022-01-01 10:07:34 +01:00
</Dropdown>
</div>
2021-12-03 20:37:38 +01:00
</div>
<div>
2022-10-01 14:47:33 +02:00
{!collectionIsCollapsed ? (
2021-12-03 20:37:38 +01:00
<div>
{collection.items && collection.items.length ? collection.items.map((i) => {
2021-12-03 20:37:38 +01:00
return <CollectionItem
2022-03-13 13:13:21 +01:00
key={i.uid}
2021-12-03 20:37:38 +01:00
item={i}
collection={collection}
2022-10-01 14:47:33 +02:00
searchText={searchText}
2021-12-03 20:37:38 +01:00
/>
}) : null}
</div>
) : null}
</div>
</StyledWrapper>
);
};
export default Collection;