forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert last used namespace local storage to userSettings
- Loading branch information
Showing
18 changed files
with
401 additions
and
329 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
frontend/packages/console-app/src/components/detect-namespace/DetectNamespace.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as React from 'react'; | ||
import { NamespaceContext, useValuesForNamespaceContext } from './namespace'; | ||
|
||
type DetectNamespaceProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const DetectNamespace: React.FC<DetectNamespaceProps> = ({ children }) => { | ||
const { namespace, setNamespace, loaded } = useValuesForNamespaceContext(); | ||
return loaded ? ( | ||
<NamespaceContext.Provider value={{ namespace, setNamespace }}> | ||
{children} | ||
</NamespaceContext.Provider> | ||
) : null; | ||
}; | ||
|
||
export default DetectNamespace; |
51 changes: 51 additions & 0 deletions
51
frontend/packages/console-app/src/components/detect-namespace/namespace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore: FIXME missing exports due to out-of-sync @types/react-redux version | ||
import { useDispatch } from 'react-redux'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { getNamespace } from '@console/internal/components/utils/link'; | ||
import { useUserSettingsCompatibility } from '@console/shared/src/hooks/useUserSettingsCompatibility'; | ||
import { setActiveNamespace } from '@console/internal/actions/ui'; | ||
import { ALL_NAMESPACES_KEY } from '@console/shared/src/constants'; | ||
|
||
type NamespaceContextType = { | ||
namespace?: string; | ||
setNamespace?: (ns: string) => void; | ||
}; | ||
|
||
const LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY = `bridge/last-namespace-name`; | ||
const LAST_NAMESPACE_NAME_USER_SETTINGS_KEY = 'console.lastNamespace'; | ||
|
||
export const NamespaceContext = React.createContext<NamespaceContextType>({}); | ||
|
||
export const useValuesForNamespaceContext = () => { | ||
const { pathname } = useLocation(); | ||
const urlNamespace = getNamespace(pathname); | ||
/** | ||
* [TODO]: use favorite namespace here if there is any | ||
* const [favorite] = useUserSettings() | ||
*/ | ||
const [namespace, setNs, loaded] = useUserSettingsCompatibility( | ||
LAST_NAMESPACE_NAME_USER_SETTINGS_KEY, | ||
LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY, | ||
urlNamespace || ALL_NAMESPACES_KEY, | ||
); | ||
const dispatchreduxAction = useDispatch(); | ||
const setNamespace = React.useCallback( | ||
(ns: string) => { | ||
dispatchreduxAction(setActiveNamespace(ns)); | ||
setNs(ns); | ||
}, | ||
[dispatchreduxAction, setNs], | ||
); | ||
|
||
React.useEffect(() => { | ||
if (loaded) { | ||
dispatchreduxAction(setActiveNamespace(urlNamespace || namespace || ALL_NAMESPACES_KEY)); | ||
} | ||
// only run this hook when namespace is loaded from user settings | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [loaded]); | ||
|
||
return { namespace: urlNamespace || namespace || ALL_NAMESPACES_KEY, setNamespace, loaded }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
frontend/packages/console-shared/src/hoc/withLastNamespace.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react'; | ||
import { useActiveNamespace } from '../hooks'; | ||
|
||
type WithLastNamespaceProps = { | ||
activeNamespace?: string; | ||
setActiveNamespace?: (v: string) => void; | ||
}; | ||
|
||
export const withLastNamespace = <Props extends WithLastNamespaceProps>( | ||
WrappedComponent: React.ComponentType<Props>, | ||
): React.FC<Omit<Props, keyof WithLastNamespaceProps>> => (props: Props) => { | ||
const [activeNamespace, setActiveNamespace] = useActiveNamespace(); | ||
return ( | ||
<WrappedComponent | ||
{...props} | ||
activeNamespace={activeNamespace} | ||
setActiveNamespace={setActiveNamespace} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
frontend/packages/console-shared/src/hooks/redux-selectors.ts
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
frontend/packages/console-shared/src/hooks/useActiveNamespace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useContext } from 'react'; | ||
import { NamespaceContext } from '@console/app/src/components/detect-namespace/namespace'; | ||
|
||
export const useActiveNamespace = (): [string, (ns: string) => void] => { | ||
const { namespace, setNamespace } = useContext(NamespaceContext); | ||
return [namespace, setNamespace]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.