Skip to content

Commit 6ecf47f

Browse files
authored
Merge pull request #152 from devtron-labs/chore/user-email-provider
chore: replace getLoginInfo with useUserEmail
2 parents 725dbb4 + deef746 commit 6ecf47f

File tree

8 files changed

+419
-476
lines changed

8 files changed

+419
-476
lines changed

package-lock.json

Lines changed: 382 additions & 458 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.0.87",
3+
"version": "0.0.88",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Helper.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,6 @@ export function getCookie(sKey) {
194194
)
195195
}
196196

197-
export function getLoginInfo() {
198-
const argocdToken = getCookie(TOKEN_COOKIE_NAME)
199-
if (argocdToken) {
200-
const jwts = argocdToken.split('.')
201-
try {
202-
return JSON.parse(atob(jwts[1]))
203-
} catch (err) {
204-
console.error('error in setting user ', err)
205-
return null
206-
}
207-
}
208-
}
209-
210197
export function useForm(stateSchema, validationSchema = {}, callback) {
211198
const [state, setState] = useState(stateSchema)
212199
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
@@ -18,7 +18,7 @@ import { useEffect, useState } from 'react'
1818
import Tippy from '@tippyjs/react'
1919
import './pageHeader.css'
2020
import ReactGA from 'react-ga4'
21-
import { getLoginInfo, getRandomColor } from '../../../Common'
21+
import { getRandomColor } from '../../../Common'
2222
import LogoutCard from '../LogoutCard'
2323
import { setActionWithExpiry, handlePostHogEventUpdate } from './utils'
2424
import { InstallationType, ServerInfo, PageHeaderType } from './types'
@@ -30,7 +30,7 @@ import { ReactComponent as Question } from '../../../Assets/Icon/ic-help-outline
3030
import { ReactComponent as Close } from '../../../Assets/Icon/ic-close.svg'
3131
import { ReactComponent as DropDownIcon } from '../../../Assets/Icon/ic-chevron-down.svg'
3232
import AnnouncementBanner from '../AnnouncementBanner/AnnouncementBanner'
33-
import { useMainContext } from '../../Providers'
33+
import { useMainContext, useUserEmail } from '../../Providers'
3434
import { InfoIconTippy } from '../InfoIconTippy'
3535

3636
const PageHeader = ({
@@ -53,8 +53,7 @@ const PageHeader = ({
5353
tippyProps || {}
5454
const [showHelpCard, setShowHelpCard] = useState(false)
5555
const [showLogOutCard, setShowLogOutCard] = useState(false)
56-
const loginInfo = getLoginInfo()
57-
const email: string = loginInfo ? loginInfo.email || loginInfo.sub : ''
56+
const { email } = useUserEmail()
5857
const [currentServerInfo, setCurrentServerInfo] = useState<{ serverInfo: ServerInfo; fetchingServerInfo: boolean }>(
5958
{
6059
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
@@ -16,3 +16,4 @@
1616

1717
export * from './MainContextProvider'
1818
export * from './ImageSelectionUtility'
19+
export * from './UserEmailProvider'

0 commit comments

Comments
 (0)