import React from 'react'; import { findItemInCollection, findCollectionByUid } from 'utils/collections'; import { useDispatch, useSelector } from 'react-redux'; import classnames from 'classnames'; import StyledWrapper from './StyledWrapper'; import reverse from 'lodash/reverse'; import { getSpecialTabName, isSpecialTab, isItemAFolder } from 'utils/tabs'; import { selectCtrlTabAction } from 'providers/ReduxStore/slices/tabs'; export const tabStackToPopupTabs = (collections, ctrlTabStack) => { if (!collections) { return []; } return ctrlTabStack .map((tab) => { const collection = findCollectionByUid(collections, tab.collectionUid); return { collection, tab: findItemInCollection(collection, tab.uid) ?? tab }; }) .map(({ collection, tab }) => ({ tabName: isSpecialTab(tab) ? getSpecialTabName(tab.type) : tab.name, path: getItemPath(collection, tab), uid: tab.uid })); }; const getItemPath = (collection, item) => { if (isSpecialTab(item)) { return collection.name; } if (collection.items.find((i) => i.uid === item.uid)) { return collection.name; } return ( collection.name + '/' + collection.items.map((i) => (isItemAFolder(i) ? getItemPath(i, item) : null)).find(Boolean) ); }; // required in cases where we remove a tab from the stack but the user is still holding ctrl const tabStackToUniqueId = (ctrlTabStack) => ctrlTabStack.map((tab) => tab.uid).join('-'); export default function CtrlTabPopup() { const ctrlTabIndex = useSelector((state) => state.tabs.ctrlTabIndex); const ctrlTabStack = useSelector((state) => state.tabs.ctrlTabStack); const collections = useSelector((state) => state.collections.collections); const dispatch = useDispatch(); if (ctrlTabIndex === null) { return null; } const popupTabs = tabStackToPopupTabs(collections, ctrlTabStack); const currentTabbedTab = popupTabs.at(ctrlTabIndex); return (
{reverse(popupTabs).map((popupTab) => ( ))}
); }