@@ -5,12 +5,12 @@ import Checkbox from '@mui/material/Checkbox';
55import { useTheme } from '@mui/material/styles' ;
66import TextField from '@mui/material/TextField' ;
77import Typography from '@mui/material/Typography' ;
8- import React , { useMemo } from 'react' ;
8+ import React , { useEffect , useMemo } from 'react' ;
99import { useTranslation } from 'react-i18next' ;
1010import { useDispatch } from 'react-redux' ;
1111import { useHistory , useLocation } from 'react-router-dom' ;
1212import helpers , { addQuery } from '../../helpers' ;
13- import { useCluster } from '../../lib/k8s' ;
13+ import { useCluster , useClustersConf } from '../../lib/k8s' ;
1414import Namespace from '../../lib/k8s/namespace' ;
1515import { setNamespaceFilter } from '../../redux/filterSlice' ;
1616import { useTypedSelector } from '../../redux/reducers/reducers' ;
@@ -152,10 +152,47 @@ export function NamespacesAutocomplete() {
152152 ) ;
153153}
154154
155+ /**
156+ * This hook will try to select a namespace in a specific case
157+ *
158+ * If we failed to load namespaces it might be because the user
159+ * doesn't have access to list all the namespaces but still has
160+ * access to a specific namespace
161+ *
162+ * Sometimes in the kubeconfig there will be a default namespace set
163+ * which we can try to use as a fallback
164+ */
165+ const useDefaultNamespaceFallback = (
166+ namespacesList : Namespace [ ] | null ,
167+ isNamespaceError : boolean
168+ ) => {
169+ const selectedNamespaces = useTypedSelector ( state => state . filter . namespaces ) ;
170+ const allClustersConfigs = useClustersConf ( ) ;
171+ const currentCluster = useCluster ( ) ;
172+ const dispatch = useDispatch ( ) ;
173+
174+ useEffect ( ( ) => {
175+ if (
176+ currentCluster &&
177+ allClustersConfigs &&
178+ isNamespaceError &&
179+ ( ! namespacesList || namespacesList ?. length === 0 ) &&
180+ selectedNamespaces . size === 0
181+ ) {
182+ const defaultNamespaceFromKubeconfig =
183+ allClustersConfigs [ currentCluster ] ?. meta_data . namespace ;
184+
185+ if ( defaultNamespaceFromKubeconfig ) {
186+ dispatch ( setNamespaceFilter ( [ defaultNamespaceFromKubeconfig ] ) ) ;
187+ }
188+ }
189+ } , [ namespacesList , isNamespaceError , currentCluster ] ) ;
190+ } ;
191+
155192function NamespacesFromClusterAutocomplete (
156193 props : Omit < PureNamespacesAutocompleteProps , 'namespaceNames' >
157194) {
158- const [ namespacesList ] = Namespace . useList ( ) ;
195+ const [ namespacesList , error ] = Namespace . useList ( ) ;
159196 const namespaceNames = useMemo (
160197 ( ) =>
161198 namespacesList
@@ -165,5 +202,7 @@ function NamespacesFromClusterAutocomplete(
165202 [ namespacesList ]
166203 ) ;
167204
205+ useDefaultNamespaceFallback ( namespacesList , Boolean ( error ) ) ;
206+
168207 return < PureNamespacesAutocomplete namespaceNames = { namespaceNames } { ...props } /> ;
169208}
0 commit comments