Skip to content
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

persist last viewed cluster id in k8s dashboard #1399

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions assets/src/components/kubernetes/Cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import LoadingIndicator from '../utils/LoadingIndicator'
import { GqlError } from '../utils/Alert'
import { useProjectId } from '../contexts/ProjectsContext'

import { LAST_SELECTED_CLUSTER_KEY } from './Navigation'

type ClusterContextT = {
clusters: KubernetesClusterFragment[]
refetch?: Nullable<() => void>
Expand Down Expand Up @@ -101,6 +103,8 @@ export default function Cluster() {
[data?.clusters]
)

const hasCurrentClusterId = clusters.some(({ id }) => id === clusterId)

const cluster = useMemo(
() => clusters.find(({ id }) => id === clusterId),
[clusterId, clusters]
Expand All @@ -125,16 +129,26 @@ export default function Cluster() {
)

useEffect(() => {
if (!isEmpty(clusters) && !clusterId) {
if (!isEmpty(clusters) && !hasCurrentClusterId) {
const lastSelectedClusterId = sessionStorage.getItem(
LAST_SELECTED_CLUSTER_KEY
)
const lastSelectedClusterExists = clusters.some(
({ id }) => id === lastSelectedClusterId
)
const mgmtCluster = clusters.find(({ self }) => !!self)

if (mgmtCluster) {
navigate(getWorkloadsAbsPath(mgmtCluster.id) + search, {
replace: true,
})
}
const redirectId = lastSelectedClusterExists
? lastSelectedClusterId
: mgmtCluster
? mgmtCluster?.id
: clusters[0].id

navigate(getWorkloadsAbsPath(redirectId) + search, {
replace: true,
})
}
}, [clusters, navigate, search, clusterId])
}, [clusters, navigate, search, clusterId, hasCurrentClusterId])

if (error)
return (
Expand Down
5 changes: 4 additions & 1 deletion assets/src/components/kubernetes/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {

export const NAMESPACE_PARAM = 'namespace'
export const FILTER_PARAM = 'filter'
export const LAST_SELECTED_CLUSTER_KEY = 'plural-last-selected-cluster'

const directory: Directory = [
{ path: WORKLOADS_REL_PATH, label: 'Workloads' },
Expand Down Expand Up @@ -63,6 +64,8 @@ export default function Navigation() {
const pageHeaderContext = useMemo(() => ({ setHeaderContent }), [])

useLayoutEffect(() => {
if (clusterId) sessionStorage.setItem(LAST_SELECTED_CLUSTER_KEY, clusterId)

const newParams = new URLSearchParams()

if (!isEmpty(dataSelect.filter))
Expand All @@ -74,7 +77,7 @@ export default function Navigation() {
if (newParams.toString() !== params.toString()) setParams(newParams)

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataSelect, pathname])
}, [dataSelect, pathname, clusterId])

return (
<ResponsiveLayoutPage>
Expand Down
Loading