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

138 lines
3.8 KiB
JavaScript
Raw Normal View History

import React, { useRef, forwardRef } from 'react';
2021-12-03 20:37:38 +01:00
import range from 'lodash/range';
import get from 'lodash/get';
2022-03-13 13:13:21 +01:00
import actions from 'providers/Store/actions'
import { useStore } from 'providers/Store';
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';
const CollectionItem = ({item, collectionUid}) => {
2022-03-13 13:13:21 +01:00
const [store, storeDispatch] = useStore();
const {
activeRequestTabUid
2022-03-13 13:13:21 +01:00
} = store;
2022-03-13 13:13:21 +01:00
const dropdownTippyRef = useRef();
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': item.collapsed
});
const itemRowClassName = classnames('flex collection-item-name items-center', {
'item-focused-in-tab': item.uid == activeRequestTabUid
2021-12-03 20:37:38 +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,
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,
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);
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
}}
>
&nbsp;{/* 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>
<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
</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
key={i.uid}
2021-12-03 20:37:38 +01:00
item={i}
collectionUid={collectionUid}
2021-12-03 20:37:38 +01:00
/>
}) : null}
</div>
) : null}
</StyledWrapper>
);
};
export default CollectionItem;