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

156 lines
5.0 KiB
JavaScript
Raw Normal View History

2022-03-18 20:35:43 +01:00
import React, { useState, useRef, forwardRef } from 'react';
2021-12-03 20:37:38 +01:00
import range from 'lodash/range';
import get from 'lodash/get';
import { IconChevronRight, IconDots } from '@tabler/icons';
2021-12-03 20:37:38 +01:00
import classnames from 'classnames';
import { useSelector, useDispatch } from 'react-redux';
import { addTab, focusTab } from 'providers/ReduxStore/slices/tabs';
import { isItemARequest, isItemAFolder, itemIsOpenedInTabs } from 'utils/tabs';
2022-03-13 13:13:21 +01:00
import Dropdown from 'components/Dropdown';
import RequestMethod from './RequestMethod';
2022-03-18 22:48:15 +01:00
import RenameCollectionItem from './RenameCollectionItem';
2022-03-18 20:35:43 +01:00
import DeleteCollectionItem from './DeleteCollectionItem';
2021-12-03 20:37:38 +01:00
import StyledWrapper from './StyledWrapper';
const CollectionItem = ({item, collection}) => {
const tabs = useSelector((state) => state.tabs.tabs);
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
const dispatch = useDispatch();
2022-03-18 22:48:15 +01:00
const [renameItemModalOpen, setRenameItemModalOpen] = useState(false);
2022-03-18 20:35:43 +01:00
const [deleteItemModalOpen, setDeleteItemModalOpen] = useState(false);
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 == activeTabUid
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;
}
if(isItemARequest(item)) {
if(itemIsOpenedInTabs(item, tabs)) {
dispatch(focusTab({
uid: item.uid
}));
} else {
dispatch(addTab({
uid: item.uid,
collectionUid: collection.uid
}));
}
} else {
// todo for folder: must expand folder : item.collapsed = !item.collapsed;
}
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;
const isRequest = isItemARequest(item);
const isFolder = isItemAFolder(item);
2021-12-03 20:37:38 +01:00
return (
<StyledWrapper className="flex flex-col">
2022-03-18 22:48:15 +01:00
{renameItemModalOpen && <RenameCollectionItem item={item} collection={collection} onClose={() => setRenameItemModalOpen(false)}/>}
2022-03-18 20:35:43 +01:00
{deleteItemModalOpen && <DeleteCollectionItem item={item} collection={collection} onClose={() => setDeleteItemModalOpen(false)}/>}
2021-12-03 20:37:38 +01:00
<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}}>
{isFolder ? (
2021-12-03 20:37:38 +01:00
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{color: 'rgb(160 160 160)'}}/>
) : null}
</div>
<div className="ml-1 flex items-center">
<RequestMethod item={item}/>
2022-03-17 21:54:30 +01:00
<div>{item.name}</div>
</div>
2021-12-03 20:37:38 +01:00
</div>
<div className="menu-icon pr-2">
<Dropdown onCreate={onDropdownCreate} icon={<MenuIcon />} placement='bottom-start'>
{isFolder && (
<>
<div className="dropdown-item" onClick={(e) => {
dropdownTippyRef.current.hide();
}}>
New Request
</div>
<div className="dropdown-item" onClick={(e) => {
dropdownTippyRef.current.hide();
}}>
New Folder
</div>
</>
)}
2022-01-03 08:48:15 +01:00
<div className="dropdown-item" onClick={(e) => {
dropdownTippyRef.current.hide();
2022-03-18 22:48:15 +01:00
setRenameItemModalOpen(true);
2022-01-03 08:48:15 +01:00
}}>
Rename
</div>
2022-03-18 20:35:43 +01:00
<div className="dropdown-item delete-item" onClick={(e) => {
2022-01-03 08:48:15 +01:00
dropdownTippyRef.current.hide();
2022-03-18 20:35:43 +01:00
setDeleteItemModalOpen(true);
2022-01-03 08:48:15 +01:00
}}>
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}
collection={collection}
2021-12-03 20:37:38 +01:00
/>
}) : null}
</div>
) : null}
</StyledWrapper>
);
};
export default CollectionItem;