All files / modules/35-user-profile/navigation/UserNav UserNav.tsx

96.55% Statements 28/29
80% Branches 8/10
100% Functions 2/2
96.55% Lines 28/29

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              1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 3x 3x 3x       3x 3x   3x 2x     2x 2x 2x   1x   1x   2x           3x                                            
/*
 * Copyright 2022 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 from 'react'
import { useParams, useHistory } from 'react-router-dom'
import { Layout, Text } from '@wings-software/uicore'
import { get } from 'lodash-es'
 
import routes from '@common/RouteDefinitions'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { SidebarLink } from '@common/navigation/SideNav/SideNav'
import { useStrings } from 'framework/strings'
import { useLogout1 } from 'services/portal'
import AppStorage from 'framework/utils/AppStorage'
import { useToaster } from '@common/exports'
import { getLoginPageURL } from 'framework/utils/SessionUtils'
import { returnUrlParams } from '@common/utils/routeUtils'
import css from './UserNav.module.scss'
 
export default function UserNav(): React.ReactElement {
  const { accountId } = useParams<AccountPathProps>()
  const history = useHistory()
  const { mutate: logout } = useLogout1({
    userId: AppStorage.get('uuid'),
    requestOptions: { headers: { 'content-type': 'application/json' } }
  })
  const { getString } = useStrings()
  const { showError } = useToaster()
 
  const signOut = async (): Promise<void> => {
    try {
      // BE is not publishing correct types for logout response yet
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const response: any = await logout()
      AppStorage.clear()
      if (response?.resource?.logoutUrl) {
        // if BE returns a logoutUrl, redirect there. Used by some customers in onprem
        window.location.href = response.resource.logoutUrl
      } else {
        history.push({ pathname: routes.toRedirect(), search: returnUrlParams(getLoginPageURL({})) })
      }
      return
    } catch (err) {
      showError(get(err, 'responseMessages[0].message', getString('somethingWentWrong')))
    }
  }
 
  return (
    <div>
      <Layout.Vertical margin={{ top: 'xxxlarge' }}>
        <SidebarLink exact label={getString('profile')} to={routes.toUserProfile({ accountId })} />
        {/* Enable when Ready */}
        {/* <SidebarLink label={getString('preferences')} to={routes.toUserPreferences({ accountId })} /> */}
      </Layout.Vertical>
      <div className={css.signout}>
        <Text
          font={{ weight: 'semi-bold' }}
          icon="log-out"
          iconProps={{ size: 16, padding: { right: 'small' } }}
          onClick={signOut}
          className={css.text}
          data-testid="signout-link"
        >
          {getString('signOut')}
        </Text>
      </div>
    </div>
  )
}