1- import { FC , useMemo , useEffect , useRef } from 'react' ;
1+ import { FC , useEffect , useRef } from 'react' ;
22
33import { YamlViewer , YamlViewerProps } from './YamlViewer.tsx' ;
4- import { useApiResource } from '../../lib/api/useApiResource.ts' ;
54import Loading from '../Shared/Loading.tsx' ;
6- import { CustomResourceDefinition } from '../../types/customResourceDefinition.ts' ;
7- import openapiSchemaToJsonSchema from '@openapi-contrib/openapi-schema-to-json-schema' ;
8-
9- import { getCustomResourceDefinitionPluralName } from '../../utils/getPluralName.ts' ;
5+ import { useCustomResourceDefinitionQuery } from '../../hooks/useCustomResourceDefinitionQuery.ts' ;
106import { useToast } from '../../context/ToastContext.tsx' ;
117import { useTranslation } from 'react-i18next' ;
128
@@ -27,22 +23,15 @@ export const YamlResourceEditorSchemaLoader: FC<YamlViewerSchemaLoaderProps> = (
2723
2824 kind,
2925} ) => {
30- const customResourceDefinitionName = getCustomResourceDefinitionPluralName ( kind ) ;
31- const { show } = useToast ( ) ;
3226 const hasShownErrorRef = useRef ( false ) ;
3327
34- const {
35- data : crdData ,
36- isLoading,
37- error,
38- } = useApiResource < CustomResourceDefinition > (
39- {
40- path : `/apis/apiextensions.k8s.io/v1/customresourcedefinitions/${ customResourceDefinitionName } .${ apiGroupName } ` ,
41- } ,
42- undefined ,
43- undefined ,
44- ! customResourceDefinitionName ,
45- ) ;
28+ const { schema, isLoading, error } = useCustomResourceDefinitionQuery ( {
29+ kind,
30+ apiGroupName,
31+ apiVersion,
32+ } ) ;
33+
34+ const { show } = useToast ( ) ;
4635
4736 const { t } = useTranslation ( ) ;
4837
@@ -53,21 +42,9 @@ export const YamlResourceEditorSchemaLoader: FC<YamlViewerSchemaLoaderProps> = (
5342 }
5443 } , [ error , show , t ] ) ;
5544
56- const schema =
57- crdData ?. spec . versions ?. find ( ( { name } ) => name === apiVersion ) ?. schema . openAPIV3Schema ??
58- crdData ?. spec . versions ?. [ 0 ] . schema . openAPIV3Schema ;
59- const editorInstanceSchema = useMemo ( ( ) => ( schema ? openapiSchemaToJsonSchema ( schema ) : undefined ) , [ schema ] ) ;
60-
61- if ( customResourceDefinitionName && isLoading ) {
45+ if ( kind && isLoading ) {
6246 return < Loading /> ;
6347 }
64- return (
65- < YamlViewer
66- schema = { editorInstanceSchema }
67- yamlString = { yamlString }
68- filename = { filename }
69- isEdit = { isEdit }
70- onApply = { onApply }
71- />
72- ) ;
48+
49+ return < YamlViewer schema = { schema } yamlString = { yamlString } filename = { filename } isEdit = { isEdit } onApply = { onApply } /> ;
7350} ;
0 commit comments