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

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-03-13 15:29:10 +01:00
import React, { useState, forwardRef, useRef } from 'react';
2022-01-01 10:07:34 +01:00
import get from 'lodash/get';
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';
import actions from 'providers/Store/actions'
import { useStore } from 'providers/Store';
2022-03-13 15:29:10 +01:00
import AddFolder from 'components/Sidebar/AddFolder';
2022-03-13 13:13:21 +01:00
import CollectionItem from './CollectionItem';
2021-12-03 20:37:38 +01:00
import StyledWrapper from './StyledWrapper';
2022-03-13 13:13:21 +01:00
const Collection = ({collection}) => {
2022-03-13 15:29:10 +01:00
const [showAddFolderModal, setShowAddFolderModal] = useState(false);
2022-03-13 13:13:21 +01:00
const [store, storeDispatch] = useStore();
const {
activeRequestTabId
} = store;
2022-01-01 10:07:34 +01:00
const menuDropdownTippyRef = useRef();
const onMenuDropdownCreate = (ref) => menuDropdownTippyRef.current = ref;
const MenuIcon = forwardRef((props, ref) => {
return (
<div ref={ref}>
<IconDots size={22}/>
</div>
);
});
2021-12-03 20:37:38 +01:00
const iconClassName = classnames({
'rotate-90': collection.collapsed
});
2022-01-01 10:07:34 +01:00
const handleClick = (event) => {
2022-03-13 13:13:21 +01:00
let envTippyEl = get(menuDropdownTippyRef, 'current.reference');
2022-01-01 10:07:34 +01:00
if(envTippyEl && envTippyEl.contains && envTippyEl.contains(event.target)) {
return;
}
2022-03-13 13:13:21 +01:00
storeDispatch({
2021-12-03 20:37:38 +01:00
type: actions.SIDEBAR_COLLECTION_CLICK,
2022-03-13 13:13:21 +01:00
collectionUid: collection.uid
2021-12-03 20:37:38 +01:00
});
};
2022-03-13 15:29:10 +01:00
const hideAddFolderModal = () => setShowAddFolderModal(false);
2022-03-13 13:13:21 +01:00
const collectionItems = get(collection, 'current.items');
2021-12-03 20:37:38 +01:00
return (
<StyledWrapper className="flex flex-col">
2022-03-13 15:29:10 +01:00
{showAddFolderModal && (
<AddFolder
collectionUid={collection.uid}
handleCancel={hideAddFolderModal}
handleClose={hideAddFolderModal}
/>
)}
2021-12-03 20:37:38 +01:00
<div className="flex py-1 collection-name items-center" onClick={handleClick}>
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{width:16, color: 'rgb(160 160 160)'}}/>
2022-03-13 13:13:21 +01:00
<span className="ml-1">{collection.current.name}</span>
2022-01-01 10:07:34 +01:00
<div className="collection-actions">
<Dropdown onCreate={onMenuDropdownCreate} icon={<MenuIcon />} placement='bottom-start'>
<div>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
}}>
Add Request
</div>
</div>
<div>
<div className="dropdown-item" onClick={(e) => {
menuDropdownTippyRef.current.hide();
2022-03-13 15:29:10 +01:00
setShowAddFolderModal(true)
2022-01-01 10:07:34 +01:00
}}>
Add Folder
</div>
</div>
</Dropdown>
</div>
2021-12-03 20:37:38 +01:00
</div>
<div>
{collection.collapsed ? (
<div>
2022-03-13 13:13:21 +01:00
{collectionItems && collectionItems.length ? collectionItems.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}
collectionId={collection.id}
actions={actions}
2022-03-13 13:13:21 +01:00
dispatch={storeDispatch}
2021-12-03 20:37:38 +01:00
activeRequestTabId={activeRequestTabId}
/>
}) : null}
</div>
) : null}
</div>
</StyledWrapper>
);
};
export default Collection;