2021-12-10 21:28:44 +01:00
|
|
|
import React, { useRef, forwardRef } from 'react';
|
2021-12-03 20:37:38 +01:00
|
|
|
import range from 'lodash/range';
|
2021-12-10 21:28:44 +01:00
|
|
|
import get from 'lodash/get';
|
2022-03-13 13:13:21 +01:00
|
|
|
import actions from 'providers/Store/actions'
|
|
|
|
import { useStore } from 'providers/Store';
|
2021-12-10 21:28:44 +01:00
|
|
|
import { IconChevronRight, IconDots } from '@tabler/icons';
|
2021-12-03 20:37:38 +01:00
|
|
|
import classnames from 'classnames';
|
2022-03-13 13:13:21 +01:00
|
|
|
import Dropdown from 'components/Dropdown';
|
2021-12-03 20:37:38 +01:00
|
|
|
|
|
|
|
import StyledWrapper from './StyledWrapper';
|
|
|
|
|
2022-03-13 22:13:36 +01:00
|
|
|
const CollectionItem = ({item, collectionUid}) => {
|
2022-03-13 13:13:21 +01:00
|
|
|
const [store, storeDispatch] = useStore();
|
|
|
|
|
|
|
|
const {
|
2022-03-13 22:13:36 +01:00
|
|
|
activeRequestTabUid
|
2022-03-13 13:13:21 +01:00
|
|
|
} = store;
|
2021-12-10 21:28:44 +01:00
|
|
|
|
2022-03-13 13:13:21 +01:00
|
|
|
const dropdownTippyRef = useRef();
|
2021-12-10 21:28:44 +01:00
|
|
|
const MenuIcon = forwardRef((props, ref) => {
|
|
|
|
return (
|
|
|
|
<div ref={ref}>
|
2022-01-01 09:26:34 +01:00
|
|
|
<IconDots size={22}/>
|
2021-12-10 21:28:44 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
2021-12-03 20:37:38 +01:00
|
|
|
|
|
|
|
const iconClassName = classnames({
|
|
|
|
'rotate-90': item.collapsed
|
|
|
|
});
|
|
|
|
|
|
|
|
const itemRowClassName = classnames('flex collection-item-name items-center', {
|
2022-03-13 22:13:36 +01:00
|
|
|
'item-focused-in-tab': item.uid == activeRequestTabUid
|
2021-12-03 20:37:38 +01:00
|
|
|
});
|
|
|
|
|
2021-12-10 21:28:44 +01:00
|
|
|
const handleClick = (event) => {
|
|
|
|
let tippyEl = get(dropdownTippyRef, 'current.reference');
|
|
|
|
if(tippyEl && tippyEl.contains && tippyEl.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_ITEM_CLICK,
|
2022-03-13 22:13:36 +01:00
|
|
|
itemUid: item.uid,
|
|
|
|
collectionUid: collectionUid
|
2021-12-03 20:37:38 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-10 23:29:44 +01:00
|
|
|
const addRequest = () => {
|
2022-03-13 13:13:21 +01:00
|
|
|
storeDispatch({
|
2021-12-10 23:29:44 +01:00
|
|
|
type: actions.ADD_REQUEST,
|
2022-03-13 22:13:36 +01:00
|
|
|
itemUid: item.uid,
|
|
|
|
collectionUid: collectionUid
|
2021-12-10 23:29:44 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
let indents = range(item.depth);
|
2021-12-10 21:28:44 +01:00
|
|
|
const onDropdownCreate = (ref) => dropdownTippyRef.current = ref;
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
return (
|
|
|
|
<StyledWrapper className="flex flex-col">
|
|
|
|
<div
|
|
|
|
className={itemRowClassName}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
<div className="flex items-center h-full w-full">
|
|
|
|
{indents && indents.length ? indents.map((i) => {
|
|
|
|
return (
|
|
|
|
<div
|
2022-01-01 17:56:56 +01:00
|
|
|
className="indent-block"
|
2021-12-03 20:37:38 +01:00
|
|
|
key={i}
|
|
|
|
style = {{
|
|
|
|
width: 16,
|
2022-01-01 17:56:56 +01:00
|
|
|
height: '100%'
|
2021-12-03 20:37:38 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{/* Indent */}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}) : null}
|
|
|
|
<div
|
|
|
|
className="flex items-center"
|
|
|
|
style = {{
|
|
|
|
paddingLeft: 8
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div style={{width:16}}>
|
|
|
|
{item.items && item.items.length ? (
|
|
|
|
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{color: 'rgb(160 160 160)'}}/>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<span className="ml-1">{item.name}</span>
|
|
|
|
</div>
|
2021-12-10 21:28:44 +01:00
|
|
|
<div className="menu-icon pr-2">
|
|
|
|
<Dropdown onCreate={onDropdownCreate} icon={<MenuIcon />} placement='bottom-start'>
|
2022-01-03 08:48:15 +01:00
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
dropdownTippyRef.current.hide();
|
|
|
|
addRequest();
|
|
|
|
}}>
|
2022-03-13 18:31:16 +01:00
|
|
|
New Request
|
2022-01-03 08:48:15 +01:00
|
|
|
</div>
|
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
dropdownTippyRef.current.hide();
|
|
|
|
}}>
|
2022-03-13 18:31:16 +01:00
|
|
|
New Folder
|
2022-01-03 08:48:15 +01:00
|
|
|
</div>
|
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
dropdownTippyRef.current.hide();
|
|
|
|
}}>
|
|
|
|
Rename
|
|
|
|
</div>
|
|
|
|
<div className="dropdown-item" onClick={(e) => {
|
|
|
|
dropdownTippyRef.current.hide();
|
|
|
|
}}>
|
|
|
|
Delete
|
2021-12-10 21:28:44 +01:00
|
|
|
</div>
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
2021-12-03 20:37:38 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{item.collapsed ? (
|
|
|
|
<div>
|
|
|
|
{item.items && item.items.length ? item.items.map((i) => {
|
|
|
|
return <CollectionItem
|
2022-03-13 22:13:36 +01:00
|
|
|
key={i.uid}
|
2021-12-03 20:37:38 +01:00
|
|
|
item={i}
|
2022-03-13 22:13:36 +01:00
|
|
|
collectionUid={collectionUid}
|
2021-12-03 20:37:38 +01:00
|
|
|
/>
|
|
|
|
}) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</StyledWrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CollectionItem;
|