@@ -17,6 +17,7 @@ import {
1717 getResourceSidebarSamples ,
1818 useTelemetry ,
1919 useUserSettingsCompatibility ,
20+ WithPostFormSubmissionCallbackProps ,
2021} from '@console/shared' ;
2122import {
2223 SHOW_YAML_EDITOR_TOOLTIPS_USER_SETTING_KEY ,
@@ -31,18 +32,23 @@ import CodeEditorSidebar from '@console/shared/src/components/editor/CodeEditorS
3132import { fold } from '@console/shared/src/components/editor/yaml-editor-utils' ;
3233import { downloadYaml } from '@console/shared/src/components/editor/yaml-download-utils' ;
3334import { useFullscreen } from '@console/shared/src/hooks/useFullscreen' ;
34- import { isYAMLTemplate , getImpersonate , YAMLTemplate } from '@console/dynamic-plugin-sdk' ;
35+ import {
36+ isYAMLTemplate ,
37+ getImpersonate ,
38+ YAMLTemplate ,
39+ K8sResourceKind ,
40+ } from '@console/dynamic-plugin-sdk' ;
3541import { useResolvedExtensions } from '@console/dynamic-plugin-sdk/src/api/useResolvedExtensions' ;
36- import { connectToFlags } from '../reducers/connectToFlags' ;
42+ import { connectToFlags , WithFlagsProps } from '../reducers/connectToFlags' ;
3743import { errorModal , managedResourceSaveModal } from './modals' ;
3844import ReplaceCodeModal from './modals/replace-code-modal' ;
3945import {
4046 checkAccess ,
4147 Firehose ,
4248 Loading ,
43- LoadingBox ,
4449 resourceObjPath ,
4550 resourceListPathFromModel ,
51+ FirehoseResult ,
4652} from './utils' ;
4753import { PageHeading } from '@console/shared/src/components/heading/PageHeading' ;
4854import {
@@ -54,9 +60,11 @@ import {
5460 groupVersionFor ,
5561 AccessReviewResourceAttributes ,
5662 CodeEditorRef ,
63+ K8sModel ,
5764} from '../module/k8s' ;
5865import { ConsoleYAMLSampleModel } from '../models' ;
5966import { getYAMLTemplates } from '../models/yaml-templates' ;
67+ import { ConnectDropTarget } from 'react-dnd' ;
6068import { findOwner } from '../module/k8s/managed-by' ;
6169import { ClusterServiceVersionModel } from '@console/operator-lifecycle-manager/src/models' ;
6270import { definitionFor } from '../module/k8s/swagger' ;
@@ -66,39 +74,89 @@ import { CodeEditorControl } from '@patternfly/react-code-editor';
6674import { CompressIcon } from '@patternfly/react-icons/dist/js/icons/compress-icon' ;
6775import { ExpandIcon } from '@patternfly/react-icons/dist/js/icons/expand-icon' ;
6876import { ToggleSidebarButton } from '@console/shared/src/components/editor/ToggleSidebarButton' ;
69-
70- const generateObjToLoad = ( templateExtensions , kind , id , yaml , namespace = 'default' ) => {
71- const sampleObj = safeLoad ( yaml ? yaml : getYAMLTemplates ( templateExtensions ) . getIn ( [ kind , id ] ) ) ;
77+ import { RootState } from '@console/internal/redux' ;
78+ import { getActiveNamespace } from '@console/internal/reducers/ui' ;
79+
80+ const generateObjToLoad = (
81+ templateExtensions : Parameters < typeof getYAMLTemplates > [ 0 ] ,
82+ kind : string ,
83+ id : string ,
84+ yaml : string ,
85+ namespace = 'default' ,
86+ ) => {
87+ const sampleObj : K8sResourceKind = safeLoad (
88+ yaml ? yaml : getYAMLTemplates ( templateExtensions ) . getIn ( [ kind , id ] ) ,
89+ ) ;
7290 if ( _ . has ( sampleObj . metadata , 'namespace' ) ) {
7391 sampleObj . metadata . namespace = namespace ;
7492 }
7593 return sampleObj ;
7694} ;
7795
78- const stateToProps = ( state ) => ( {
79- activeNamespace : state . UI . get ( 'activeNamespace' ) ,
96+ const stateToProps = ( state : RootState ) => ( {
97+ activeNamespace : getActiveNamespace ( state ) ,
8098 impersonate : getImpersonate ( state ) ,
81- models : state . k8s . getIn ( [ 'RESOURCES' , 'models' ] ) ,
99+ models : state . k8s . getIn ( [ 'RESOURCES' , 'models' ] ) as Map < string , K8sModel > ,
82100} ) ;
83101
84- const WithYamlTemplates = ( Component ) =>
85- function Comp ( props ) {
86- const kind = props ?. obj ?. kind ;
87- const [ templateExtensions , resolvedTemplates ] = useResolvedExtensions < YAMLTemplate > (
88- React . useCallback (
89- ( e ) : e is YAMLTemplate => isYAMLTemplate ( e ) && e . properties . model . kind === kind ,
90- [ kind ] ,
91- ) ,
92- ) ;
93-
94- return ! resolvedTemplates ? (
95- < LoadingBox />
96- ) : (
97- < Component templateExtensions = { templateExtensions } { ...props } />
98- ) ;
102+ type EditYAMLInnerProps = WithPostFormSubmissionCallbackProps < any > &
103+ ReturnType < typeof stateToProps > & {
104+ /** The sample object to load into the editor */
105+ sampleObj ?: ReturnType < typeof generateObjToLoad > ;
106+
107+ /** Whether to allow multiple YAML documents in the editor */
108+ allowMultiple ?: boolean ;
109+ /** Function to connect the drop target for drag-and-drop */
110+ connectDropTarget ?: ConnectDropTarget ;
111+ /** Whether the drop target is currently being hovered over */
112+ isOver ?: boolean ;
113+ /** Whether the drop target can accept a file */
114+ canDrop ?: boolean ;
115+ /** Whether this is a create operation */
116+ create : boolean ;
117+ /** List of YAML samples to display */
118+ yamlSamplesList ?: FirehoseResult ;
119+ /** Custom CSS class for the editor */
120+ customClass ?: string ;
121+ /** Callback function to handle changes in the YAML content */
122+ onChange ?: ( yaml : string ) => void ;
123+ /** Model to use for the editor */
124+ model ?: K8sModel ;
125+ /** Whether to show tooltips in the editor */
126+ showTooltips ?: boolean ;
127+ /** Whether to add a button to download the YAML */
128+ download ?: boolean ;
129+ /** Header text or component to display above the editor */
130+ header : React . ComponentProps < typeof PageHeading > [ 'title' ] ;
131+ /** Whether the YAML is generic (not tied to a specific resource) */
132+ genericYAML ?: boolean ;
133+ /** Custom alerts to display in the editor */
134+ children ?: React . ReactNode ;
135+ /** URL to redirect to after saving */
136+ redirectURL ?: string ;
137+ /** Function to clear the file upload state */
138+ clearFileUpload : ( ) => void ;
139+ /** Callback function to save the YAML content */
140+ onSave ?: ( yaml : string ) => void ;
141+ /** Whether this is a redirect from code import */
142+ isCodeImportRedirect ?: boolean ;
143+ /** The object being edited */
144+ obj ?: K8sResourceKind ;
145+ /** Error message to display */
146+ error ?: string ;
147+ /** Whether the editor is in read-only mode */
148+ readOnly ?: boolean ;
149+ /** Whether to hide the header */
150+ hideHeader ?: boolean ;
151+ /** Function to get the resource object path */
152+ resourceObjPath ?: ( obj : K8sResourceKind , objRef : string ) => string ;
153+ /** Callback function to be called on cancel */
154+ onCancel ?: ( ) => void ;
155+ /** The file upload content */
156+ fileUpload ?: string ;
99157 } ;
100158
101- const EditYAMLInner = ( props ) => {
159+ const EditYAMLInner : React . FC < EditYAMLInnerProps > = ( props ) => {
102160 const {
103161 allowMultiple,
104162 connectDropTarget,
@@ -135,6 +193,13 @@ const EditYAMLInner = (props) => {
135193 const [ editorMounted , setEditorMounted ] = React . useState ( false ) ;
136194 const [ fullscreenRef , toggleFullscreen , isFullscreen , canUseFullScreen ] = useFullscreen ( ) ;
137195
196+ const [ templateExtensions , resolvedTemplates ] = useResolvedExtensions < YAMLTemplate > (
197+ React . useCallback (
198+ ( e ) : e is YAMLTemplate => isYAMLTemplate ( e ) && e . properties . model . kind === props ?. obj ?. kind ,
199+ [ props ?. obj ?. kind ] ,
200+ ) ,
201+ ) ;
202+
138203 const [ showTooltips ] = useUserSettingsCompatibility (
139204 SHOW_YAML_EDITOR_TOOLTIPS_USER_SETTING_KEY ,
140205 SHOW_YAML_EDITOR_TOOLTIPS_LOCAL_STORAGE_KEY ,
@@ -164,9 +229,7 @@ const EditYAMLInner = (props) => {
164229 const { t } = useTranslation ( ) ;
165230
166231 const getEditor = ( ) : editor . IStandaloneCodeEditor | undefined =>
167- monacoRef ?. current && 'editor' in monacoRef . current
168- ? ( monacoRef . current as any ) . editor
169- : undefined ;
232+ 'editor' in monacoRef ?. current ? monacoRef . current . editor : undefined ;
170233
171234 const getModel = React . useCallback (
172235 ( obj ) => {
@@ -706,13 +769,7 @@ const EditYAMLInner = (props) => {
706769
707770 const getYamlContent_ = ( id = 'default' , yaml = '' , kind = referenceForModel ( props . model ) ) => {
708771 try {
709- const s = generateObjToLoad (
710- props . templateExtensions ,
711- kind ,
712- id ,
713- yaml ,
714- props . obj . metadata . namespace ,
715- ) ;
772+ const s = generateObjToLoad ( templateExtensions , kind , id , yaml , props . obj . metadata . namespace ) ;
716773 setSampleObj ( s ) ;
717774 return s ;
718775 } catch ( error ) {
@@ -744,7 +801,7 @@ const EditYAMLInner = (props) => {
744801 editorMounted && getEditor ( ) ?. updateOptions ( { stickyScroll : { enabled : stickyScrollEnabled } } ) ;
745802 } , [ showTooltips , stickyScrollEnabled , editorMounted ] ) ;
746803
747- if ( ! create && ! props . obj ) {
804+ if ( ( ! create && ! props . obj ) || ! resolvedTemplates ) {
748805 return < Loading /> ;
749806 }
750807
@@ -967,25 +1024,24 @@ const EditYAMLInner = (props) => {
9671024 * This component loads the entire Monaco editor library with it.
9681025 * Consider using `AsyncComponent` to dynamically load this component when needed.
9691026 */
970- /** @augments {React.Component<{allowMultiple?: boolean, obj?: any, create: boolean, kind: string, redirectURL?: string, resourceObjPath?: (obj: K8sResourceKind, objRef: string) => string}, onChange?: (yaml: string) => void, clearFileUpload?: () => void> } */
971- export const EditYAML_ = connect ( stateToProps ) (
972- WithYamlTemplates ( withPostFormSubmissionCallback ( EditYAMLInner ) ) ,
973- ) ;
1027+ export const EditYAML_ = connect ( stateToProps ) ( withPostFormSubmissionCallback ( EditYAMLInner ) ) ;
1028+
1029+ export const EditYAML = connectToFlags ( FLAGS . CONSOLE_YAML_SAMPLE ) (
1030+ ( { flags, ...props } : WithFlagsProps & EditYAMLInnerProps ) => {
1031+ const resources = flags [ FLAGS . CONSOLE_YAML_SAMPLE ]
1032+ ? [
1033+ {
1034+ kind : referenceForModel ( ConsoleYAMLSampleModel ) ,
1035+ isList : true ,
1036+ prop : 'yamlSamplesList' ,
1037+ } ,
1038+ ]
1039+ : [ ] ;
9741040
975- export const EditYAML = connectToFlags ( FLAGS . CONSOLE_YAML_SAMPLE ) ( ( { flags, ...props } ) => {
976- const resources = flags [ FLAGS . CONSOLE_YAML_SAMPLE ]
977- ? [
978- {
979- kind : referenceForModel ( ConsoleYAMLSampleModel ) ,
980- isList : true ,
981- prop : 'yamlSamplesList' ,
982- } ,
983- ]
984- : [ ] ;
985-
986- return (
987- < Firehose resources = { resources } >
988- < EditYAML_ { ...props } />
989- </ Firehose >
990- ) ;
991- } ) ;
1041+ return (
1042+ < Firehose resources = { resources } >
1043+ < EditYAML_ { ...props } />
1044+ </ Firehose >
1045+ ) ;
1046+ } ,
1047+ ) ;
0 commit comments