import Head from 'next/head'; import { useRouter } from 'next/router'; import { FC, ReactElement, useEffect, useMemo, useState } from 'react'; import api from '../../lib/api'; import getQueryParam from '../../lib/getQueryParam'; import Grid from '../../components/Grid'; import handleAPIError from '../../lib/handleAPIError'; import Header from '../../components/Header'; import ManageFencePanel from '../../components/ManageFence'; import ManageManifestPanel from '../../components/ManageManifest'; import ManageUpsPanel from '../../components/ManageUps'; import { Panel } from '../../components/Panels'; import PrepareHostForm from '../../components/PrepareHostForm'; import PrepareNetworkForm from '../../components/PrepareNetworkForm'; import Spinner from '../../components/Spinner'; import Tab from '../../components/Tab'; import TabContent from '../../components/TabContent'; import Tabs from '../../components/Tabs'; import useIsFirstRender from '../../hooks/useIsFirstRender'; import useProtect from '../../hooks/useProtect'; import useProtectedState from '../../hooks/useProtectedState'; const TAB_ID_PREPARE_HOST = 'prepare-host'; const TAB_ID_PREPARE_NETWORK = 'prepare-network'; const TAB_ID_MANAGE_FENCE = 'manage-fence'; const TAB_ID_MANAGE_UPS = 'manage-ups'; const TAB_ID_MANAGE_MANIFEST = 'manage-manifest'; const MAP_TO_PAGE_TITLE: Record = { [TAB_ID_PREPARE_HOST]: 'Prepare Host', [TAB_ID_PREPARE_NETWORK]: 'Prepare Network', [TAB_ID_MANAGE_FENCE]: 'Manage Fence Devices', [TAB_ID_MANAGE_UPS]: 'Manage UPSes', [TAB_ID_MANAGE_MANIFEST]: 'Manage Manifests', }; const PAGE_TITLE_LOADING = 'Loading'; const STEP_CONTENT_GRID_COLUMNS = { md: 8, sm: 6, xs: 1 }; const STEP_CONTENT_GRID_CENTER_COLUMN = { md: 6, sm: 4, xs: 1 }; const PrepareHostTabContent: FC = () => ( , ...STEP_CONTENT_GRID_CENTER_COLUMN, }, }} /> ); const PrepareNetworkTabContent: FC = () => { const isFirstRender = useIsFirstRender(); const { protect } = useProtect(); const [hostOverviewList, setHostOverviewList] = useProtectedState< APIHostOverviewList | undefined >(undefined, protect); const [hostSubTabId, setHostSubTabId] = useState(false); const hostSubTabs = useMemo(() => { let result: ReactElement | undefined; if (hostOverviewList) { const hostOverviewPairs = Object.entries(hostOverviewList); result = ( { setHostSubTabId(newSubTabId); }} orientation="vertical" value={hostSubTabId} > {hostOverviewPairs.map(([hostUUID, { shortHostName }]) => ( ))} ); } else { result = ; } return result; }, [hostOverviewList, hostSubTabId]); if (isFirstRender) { api .get('/host', { params: { types: ['dr', 'node'] } }) .then(({ data }) => { setHostOverviewList(data); setHostSubTabId(Object.keys(data)[0]); }) .catch((error) => { handleAPIError(error); }); } return ( {hostSubTabs}, sm: 2, }, 'preparenetwork-center-column': { children: ( ), ...STEP_CONTENT_GRID_CENTER_COLUMN, }, }} /> ); }; const ManageFenceTabContent: FC = () => ( , ...STEP_CONTENT_GRID_CENTER_COLUMN, }, }} /> ); const ManageUpsTabContent: FC = () => ( , ...STEP_CONTENT_GRID_CENTER_COLUMN, }, }} /> ); const ManageManifestContent: FC = () => ( , ...STEP_CONTENT_GRID_CENTER_COLUMN, }, }} /> ); const ManageElement: FC = () => { const { isReady, query: { step: rawStep }, } = useRouter(); const [pageTabId, setPageTabId] = useState(false); const [pageTitle, setPageTitle] = useState(PAGE_TITLE_LOADING); useEffect(() => { if (isReady) { let step = getQueryParam(rawStep, { fallbackValue: TAB_ID_PREPARE_HOST, }); if (!MAP_TO_PAGE_TITLE[step]) { step = TAB_ID_PREPARE_HOST; } if (pageTitle === PAGE_TITLE_LOADING) { setPageTitle(MAP_TO_PAGE_TITLE[step]); } if (!pageTabId) { setPageTabId(step); } } }, [isReady, pageTabId, pageTitle, rawStep]); return ( <> {pageTitle}
{ setPageTabId(newTabId); setPageTitle(MAP_TO_PAGE_TITLE[newTabId]); }} orientation={{ xs: 'vertical', sm: 'horizontal' }} value={pageTabId} > ); }; export default ManageElement;