All files / modules/85-sto-steps/components/STOSideNav STOSideNav.tsx

100% Statements 33/33
100% Branches 20/20
100% Functions 5/5
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111              1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 7x 7x 7x 7x 7x     7x 2x 2x 1x                   7x 1x 1x     7x                                       1x 14x 14x 14x 14x     14x   14x         1x 1x                                                            
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { Layout } from '@wings-software/uicore'
import { Tabs, Tab } from '@blueprintjs/core'
import routes from '@common/RouteDefinitions'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { SidebarLink } from '@common/navigation/SideNav/SideNav'
import { ProjectSelector } from '@projects-orgs/components/ProjectSelector/ProjectSelector'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useStrings } from 'framework/strings'
import css from './STOSideNav.module.scss'
 
export default function STOSideNav(): React.ReactElement {
  const { getString } = useStrings()
  const { accountId, projectIdentifier } = useParams<ProjectPathProps>()
  const { selectedProject } = useAppStore()
  const history = useHistory()
  const [isProjectMode, setIsProjectMode] = useState<boolean>(!!projectIdentifier)
  // Telemetry?
 
  const toProjectMode = () => {
    setIsProjectMode(true)
    if (selectedProject?.identifier) {
      history.push(
        routes.toSTOProjectOverview({
          accountId,
          projectIdentifier: selectedProject.identifier,
          orgIdentifier: selectedProject.orgIdentifier || /* istanbul ignore next */ ''
        })
      )
    }
  }
 
  const toAccountMode = () => {
    setIsProjectMode(false)
    history.push(routes.toSTOOverview({ accountId }))
  }
 
  return (
    <Layout.Vertical spacing="small">
      <Tabs id="navTab" selectedTabId={isProjectMode ? 'project' : 'account'} className={css.sideNavTabs}>
        <Tab id="account" title={getString('account')} onClickCapture={toAccountMode} panel={<TabPanel />} />
        <Tabs.Expander />
        <Tab
          id="project"
          title={getString('projectLabel')}
          onClickCapture={toProjectMode}
          panel={<TabPanel isProjectMode />}
        />
      </Tabs>
    </Layout.Vertical>
  )
}
 
interface TabPanelProps {
  isProjectMode?: boolean
}
 
const TabPanel: React.FC<TabPanelProps> = ({ isProjectMode }) => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { updateAppStore } = useAppStore()
  const history = useHistory()
  // Telemetry?
 
  const showLinks = !isProjectMode || projectIdentifier
 
  return (
    <Layout.Vertical spacing="small" className={isProjectMode ? css.projectPanel : undefined}>
      {isProjectMode && (
        <ProjectSelector
          onSelect={selectedProject => {
            updateAppStore({ selectedProject: selectedProject })
            history.push(
              routes.toSTOProjectOverview({
                accountId,
                projectIdentifier: selectedProject.identifier,
                orgIdentifier: selectedProject.orgIdentifier || /* istanbul ignore next */ ''
              })
            )
          }}
        />
      )}
 
      {showLinks && (
        <React.Fragment>
          {isProjectMode ? (
            <SidebarLink
              label={getString('overview')}
              to={routes.toSTOProjectOverview({
                accountId,
                projectIdentifier,
                orgIdentifier
              })}
            />
          ) : (
            <SidebarLink label={getString('overview')} to={routes.toSTOOverview({ accountId })} />
          )}
        </React.Fragment>
      )}
    </Layout.Vertical>
  )
}