Skip to content

Commit

Permalink
feat: Enhance Config Maps (#3300)
Browse files Browse the repository at this point in the history
* add binaryData

* dependencies for useEffect

* fix naming

* fix graph for config maps

* remove configMap from pod graph config
  • Loading branch information
OliwiaGowor authored Aug 28, 2024
1 parent d1d3398 commit 5bdc4bb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
20 changes: 16 additions & 4 deletions src/resources/ConfigMaps/ConfigMapDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,33 @@ export function ConfigMapDetails(props) {
));
};

const ConfigMapBinaryDataEditor = resource => {
const { binaryData } = resource;
return Object.keys(binaryData || {}).map(key => (
<ReadonlyEditorPanel
title={key}
value={binaryData[key]}
key={key + JSON.stringify(binaryData[key])}
isBase64={true}
/>
));
};

const customColumns = [
{
header: t('common.headers.owner'),
value: secret => (
value: configMap => (
<ControlledBy
ownerReferences={secret.metadata.ownerReferences}
namespace={secret.metadata.namespace}
ownerReferences={configMap.metadata.ownerReferences}
namespace={configMap.metadata.namespace}
/>
),
},
];

return (
<ResourceDetails
customComponents={[ConfigMapEditor]}
customComponents={[ConfigMapEditor, ConfigMapBinaryDataEditor]}
customColumns={customColumns}
description={ResourceDescription}
createResourceForm={ConfigMapCreate}
Expand Down
11 changes: 11 additions & 0 deletions src/resources/ConfigMaps/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { ResourceRelationConfig } from 'shared/components/ResourceGraph/types';
import { predefinedCategories } from 'state/navigation/categories';
import { Description } from 'shared/components/Description/Description';
import { matchByOwnerReference } from 'shared/utils/helpers';

export const resourceType = 'ConfigMaps';
export const namespaced = true;
Expand All @@ -24,4 +25,14 @@ export const Create = React.lazy(() => import('./ConfigMapCreate'));
export const resourceGraphConfig = (): ResourceRelationConfig => ({
depth: 1,
networkFlowLevel: 1,
relations: [
{
resource: { kind: 'Pod' },
filter: (cm, pod) =>
matchByOwnerReference({
resource: cm,
owner: pod,
}),
},
],
});
4 changes: 0 additions & 4 deletions src/resources/Pods/index.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/shared/components/MonacoEditorESM/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export function Editor({
}
}, [value, editorInstance, error]);

useEffect(() => {
if (prevValueRef.current !== value && readOnly) {
editorInstance.setValue(value);
prevValueRef.current = value;
}
}, [value, editorInstance, readOnly]);

useUpdateValueOnParentChange({
updateValueOnParentChange,
editorInstance,
Expand Down
56 changes: 49 additions & 7 deletions src/shared/components/ReadonlyEditorPanel.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
import React from 'react';

import { EditorActions } from 'shared/contexts/YamlEditorContext/EditorActions';
import { Editor } from 'shared/components/MonacoEditorESM/Editor';
import { UI5Panel } from './UI5Panel/UI5Panel';
import { useTranslation } from 'react-i18next';
import { useState } from 'react';
import { Button } from '@ui5/webcomponents-react';
import { base64Decode } from 'shared/helpers';

export function ReadonlyEditorPanel({ title, value, editorProps, actions }) {
const [editor, setEditor] = React.useState(null);
export function ReadonlyEditorPanel({
title,
value,
editorProps,
actions,
isBase64 = false,
}) {
const { t } = useTranslation();
const [editor, setEditor] = useState(null);
const [isEncoded, setEncoded] = useState(true);
const [valueState, setValueState] = useState(value);

const decode = () => {
setEncoded(true);
setValueState(value);
};
const encode = () => {
setEncoded(false);
setValueState(base64Decode(value));
};

const options = {
minimap: {
enabled: false,
},
};

const headerActions =
actions ||
(isBase64 && (
<>
{actions && actions}
{isBase64 && (
<Button
design="Transparent"
icon={isEncoded ? 'show' : 'hide'}
disabled={!value}
onClick={() => {
return isEncoded ? encode() : decode();
}}
>
{isEncoded
? t('secrets.buttons.decode')
: t('secrets.buttons.encode')}
</Button>
)}
</>
));

return (
<UI5Panel title={title} headerActions={actions}>
<UI5Panel title={title} headerActions={headerActions}>
<EditorActions
val={value}
val={valueState}
editor={editor}
title={title}
saveDisabled={true}
/>
<Editor
height="20em"
value={value}
value={valueState}
options={options}
onMount={setEditor}
autocompletionDisabled
Expand Down

0 comments on commit 5bdc4bb

Please sign in to comment.