-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cluster in url - Newest cleaned up code #7
base: multi-cluster
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,17 @@ 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 { setActiveCluster } from '@console/internal/actions/ui'; | ||
import { useLocation } from 'react-router-dom'; | ||
import isMultiClusterEnabled from '@console/app/src/utils/isMultiClusterEnabled'; | ||
import { setActiveCluster, formatNamespaceRoute } from '@console/internal/actions/ui'; | ||
import { getCluster } from '@console/internal/components/utils/link'; | ||
import { history } from '@console/internal/components/utils/router'; | ||
// import { useActiveNamespace } from '@console/shared'; | ||
import { LAST_CLUSTER_USER_SETTINGS_KEY } from '@console/shared/src/constants'; | ||
import { useUserSettings } from '@console/shared/src/hooks/useUserSettings'; | ||
import store from '../../../../../public/redux'; | ||
|
||
export const multiClusterRoutePrefixes = ['/k8s/all-namespaces', '/k8s/cluster', '/k8s/ns']; | ||
|
||
type ClusterContextType = { | ||
cluster?: string; | ||
|
@@ -28,14 +36,39 @@ export const useValuesForClusterContext = () => { | |
[dispatch, setLastCluster], | ||
); | ||
|
||
// const [activeNamespace] = useActiveNamespace(); | ||
const urlCluster = getCluster(useLocation().pathname); | ||
React.useEffect(() => { | ||
// TODO: Detect cluster from URL. | ||
if (lastClusterLoaded && lastCluster) { | ||
if (urlCluster) { | ||
setLastCluster(urlCluster); | ||
dispatch(setActiveCluster(urlCluster)); | ||
} else if (lastClusterLoaded && lastCluster) { | ||
dispatch(setActiveCluster(lastCluster)); | ||
} | ||
// Only run this hook after last cluster is loaded. | ||
|
||
if ( | ||
isMultiClusterEnabled() && | ||
lastClusterLoaded && | ||
lastCluster && | ||
!urlCluster && | ||
multiClusterRoutePrefixes.some((pattern) => window.location.pathname.startsWith(pattern)) | ||
) { | ||
const activeNamespace = store.getState().UI.get('activeNamespace'); | ||
const newPath = formatNamespaceRoute( | ||
activeNamespace, | ||
window.location.pathname, | ||
window.location, | ||
false, | ||
lastCluster, | ||
); | ||
|
||
if (newPath !== window.location.pathname) { | ||
history.pushPath(newPath); | ||
} | ||
} | ||
// Only run this hook after last cluster is loaded or window path changes. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [lastClusterLoaded]); | ||
}, [lastClusterLoaded, urlCluster, window.location.pathname]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting |
||
|
||
return { | ||
cluster: lastCluster, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
ALL_NAMESPACES_KEY, | ||
LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY, | ||
} from '@console/shared/src/constants'; | ||
// import { multiClusterRoutePrefixes } from '@console/app/src/components/detect-cluster/cluster' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the import caused most of the tests to fail. Unsure of cause, so the prefixes are duplicated in the code below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this again. When you import the multiClusterRoutePrefixes it causes this test suite to fail: Each test fails with this message:
Seems kind of crazy pants to me, but I tested it numerous times and it fails without fail (hahaha) when using the import. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stack trace for failed test:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is most likely an issue with a jest.mock in the user settings hook unit test. |
||
import { K8sResourceKind, PodKind, NodeKind } from '../module/k8s'; | ||
import { allModels } from '../module/k8s/k8s-models'; | ||
import { detectFeatures, clearSSARFlags } from './features'; | ||
|
@@ -152,10 +153,23 @@ export const formatNamespaceRoute = ( | |
originalPath, | ||
location?, | ||
forceList?: boolean, | ||
activeCluster?: string, | ||
) => { | ||
let path = originalPath.substr(window.SERVER_FLAGS.basePath.length); | ||
|
||
let parts = path.split('/').filter((p) => p); | ||
|
||
if (parts[0] === 'cluster') { | ||
// The url pattern that includes the cluster name starts with /cluster/:clusterName | ||
parts.splice(0, 2); | ||
} | ||
|
||
const multiClusterRoutePrefixes = ['/k8s/all-namespaces', '/k8s/cluster', '/k8s/ns']; | ||
const clusterPathPart = | ||
activeCluster && multiClusterRoutePrefixes.includes(`/${parts[0]}/${parts[1]}`) | ||
? `/cluster/${activeCluster}` | ||
: ''; | ||
|
||
const prefix = parts.shift(); | ||
|
||
let previousNS; | ||
|
@@ -168,7 +182,7 @@ export const formatNamespaceRoute = ( | |
} | ||
|
||
if (!previousNS) { | ||
return originalPath; | ||
return `${clusterPathPart}${originalPath}`; | ||
} | ||
|
||
if ( | ||
|
@@ -193,7 +207,7 @@ export const formatNamespaceRoute = ( | |
path += `${location.search}${location.hash}`; | ||
} | ||
|
||
return path; | ||
return `${clusterPathPart}${path}`; | ||
}; | ||
|
||
export const setCurrentLocation = (location: string) => | ||
|
@@ -211,6 +225,7 @@ export const setActiveNamespace = (namespace: string = '') => { | |
// make it noop when new active namespace is the same | ||
// otherwise users will get page refresh and cry about | ||
// broken direct links and bookmarks | ||
|
||
if (namespace !== getActiveNamespace()) { | ||
const oldPath = window.location.pathname; | ||
const newPath = formatNamespaceRoute(namespace, oldPath, window.location); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the store isn't the best option, but there were weird bugs when using
useActiveNamespace
. The bug is most notable when going to the pods list page for all projects and then clicking on a pod. Instead of going to the details page, the user was routed back to the list page.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you see the same behavior is you use
useSelector
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I thought about that last night. I'll check it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First changed the path to the store to
import store from '@console/internal/redux';
so at least it isn't relative.So neither the
useSelector
nor theactiveNamespace
fromuseActiveNamespace
worked. Here is what I think is going on. We need to know the current Namespace inside an useEffect hook, but sinceuseSelector
anduseActiveNamespace
are hooks and React gets grumppy when trying to put a hook in hook, I need to put them outside of the useEffect block. I'm thinking that when I do that the namespace from the hook is a step behind when calling the useEffect.So not sure what to do.