@@ -4,10 +4,12 @@ import {
44 Title ,
55 Icon ,
66 FlexBox ,
7- Link ,
87 Button ,
98} from '@ui5/webcomponents-react' ;
10- import { formatCodeSegment } from 'components/KymaCompanion/utils/formatMarkdown' ;
9+ import {
10+ Segment ,
11+ formatCodeSegment ,
12+ } from 'components/KymaCompanion/utils/formatMarkdown' ;
1113import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
1214import { useRecoilState , useRecoilValue } from 'recoil' ;
1315import {
@@ -18,7 +20,6 @@ import {
1820import copyToCliboard from 'copy-to-clipboard' ;
1921import './CodePanel.scss' ;
2022import { columnLayoutState } from 'state/columnLayoutAtom' ;
21- import { useUrl } from 'hooks/useUrl' ;
2223import { useNavigate , useSearchParams } from 'react-router-dom' ;
2324import { clusterState } from 'state/clusterAtom' ;
2425import jsyaml from 'js-yaml' ;
@@ -65,15 +66,16 @@ function getCustomTheme(theme: Theme) {
6566}
6667
6768interface CodePanelProps {
68- segment : object ;
69+ segment : CodeSegment ;
6970}
7071
71- export default function CodePanel ( { segment } : CodePanelProps ) : JSX . Element {
72+ export default function CodePanel ( {
73+ segment,
74+ } : CodePanelProps | Segment ) : JSX . Element {
7275 const theme = useRecoilValue ( themeState ) ;
7376 const syntaxTheme = getCustomTheme ( theme ) ;
7477 const { language, code } = formatCodeSegment ( segment ?. content ?? segment ) ;
7578 const [ layoutState , setLayoutColumn ] = useRecoilState ( columnLayoutState ) ;
76- const currUrl = useUrl ( ) ;
7779 const navigate = useNavigate ( ) ;
7880 const [ searchParams ] = useSearchParams ( ) ;
7981 const cluster = useRecoilValue ( clusterState ) ;
@@ -83,20 +85,27 @@ export default function CodePanel({ segment }: CodePanelProps): JSX.Element {
8385 const resourcePath = `${
8486 namespace ? `/namespaces/${ namespace } ` : ''
8587 } /${ pluralize ( resType ) . toLowerCase ( ) } ${ resName ? '/' + resName : '' } `;
86- // const layoutParam = searchParams.get('layout')
87- // ? `?layout=${searchParams.get('layout')}`
88- // : '';
89- const createParams =
90- type === 'Update'
91- ? '?layout=TwoColumnsMidExpanded&showEdit=true'
92- : '?layout=TwoColumnsMidExpanded&showCreate=true' ;
9388
94- return `${ basePath } ${ resourcePath } ${ createParams } ` ;
89+ const params = new URLSearchParams ( ) ;
90+ if ( layoutState . layout !== 'OneColumn' ) {
91+ params . set ( 'layout' , layoutState . layout ) ;
92+ }
93+ if ( type === 'Update' ) {
94+ params . set ( 'showEdit' , 'true' ) ;
95+ } else {
96+ params . set ( 'showCreate' , 'true' ) ;
97+ }
98+
99+ return `${ basePath } ${ resourcePath } ?${ params } ` ;
95100 } ;
96101
97102 const handleSetupInEditor = ( url , resource , type ) => {
98103 const parts = url . split ( '/' ) . slice ( 1 ) ; // Remove the leading empty string from split
99104 let [ namespace , resType , resName ] = [ null , '' , '' ] ;
105+ const parsedResource = jsyaml . load ( resource . replace ( 'yaml' , '' ) ) as
106+ | object
107+ | null ;
108+
100109 if ( parts [ 0 ] === 'namespaces' ) {
101110 [ namespace , resType , resName ] = [ parts [ 1 ] , parts [ 2 ] , parts [ 3 ] ] ;
102111
@@ -107,7 +116,7 @@ export default function CodePanel({ segment }: CodePanelProps): JSX.Element {
107116 type === 'New'
108117 ? {
109118 ...layoutState . showCreate ,
110- resource : jsyaml . load ( resource . replace ( 'yaml' , '' ) ) ,
119+ resource : parsedResource ,
111120 resourceType : resType ,
112121 namespaceId : namespace ,
113122 }
@@ -116,9 +125,10 @@ export default function CodePanel({ segment }: CodePanelProps): JSX.Element {
116125 type === 'Update'
117126 ? {
118127 ...layoutState . showEdit ,
119- resource : jsyaml . load ( resource . replace ( 'yaml' , '' ) ) ,
128+ resource : parsedResource ,
120129 resourceType : resType ,
121130 namespaceId : namespace ,
131+ resourceName : resName ,
122132 }
123133 : null ,
124134 } ) ;
@@ -148,17 +158,22 @@ export default function CodePanel({ segment }: CodePanelProps): JSX.Element {
148158 < Title level = "H6" size = "H6" >
149159 { language }
150160 </ Title >
151- < Button icon = "copy" onClick = { ( ) => copyToCliboard ( code ) } >
152- { segment ?. link ?. name }
161+ < Button
162+ design = "Transparent"
163+ icon = "copy"
164+ onClick = { ( ) => copyToCliboard ( code ) }
165+ >
166+ Copy
153167 </ Button >
154168 { segment ?. type === 'codeWithAction' && (
155169 < Button
170+ design = "Transparent"
156171 icon = "sys-add"
157172 onClick = { ( ) =>
158173 handleSetupInEditor (
159174 segment ?. link ?. address ,
160175 segment ?. content ,
161- segment ?. link ?. type ,
176+ segment ?. link ?. actionType ,
162177 )
163178 }
164179 >
0 commit comments