@openshift-console/dynamic-plugin-sdk
Version:
Provides core APIs, types and utilities used by dynamic plugins at runtime.
26 lines (25 loc) • 1.3 kB
JavaScript
import { useSelector } from 'react-redux';
import { getGroupVersionKindForReference, transformGroupVersionKindToReference } from '../k8s-ref';
export const getK8sModel = (k8s, k8sGroupVersionKind) => {
const kindReference = transformGroupVersionKindToReference(k8sGroupVersionKind);
return kindReference
? k8s.getIn(['RESOURCES', 'models', kindReference]) ??
k8s.getIn(['RESOURCES', 'models', getGroupVersionKindForReference(kindReference).kind])
: undefined;
};
/**
* Hook that retrieves the k8s model for provided K8sGroupVersionKind from redux.
* @param groupVersionKind group, version, kind of k8s resource `K8sGroupVersionKind` is preferred alternatively can pass reference for group, version, kind which is deprecated i.e `group~version~kind` `K8sResourceKindReference`.
* @returns An array with the first item as k8s model and second item as inFlight status
* @example
* ```ts
* const Component: React.FC = () => {
* const [model, inFlight] = useK8sModel({ group: 'app'; version: 'v1'; kind: 'Deployment' });
* return ...
* }
* ```
*/
export const useK8sModel = (k8sGroupVersionKind) => [
useSelector(({ k8s }) => getK8sModel(k8s, k8sGroupVersionKind)),
useSelector(({ k8s }) => k8s.getIn(['RESOURCES', 'inFlight']) ?? false),
];