|
1 |
| -import React, {useEffect, useState} from 'react'; |
2 |
| -import {Divider, Row, Alert, Descriptions} from 'antd'; |
3 |
| -import axios from 'axios'; |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { Divider, Row, Alert, Descriptions } from "antd"; |
| 3 | +import axios from "axios"; |
4 | 4 | import ReactAce from "react-ace";
|
5 | 5 |
|
6 | 6 | interface Props {
|
7 |
| - name: string; |
8 |
| - namespace: string; |
| 7 | + name: string; |
| 8 | + namespace: string; |
9 | 9 | }
|
10 | 10 |
|
11 |
| -const ConfigMap = ({name, namespace}: Props) => { |
12 |
| - const [configMap, setConfigMap] = useState({}); |
13 |
| - const [error, setError] = useState({ |
14 |
| - message: "", |
15 |
| - description: "", |
16 |
| - }); |
| 11 | +const ConfigMap = ({ name, namespace }: Props) => { |
| 12 | + const [configMap, setConfigMap] = useState({}); |
| 13 | + const [error, setError] = useState({ |
| 14 | + message: "", |
| 15 | + description: "", |
| 16 | + }); |
17 | 17 |
|
18 |
| - function fetchConfigMap() { |
19 |
| - axios.get(`/api/resources`,{ |
20 |
| - params: { |
21 |
| - group: ``, |
22 |
| - version: `v1`, |
23 |
| - kind: `ConfigMap`, |
24 |
| - name: name, |
25 |
| - namespace: namespace |
26 |
| - } |
27 |
| - }).then(res => { |
28 |
| - setConfigMap(res.data) |
29 |
| - }).catch(error => { |
30 |
| - console.log(error) |
31 |
| - if (error.response === undefined) { |
32 |
| - setError({ |
33 |
| - message: String(error), |
34 |
| - description: "Check if Cyclops backend is available on: " + window.__RUNTIME_CONFIG__.REACT_APP_CYCLOPS_CTRL_HOST |
35 |
| - }) |
36 |
| - } else { |
37 |
| - setError(error.response.data); |
38 |
| - } |
39 |
| - }) |
40 |
| - } |
41 |
| - |
42 |
| - useEffect(() => { |
43 |
| - fetchConfigMap() |
44 |
| - const interval = setInterval(() => fetchConfigMap(), ) |
45 |
| - return () => { |
46 |
| - clearInterval(interval); |
| 18 | + function fetchConfigMap() { |
| 19 | + axios |
| 20 | + .get(`/api/resources`, { |
| 21 | + params: { |
| 22 | + group: ``, |
| 23 | + version: `v1`, |
| 24 | + kind: `ConfigMap`, |
| 25 | + name: name, |
| 26 | + namespace: namespace, |
| 27 | + }, |
| 28 | + }) |
| 29 | + .then((res) => { |
| 30 | + setConfigMap(res.data); |
| 31 | + }) |
| 32 | + .catch((error) => { |
| 33 | + console.log(error); |
| 34 | + if (error.response === undefined) { |
| 35 | + setError({ |
| 36 | + message: String(error), |
| 37 | + description: |
| 38 | + "Check if Cyclops backend is available on: " + |
| 39 | + window.__RUNTIME_CONFIG__.REACT_APP_CYCLOPS_CTRL_HOST, |
| 40 | + }); |
| 41 | + } else { |
| 42 | + setError(error.response.data); |
47 | 43 | }
|
48 |
| - }, []); |
| 44 | + }); |
| 45 | + } |
49 | 46 |
|
| 47 | + useEffect(() => { |
| 48 | + fetchConfigMap(); |
| 49 | + const interval = setInterval(() => fetchConfigMap()); |
| 50 | + return () => { |
| 51 | + clearInterval(interval); |
| 52 | + }; |
| 53 | + }, []); |
50 | 54 |
|
51 |
| - const configMapData = (configMap: any) => { |
52 |
| - if (configMap.data) { |
53 |
| - return <Descriptions style={{width: "100%"}} bordered> |
54 |
| - {Object.entries<string>(configMap.data).map(([key, dataValue]) => ( |
55 |
| - <Descriptions.Item key={key} labelStyle={{width: "20%"}} label={key} span={24} > |
56 |
| - {configMapDataValues(key, dataValue)} |
57 |
| - </Descriptions.Item> |
58 |
| - ))} |
59 |
| - </Descriptions> |
60 |
| - } |
| 55 | + const configMapData = (configMap: any) => { |
| 56 | + if (configMap.data) { |
| 57 | + return ( |
| 58 | + <Descriptions style={{ width: "100%" }} bordered> |
| 59 | + {Object.entries<string>(configMap.data).map(([key, dataValue]) => ( |
| 60 | + <Descriptions.Item |
| 61 | + key={key} |
| 62 | + labelStyle={{ width: "20%" }} |
| 63 | + label={key} |
| 64 | + span={24} |
| 65 | + > |
| 66 | + {configMapDataValues(key, dataValue)} |
| 67 | + </Descriptions.Item> |
| 68 | + ))} |
| 69 | + </Descriptions> |
| 70 | + ); |
61 | 71 | }
|
| 72 | + }; |
62 | 73 |
|
63 |
| - const configMapDataValues = (key: string, data: string) => { |
64 |
| - const lines = data.split('\n').length; |
| 74 | + const configMapDataValues = (key: string, data: string) => { |
| 75 | + const lines = data.split("\n").length; |
65 | 76 |
|
66 |
| - if (lines > 1) { |
67 |
| - return <ReactAce |
68 |
| - setOptions={{ useWorker: false }} |
69 |
| - value={data} |
70 |
| - readOnly={true} |
71 |
| - width="100%" |
72 |
| - mode={configMapDataExtension(key)} |
73 |
| - height={calculateEditorHeight(lines)} |
74 |
| - /> |
75 |
| - } else { |
76 |
| - return data |
77 |
| - } |
| 77 | + if (lines > 1) { |
| 78 | + return ( |
| 79 | + <ReactAce |
| 80 | + setOptions={{ useWorker: false }} |
| 81 | + value={data} |
| 82 | + readOnly={true} |
| 83 | + width="100%" |
| 84 | + mode={configMapDataExtension(key)} |
| 85 | + height={calculateEditorHeight(lines)} |
| 86 | + /> |
| 87 | + ); |
| 88 | + } else { |
| 89 | + return data; |
78 | 90 | }
|
| 91 | + }; |
79 | 92 |
|
80 |
| - const calculateEditorHeight = (lines: number) => { |
81 |
| - if (lines > 20) { |
82 |
| - return '320px' |
83 |
| - } else { |
84 |
| - return `${lines * 16}px` |
85 |
| - } |
86 |
| - }; |
| 93 | + const calculateEditorHeight = (lines: number) => { |
| 94 | + if (lines > 20) { |
| 95 | + return "320px"; |
| 96 | + } else { |
| 97 | + return `${lines * 16}px`; |
| 98 | + } |
| 99 | + }; |
87 | 100 |
|
88 |
| - const configMapDataExtension = (filename: string) => { |
89 |
| - const ext = filename.split('.').pop(); |
90 |
| - switch (ext) { |
91 |
| - case "json": |
92 |
| - return "json" |
93 |
| - default: |
94 |
| - return "json" |
95 |
| - } |
| 101 | + const configMapDataExtension = (filename: string) => { |
| 102 | + const ext = filename.split(".").pop(); |
| 103 | + switch (ext) { |
| 104 | + case "json": |
| 105 | + return "json"; |
| 106 | + default: |
| 107 | + return "json"; |
96 | 108 | }
|
| 109 | + }; |
97 | 110 |
|
98 |
| - return ( |
99 |
| - <div> |
100 |
| - { |
101 |
| - error.message.length !== 0 && <Alert |
102 |
| - message={error.message} |
103 |
| - description={error.description} |
104 |
| - type="error" |
105 |
| - closable |
106 |
| - afterClose={() => {setError({ |
107 |
| - message: "", |
108 |
| - description: "", |
109 |
| - })}} |
110 |
| - style={{marginBottom: '20px'}} |
111 |
| - /> |
112 |
| - } |
113 |
| - <Row> |
114 |
| - <Divider style={{fontSize: '120%'}} orientationMargin="0" orientation={"left"}>Data:</Divider> |
115 |
| - {configMapData(configMap)} |
116 |
| - </Row> |
117 |
| - </div> |
118 |
| - ); |
119 |
| -} |
| 111 | + return ( |
| 112 | + <div> |
| 113 | + {error.message.length !== 0 && ( |
| 114 | + <Alert |
| 115 | + message={error.message} |
| 116 | + description={error.description} |
| 117 | + type="error" |
| 118 | + closable |
| 119 | + afterClose={() => { |
| 120 | + setError({ |
| 121 | + message: "", |
| 122 | + description: "", |
| 123 | + }); |
| 124 | + }} |
| 125 | + style={{ marginBottom: "20px" }} |
| 126 | + /> |
| 127 | + )} |
| 128 | + <Row> |
| 129 | + <Divider |
| 130 | + style={{ fontSize: "120%" }} |
| 131 | + orientationMargin="0" |
| 132 | + orientation={"left"} |
| 133 | + > |
| 134 | + Data: |
| 135 | + </Divider> |
| 136 | + {configMapData(configMap)} |
| 137 | + </Row> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}; |
120 | 141 |
|
121 | 142 | export default ConfigMap;
|
0 commit comments