@@ -47,7 +47,6 @@ import { TableColumn } from '@console/internal/module/k8s';
4747import { GetDataViewRows , ResourceFilters } from '@console/app/src/components/data-view/types' ;
4848import { tableFilters } from '../factory/table-filters' ;
4949import { ButtonBar } from '../utils/button-bar' ;
50- import { Firehose } from '../utils/firehose' ;
5150import { getQueryArgument } from '../utils/router' ;
5251import { kindObj } from '../utils/inject' ;
5352import type { ListDropdownProps } from '../utils/list-dropdown' ;
@@ -57,7 +56,7 @@ import { ResourceName } from '../utils/resource-icon';
5756import { StatusBox , LoadingBox } from '../utils/status-box' ;
5857import { useAccessReview } from '../utils/rbac' ;
5958import { flagPending } from '../../reducers/features' ;
60- import { useK8sWatchResources } from '../utils/k8s-watch-hook' ;
59+ import { useK8sWatchResource , useK8sWatchResources } from '../utils/k8s-watch-hook' ;
6160
6261// Split each binding into one row per subject
6362export const flatten = ( resources ) : BindingKind [ ] =>
@@ -185,18 +184,19 @@ const bindingType = (binding: BindingKind) => {
185184 if ( ! binding ) {
186185 return undefined ;
187186 }
188- if ( binding . roleRef . name . startsWith ( 'system:' ) ) {
187+ if ( binding . roleRef ? .name ? .startsWith ( 'system:' ) ) {
189188 return 'system' ;
190189 }
191- return binding . metadata . namespace ? 'namespace' : 'cluster' ;
190+ return binding . metadata ? .namespace ? 'namespace' : 'cluster' ;
192191} ;
193192
194193const getDataViewRows : GetDataViewRows < BindingKind > = ( data , columns ) => {
195- return data . map ( ( { obj : binding } ) => {
194+ return data . map ( ( row ) => {
195+ const binding = row . obj ;
196196 const rowCells = {
197197 [ tableColumnInfo [ 0 ] . id ] : {
198198 cell : < BindingName binding = { binding } /> ,
199- props : getNameCellProps ( binding . metadata . name ) ,
199+ props : getNameCellProps ( binding . metadata ? .name ) ,
200200 } ,
201201 [ tableColumnInfo [ 1 ] . id ] : {
202202 cell : < RoleLink binding = { binding } /> ,
@@ -208,7 +208,7 @@ const getDataViewRows: GetDataViewRows<BindingKind> = (data, columns) => {
208208 cell : binding . subject . name ,
209209 } ,
210210 [ tableColumnInfo [ 4 ] . id ] : {
211- cell : binding . metadata . namespace ? (
211+ cell : binding . metadata ? .namespace ? (
212212 < ResourceLink kind = "Namespace" name = { binding . metadata . namespace } />
213213 ) : (
214214 i18next . t ( 'public~All namespaces' )
@@ -360,11 +360,18 @@ export const RoleBindingsPage: FC<RoleBindingsPageProps> = ({
360360 } ,
361361 } ) ;
362362
363- const data = useMemo ( ( ) => flatten ( resources ) , [ resources ] ) ;
364-
365- const loaded = Object . values ( resources )
366- . filter ( ( r ) => ! r . loadError )
367- . every ( ( r ) => r . loaded ) ;
363+ const data = useMemo ( ( ) => flatten ( resources ) , [
364+ resources . RoleBinding . data ,
365+ resources . ClusterRoleBinding . data ,
366+ ] ) ;
367+
368+ const loaded = useMemo (
369+ ( ) =>
370+ Object . values ( resources )
371+ . filter ( ( r ) => ! r . loadError )
372+ . every ( ( r ) => r . loaded ) ,
373+ [ resources ] ,
374+ ) ;
368375
369376 return (
370377 < >
@@ -784,52 +791,79 @@ const getSubjectIndex = () => {
784791} ;
785792
786793const BindingLoadingWrapper : FC < BindingLoadingWrapperProps > = ( props ) => {
794+ const { obj, loaded, loadError, fixedKeys } = props ;
787795 const [ , setActiveNamespace ] = useActiveNamespace ( ) ;
796+
797+ if ( ! loaded ) {
798+ return < LoadingBox /> ;
799+ }
800+
801+ if ( loadError ) {
802+ return < StatusBox data = { obj } loaded = { loaded } loadError = { loadError } /> ;
803+ }
804+
805+ if ( ! obj || _ . isEmpty ( obj ) ) {
806+ return < StatusBox data = { obj } loaded = { loaded } loadError = { loadError } /> ;
807+ }
808+
788809 const fixed : { [ key : string ] : any } = { } ;
789- _ . each ( props . fixedKeys , ( k ) => ( fixed [ k ] = _ . get ( props . obj . data , k ) ) ) ;
810+ fixedKeys . forEach ( ( k ) => ( fixed [ k ] = obj ?. [ k ] ) ) ;
811+
790812 return (
791- < StatusBox { ...props . obj } >
792- < BaseEditRoleBinding
793- { ...props }
794- setActiveNamespace = { setActiveNamespace }
795- fixed = { fixed }
796- obj = { props . obj . data }
797- />
798- </ StatusBox >
813+ < BaseEditRoleBinding
814+ { ...props }
815+ setActiveNamespace = { setActiveNamespace }
816+ fixed = { fixed }
817+ obj = { obj }
818+ />
799819 ) ;
800820} ;
801821
802822export const EditRoleBinding : FC < EditRoleBindingProps > = ( { kind } ) => {
803823 const { t } = useTranslation ( ) ;
804824 const params = useParams ( ) ;
825+
826+ const [ obj , loaded , loadError ] = useK8sWatchResource < RoleBindingKind | ClusterRoleBindingKind > ( {
827+ kind,
828+ name : params . name ,
829+ namespace : params . ns ,
830+ isList : false ,
831+ } ) ;
832+
805833 return (
806- < Firehose
807- resources = { [ { kind, name : params . name , namespace : params . ns , isList : false , prop : 'obj' } ] }
808- >
809- < BindingLoadingWrapper
810- fixedKeys = { [ 'kind' , 'metadata' , 'roleRef' ] }
811- subjectIndex = { getSubjectIndex ( ) }
812- titleVerbAndKind = { t ( 'public~Edit RoleBinding' ) }
813- saveButtonText = { t ( 'public~Save' ) }
814- />
815- </ Firehose >
834+ < BindingLoadingWrapper
835+ obj = { obj }
836+ loaded = { loaded }
837+ loadError = { loadError }
838+ fixedKeys = { [ 'kind' , 'metadata' , 'roleRef' ] }
839+ subjectIndex = { getSubjectIndex ( ) }
840+ titleVerbAndKind = { t ( 'public~Edit RoleBinding' ) }
841+ saveButtonText = { t ( 'public~Save' ) }
842+ />
816843 ) ;
817844} ;
818845
819846export const CopyRoleBinding : FC < EditRoleBindingProps > = ( { kind } ) => {
820847 const { t } = useTranslation ( ) ;
821848 const params = useParams ( ) ;
849+
850+ const [ obj , loaded , loadError ] = useK8sWatchResource < RoleBindingKind | ClusterRoleBindingKind > ( {
851+ kind,
852+ name : params . name ,
853+ namespace : params . ns ,
854+ isList : false ,
855+ } ) ;
856+
822857 return (
823- < Firehose
824- resources = { [ { kind, name : params . name , namespace : params . ns , isList : false , prop : 'obj' } ] }
825- >
826- < BindingLoadingWrapper
827- fixedKeys = { [ 'kind' ] }
828- subjectIndex = { getSubjectIndex ( ) }
829- isCreate = { true }
830- titleVerbAndKind = { t ( 'public~Duplicate RoleBinding' ) }
831- />
832- </ Firehose >
858+ < BindingLoadingWrapper
859+ obj = { obj }
860+ loaded = { loaded }
861+ loadError = { loadError }
862+ fixedKeys = { [ 'kind' ] }
863+ subjectIndex = { getSubjectIndex ( ) }
864+ isCreate = { true }
865+ titleVerbAndKind = { t ( 'public~Duplicate RoleBinding' ) }
866+ />
833867 ) ;
834868} ;
835869
@@ -881,9 +915,9 @@ type BindingLoadingWrapperProps = {
881915 titleVerbAndKind : string ;
882916 saveButtonText ?: string ;
883917 isCreate ?: boolean ;
884- obj ?: {
885- data : RoleBindingKind | ClusterRoleBindingKind ;
886- } ;
918+ obj ?: RoleBindingKind | ClusterRoleBindingKind ;
919+ loaded : boolean ;
920+ loadError : any ;
887921} ;
888922
889923type EditRoleBindingProps = {
0 commit comments