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

189 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

2022-10-01 14:47:33 +02:00
import React, { useState, useRef, forwardRef, useEffect } from 'react';
2022-03-19 10:53:20 +01:00
import range from 'lodash/range';
2021-12-03 20:37:38 +01:00
import classnames from 'classnames';
2022-03-19 10:53:20 +01:00
import { IconChevronRight, IconDots } from '@tabler/icons';
import { useSelector, useDispatch } from 'react-redux';
import { addTab, focusTab } from 'providers/ReduxStore/slices/tabs';
2022-03-19 10:53:20 +01:00
import { collectionFolderClicked } from 'providers/ReduxStore/slices/collections';
2022-03-13 13:13:21 +01:00
import Dropdown from 'components/Dropdown';
2022-03-19 10:53:20 +01:00
import NewRequest from 'components/Sidebar/NewRequest';
import NewFolder from 'components/Sidebar/NewFolder';
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';
2022-03-19 10:53:20 +01:00
import { isItemARequest, isItemAFolder, itemIsOpenedInTabs } from 'utils/tabs';
2022-10-01 14:47:33 +02:00
import { doesRequestMatchSearchText, doesFolderHaveItemsMatchSearchText } 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 CollectionItem = ({item, collection, searchText}) => {
const tabs = useSelector((state) => state.tabs.tabs);
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
2022-03-22 13:48:20 +01:00
const isDragging = useSelector((state) => state.app.isDragging);
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-19 10:53:20 +01:00
const [newRequestModalOpen, setNewRequestModalOpen] = useState(false);
const [newFolderModalOpen, setNewFolderModalOpen] = useState(false);
2022-10-01 14:47:33 +02:00
const [itemIsCollapsed, setItemisCollapsed] = useState(item.collapsed);
useEffect(() => {
if (searchText && searchText.length) {
setItemisCollapsed(false);
} else {
setItemisCollapsed(item.collapsed);
}
}, [searchText, item]);
2022-03-18 20:35:43 +01:00
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({
2022-10-01 14:47:33 +02:00
'rotate-90': !itemIsCollapsed
2021-12-03 20:37:38 +01:00
});
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) => {
if(isItemARequest(item)) {
if(itemIsOpenedInTabs(item, tabs)) {
dispatch(focusTab({
uid: item.uid
}));
} else {
dispatch(addTab({
uid: item.uid,
collectionUid: collection.uid
}));
}
} else {
2022-03-19 10:53:20 +01:00
dispatch(collectionFolderClicked({
itemUid: item.uid,
collectionUid: collection.uid
}));
}
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 isFolder = isItemAFolder(item);
2022-03-22 13:48:20 +01:00
const className = classnames('flex flex-col w-full', {
'is-dragging': isDragging
});
2022-10-01 14:47:33 +02:00
if(searchText && searchText.length) {
if(isItemARequest(item)) {
if(!doesRequestMatchSearchText(item, searchText)) {
return null;
}
} else {
if (!doesFolderHaveItemsMatchSearchText(item, searchText)) {
return null;
};
}
}
2021-12-03 20:37:38 +01:00
return (
2022-03-22 13:48:20 +01:00
<StyledWrapper className={className}>
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)}/>}
2022-03-19 10:53:20 +01:00
{newRequestModalOpen && <NewRequest item={item} collection={collection} onClose={() => setNewRequestModalOpen(false)}/>}
{newFolderModalOpen && <NewFolder item={item} collection={collection} onClose={() => setNewFolderModalOpen(false)}/>}
2022-03-22 13:48:20 +01:00
<div className={itemRowClassName}>
2021-12-03 20:37:38 +01:00
<div className="flex items-center h-full w-full">
{indents && indents.length ? indents.map((i) => {
return (
<div
2022-03-22 13:48:20 +01:00
onClick={handleClick}
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-03-22 13:48:20 +01:00
minWidth: 16,
2022-01-01 17:56:56 +01:00
height: '100%'
2021-12-03 20:37:38 +01:00
}}
>
&nbsp;{/* Indent */}
</div>
);
}) : null}
<div
2022-03-22 13:48:20 +01:00
onClick={handleClick}
className="flex flex-grow items-center h-full overflow-hidden"
2021-12-03 20:37:38 +01:00
style = {{
paddingLeft: 8
}}
>
2022-03-22 13:48:20 +01:00
<div style={{width:16, minWidth: 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>
2022-03-22 13:48:20 +01:00
<div className="ml-1 flex items-center overflow-hidden">
<RequestMethod item={item}/>
2022-03-22 13:48:20 +01:00
<span className="item-name" title={item.name}>{item.name}</span>
</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();
2022-03-19 10:53:20 +01:00
setNewRequestModalOpen(true);
}}>
New Request
</div>
<div className="dropdown-item" onClick={(e) => {
dropdownTippyRef.current.hide();
setNewFolderModalOpen(true);
}}>
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>
2022-10-01 14:47:33 +02:00
{!itemIsCollapsed ? (
2021-12-03 20:37:38 +01:00
<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}
2022-10-01 14:47:33 +02:00
searchText={searchText}
2021-12-03 20:37:38 +01:00
/>
}) : null}
</div>
) : null}
</StyledWrapper>
);
};
export default CollectionItem;