From 92ee0dbf341fb65ef84307cdd67300e210c84b48 Mon Sep 17 00:00:00 2001 From: Naumov Alexey Date: Mon, 17 Jan 2022 00:50:34 +0300 Subject: [PATCH] feat(core-blocks): CB-17 use copy of tabList --- .../packages/core-ui/src/Tabs/TabsState.tsx | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/webapp/packages/core-ui/src/Tabs/TabsState.tsx b/webapp/packages/core-ui/src/Tabs/TabsState.tsx index d79952eba1..08648d7051 100644 --- a/webapp/packages/core-ui/src/Tabs/TabsState.tsx +++ b/webapp/packages/core-ui/src/Tabs/TabsState.tsx @@ -10,13 +10,13 @@ import { observer } from 'mobx-react-lite'; import { useEffect, useMemo, useState, useCallback } from 'react'; import { useTabState } from 'reakit/Tab'; +import { useObjectRef } from '@cloudbeaver/core-blocks'; import { Executor, ExecutorInterrupter, IExecutorHandler } from '@cloudbeaver/core-executor'; import { MetadataMap, MetadataValueGetter } from '@cloudbeaver/core-utils'; import type { ITabData, ITabsContainer } from './TabsContainer/ITabsContainer'; import { TabsContext, ITabsContext } from './TabsContext'; import type { TabDirection } from './TabsContext'; -import { useObjectRef } from '@cloudbeaver/core-blocks'; type ExtractContainerProps = T extends void ? Record : T; @@ -82,7 +82,7 @@ export const TabsState = observer(function TabsState>({ tabsState, container, state, - tabList, + tabList }); if (currentTabId !== undefined) { @@ -144,11 +144,11 @@ export const TabsState = observer(function TabsState>({ const closeAll = useCallback(async () => { if (dynamic.tabList) { - for (const tab of dynamic.tabList) { + for (const tab of dynamic.tabList.slice()) { await handleClose(tab); } } - }, [handleClose]) + }, [handleClose]); const closeAllToTheDirection = useCallback(async (tabId: string, direction: TabDirection) => { if (dynamic.tabList) { @@ -158,33 +158,22 @@ export const TabsState = observer(function TabsState>({ return; } - let start = index + 1; - let end = dynamic.tabList.length; + const tabs = direction === 'left' ? dynamic.tabList.slice(0, index) : dynamic.tabList.slice(index + 1); - if (direction === 'left') { - start = 0; - end = index; - } - - for (let i = start; i < end; i++) { - await handleClose(dynamic.tabList[i]); + for (const tab of tabs) { + await handleClose(tab); } } - }, [handleClose]) + }, [handleClose]); const closeOthers = useCallback(async (tabId: string) => { if (dynamic.tabList) { - const index = dynamic.tabList.indexOf(tabId); - - if (index > -1) { - for (const tab of dynamic.tabList) { - if (tab !== tabId) { - await handleClose(tab); - } - } + const tabs = dynamic.tabList.filter(tab => tab !== tabId); + for (const tab of tabs) { + await handleClose(tab); } } - }, [handleClose]) + }, [handleClose]); const getTabInfo = useCallback((tabId: string) => dynamic.container?.getTabInfo(tabId), []); const getTabState = useCallback(