From 3b2a6ada12210ba15a716d3e302884565dc4b998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Tue, 9 Jul 2024 17:02:50 +0200 Subject: [PATCH] refactor: change TAB_LIST to enum and remove currentTab state --- .../LibraryAuthoringPage.tsx | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/library-authoring/LibraryAuthoringPage.tsx b/src/library-authoring/LibraryAuthoringPage.tsx index f45ef11e20..7f16432778 100644 --- a/src/library-authoring/LibraryAuthoringPage.tsx +++ b/src/library-authoring/LibraryAuthoringPage.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useState } from 'react'; import { StudioFooter } from '@edx/frontend-component-footer'; import { useIntl } from '@edx/frontend-platform/i18n'; import { @@ -19,11 +19,11 @@ import LibraryHome from './LibraryHome'; import { useContentLibrary } from './data/apiHook'; import messages from './messages'; -const TAB_LIST = { - home: '', - components: 'components', - collections: 'collections', -}; +enum TabList { + home = '', + components = 'components', + collections = 'collections', +} const SubHeaderTitle = ({ title }: { title: string }) => { const intl = useIntl(); @@ -44,21 +44,14 @@ const LibraryAuthoringPage = () => { const intl = useIntl(); const location = useLocation(); const navigate = useNavigate(); - const [tabKey, setTabKey] = useState(TAB_LIST.home); const [searchKeywords, setSearchKeywords] = useState(''); const { libraryId } = useParams(); const { data: libraryData, isLoading } = useContentLibrary(libraryId); - useEffect(() => { - const currentPath = location.pathname.split('/').pop(); - if (currentPath && Object.values(TAB_LIST).includes(currentPath)) { - setTabKey(currentPath); - } else { - setTabKey(TAB_LIST.home); - } - }, [location]); + const currentPath = location.pathname.split('/').pop(); + const activeKey = (currentPath && currentPath in TabList) ? TabList[currentPath] : TabList.home; if (isLoading) { return ; @@ -69,7 +62,7 @@ const LibraryAuthoringPage = () => { } const handleTabChange = (key: string) => { - setTabKey(key); + // setTabKey(key); navigate(key); }; @@ -96,25 +89,25 @@ const LibraryAuthoringPage = () => { /> - - - + + + } /> } /> } />