Skip to content

Commit f1391f7

Browse files
committed
chore: replace getLoginInfo with useUserEmail
1 parent 28a901b commit f1391f7

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

src/Common/Helper.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,6 @@ export function getCookie(sKey) {
175175
)
176176
}
177177

178-
export function getLoginInfo() {
179-
const argocdToken = getCookie(TOKEN_COOKIE_NAME)
180-
if (argocdToken) {
181-
const jwts = argocdToken.split('.')
182-
try {
183-
return JSON.parse(atob(jwts[1]))
184-
} catch (err) {
185-
console.error('error in setting user ', err)
186-
return null
187-
}
188-
}
189-
}
190-
191178
export function useForm(stateSchema, validationSchema = {}, callback) {
192179
const [state, setState] = useState(stateSchema)
193180
const [disable, setDisable] = useState(true)

src/Shared/Components/Header/PageHeader.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react'
22
import Tippy from '@tippyjs/react'
33
import './pageHeader.css'
44
import ReactGA from 'react-ga4'
5-
import { getLoginInfo, getRandomColor, TippyCustomized, TippyTheme } from '../../../Common'
5+
import { getRandomColor, TippyCustomized, TippyTheme } from '../../../Common'
66
import LogoutCard from '../LogoutCard'
77
import { setActionWithExpiry, handlePostHogEventUpdate } from './utils'
88
import { InstallationType, ServerInfo, PageHeaderType } from './types'
@@ -15,7 +15,7 @@ import { ReactComponent as QuestionFilled } from '../../../Assets/Icon/ic-help.s
1515
import { ReactComponent as Close } from '../../../Assets/Icon/ic-close.svg'
1616
import { ReactComponent as DropDownIcon } from '../../../Assets/Icon/ic-chevron-down.svg'
1717
import AnnouncementBanner from '../AnnouncementBanner/AnnouncementBanner'
18-
import { useMainContext } from '../../Providers'
18+
import { useMainContext, useUserEmail } from '../../Providers'
1919

2020
const PageHeader = ({
2121
headerName,
@@ -39,8 +39,7 @@ const PageHeader = ({
3939
useMainContext()
4040
const [showHelpCard, setShowHelpCard] = useState(false)
4141
const [showLogOutCard, setShowLogOutCard] = useState(false)
42-
const loginInfo = getLoginInfo()
43-
const email: string = loginInfo ? loginInfo.email || loginInfo.sub : ''
42+
const { email } = useUserEmail()
4443
const [currentServerInfo, setCurrentServerInfo] = useState<{ serverInfo: ServerInfo; fetchingServerInfo: boolean }>(
4544
{
4645
serverInfo: undefined,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { createContext, useContext, useMemo, useState } from 'react'
2+
import { UseUserEmailContextType } from './types'
3+
4+
const context = createContext<UseUserEmailContextType>(null)
5+
6+
export const useUserEmail = () => useContext(context)
7+
8+
export const UserEmailProvider: React.FC<{}> = ({ children }) => {
9+
const [email, setEmail] = useState<string>('')
10+
11+
const providerValue = useMemo(
12+
() => ({
13+
email,
14+
setEmail,
15+
}),
16+
[email],
17+
)
18+
19+
return <context.Provider value={providerValue}>{children}</context.Provider>
20+
}
21+
22+
// For using the provider in class based components
23+
export const withUserEmail = (Component: React.ComponentClass) => (props: object) => {
24+
const { email, setEmail } = useUserEmail()
25+
return <Component {...{ ...props, email, setEmail }} />
26+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './UserEmailProvider'
2+
export * from './types'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface UseUserEmailContextType {
2+
email: string
3+
setEmail: React.Dispatch<React.SetStateAction<string>>
4+
}

src/Shared/Providers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './MainContextProvider'
22
export * from './ImageSelectionUtility'
3+
export * from './UserEmailProvider'

0 commit comments

Comments
 (0)