Skip to content

Commit

Permalink
persist last viewed cluster id in k8s dashboard (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsladerman authored Sep 18, 2024
1 parent b5831dd commit 287a428
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
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

0 comments on commit 287a428

Please sign in to comment.