@dovetail-v2/refine 0.0.46 → 0.0.48-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. package/dist/{MonacoYamlDiffEditor-68215003.js → MonacoYamlDiffEditor-ed04648b.js} +1 -1
  2. package/dist/{index-238ec29f.js → index-04f52e89.js} +56 -39
  3. package/dist/refine.js +1 -1
  4. package/dist/refine.umd.cjs +55 -38
  5. package/dist/style.css +4 -4
  6. package/lib/src/components/EventsTable/EventsTable.d.ts +5 -1
  7. package/lib/src/components/ModalContextProvider/index.d.ts +12 -0
  8. package/lib/src/components/NetworkPolicyRulesTable/NetworkPolicyRulesTable.d.ts +2 -1
  9. package/lib/src/components/YamlForm/index.d.ts +4 -0
  10. package/lib/src/hooks/useEagleForm.d.ts +2 -1
  11. package/lib/src/hooks/useModal.d.ts +0 -0
  12. package/lib/src/hooks/useNamespaceFilter.d.ts +5 -0
  13. package/lib/src/i18n.d.ts +2 -0
  14. package/lib/src/locales/en-US/index.d.ts +1 -0
  15. package/lib/src/locales/zh-CN/index.d.ts +1 -0
  16. package/lib/src/model/cronjob-model.d.ts +9 -0
  17. package/lib/src/model/index.d.ts +6 -0
  18. package/lib/src/model/job-model.d.ts +10 -0
  19. package/lib/src/model/pod-metrics-model.d.ts +7 -0
  20. package/lib/src/model/pod-model.d.ts +15 -0
  21. package/lib/src/model/resource-model.d.ts +17 -0
  22. package/lib/src/model/workload-model.d.ts +17 -0
  23. package/lib/src/types/metric.d.ts +25 -0
  24. package/package.json +2 -2
  25. package/lib/src/App.js +0 -91
  26. package/lib/src/Dovetail.js +0 -45
  27. package/lib/src/components/Breadcrumb/index.js +0 -20
  28. package/lib/src/components/ConditionsTable/ConditionsTable.js +0 -56
  29. package/lib/src/components/ConditionsTable/index.js +0 -1
  30. package/lib/src/components/CreateButton/index.js +0 -10
  31. package/lib/src/components/CronJobDropdown/index.js +0 -26
  32. package/lib/src/components/CronjobJobsTable/index.js +0 -49
  33. package/lib/src/components/DeleteButton/index.js +0 -13
  34. package/lib/src/components/DeleteManyButton/index.js +0 -16
  35. package/lib/src/components/DrawerShow/DrawerShow.js +0 -13
  36. package/lib/src/components/DrawerShow/index.js +0 -1
  37. package/lib/src/components/EditButton/index.js +0 -13
  38. package/lib/src/components/EditField/index.js +0 -47
  39. package/lib/src/components/ErrorContent/index.js +0 -36
  40. package/lib/src/components/EventsTable/EventsTable.js +0 -59
  41. package/lib/src/components/EventsTable/index.js +0 -1
  42. package/lib/src/components/Form/KeyValueListWidget.js +0 -56
  43. package/lib/src/components/Form/MetadataForm.js +0 -9
  44. package/lib/src/components/Form/NameInputWidget.js +0 -50
  45. package/lib/src/components/Form/NamespaceSelectWidget.js +0 -21
  46. package/lib/src/components/Form/index.js +0 -4
  47. package/lib/src/components/Form/widget.js +0 -1
  48. package/lib/src/components/FormErrorAlert/index.js +0 -8
  49. package/lib/src/components/FormLayout/index.js +0 -21
  50. package/lib/src/components/FormModal/index.js +0 -51
  51. package/lib/src/components/ImageNames/index.js +0 -14
  52. package/lib/src/components/IngressRulesComponent/index.js +0 -11
  53. package/lib/src/components/IngressRulesTable/IngressRulesTable.js +0 -51
  54. package/lib/src/components/IngressRulesTable/index.js +0 -1
  55. package/lib/src/components/K8sDropdown/index.js +0 -31
  56. package/lib/src/components/KeyValue/KeyValue.js +0 -27
  57. package/lib/src/components/KeyValue/index.js +0 -1
  58. package/lib/src/components/KeyValueData/index.js +0 -50
  59. package/lib/src/components/Layout/index.js +0 -39
  60. package/lib/src/components/ListPage/index.js +0 -25
  61. package/lib/src/components/Menu/index.js +0 -36
  62. package/lib/src/components/NamespacesFilter/index.js +0 -34
  63. package/lib/src/components/NetworkPolicyRulesTable/NetworkPolicyRulesTable.js +0 -77
  64. package/lib/src/components/NetworkPolicyRulesTable/index.js +0 -1
  65. package/lib/src/components/PageShow/PageShow.js +0 -11
  66. package/lib/src/components/PageShow/index.js +0 -1
  67. package/lib/src/components/PodContainersTable/PodContainersTable.js +0 -78
  68. package/lib/src/components/PodContainersTable/index.js +0 -1
  69. package/lib/src/components/PodLog/index.js +0 -136
  70. package/lib/src/components/ReferenceLink/index.js +0 -17
  71. package/lib/src/components/RefineForm/RefineFormContent.d.ts +0 -10
  72. package/lib/src/components/RefineForm/RefineFormModal.d.ts +0 -8
  73. package/lib/src/components/RefineForm/RefineFormPage.d.ts +0 -7
  74. package/lib/src/components/RefineForm/index.d.ts +0 -3
  75. package/lib/src/components/RefineForm/type.d.ts +0 -20
  76. package/lib/src/components/RefineForm/useRefineForm.d.ts +0 -10
  77. package/lib/src/components/ResourceCRUD/ResourceCRUD.js +0 -15
  78. package/lib/src/components/ResourceCRUD/create/index.js +0 -13
  79. package/lib/src/components/ResourceCRUD/index.js +0 -4
  80. package/lib/src/components/ResourceCRUD/list/index.js +0 -19
  81. package/lib/src/components/ResourceCRUD/show/index.js +0 -6
  82. package/lib/src/components/ResourceLink/index.js +0 -21
  83. package/lib/src/components/ResourceUsageBar/index.js +0 -81
  84. package/lib/src/components/Separator/index.js +0 -11
  85. package/lib/src/components/ShowContent/ShowContent.js +0 -157
  86. package/lib/src/components/ShowContent/fields.js +0 -157
  87. package/lib/src/components/ShowContent/index.js +0 -2
  88. package/lib/src/components/StateTag/StateTag.js +0 -22
  89. package/lib/src/components/StateTag/index.js +0 -1
  90. package/lib/src/components/Table/ErrorContent.js +0 -36
  91. package/lib/src/components/Table/TableToolBar.js +0 -14
  92. package/lib/src/components/Table/TableWidgets.js +0 -28
  93. package/lib/src/components/Table/index.js +0 -69
  94. package/lib/src/components/Tags/index.js +0 -22
  95. package/lib/src/components/Time/index.js +0 -14
  96. package/lib/src/components/WorkloadDropdown/index.js +0 -24
  97. package/lib/src/components/WorkloadPodsTable/WorkloadPodsTable.js +0 -39
  98. package/lib/src/components/WorkloadPodsTable/index.js +0 -1
  99. package/lib/src/components/WorkloadReplicas/index.js +0 -50
  100. package/lib/src/components/YamlEditor/MonacoYamlDiffEditor.js +0 -34
  101. package/lib/src/components/YamlEditor/MonacoYamlEditor.js +0 -149
  102. package/lib/src/components/YamlEditor/YamlEditorComponent.js +0 -90
  103. package/lib/src/components/YamlEditor/index.js +0 -1
  104. package/lib/src/components/YamlEditor/style.js +0 -102
  105. package/lib/src/components/YamlEditor/yaml.worker.js +0 -1
  106. package/lib/src/components/YamlForm/index.js +0 -61
  107. package/lib/src/components/index.js +0 -38
  108. package/lib/src/constants/index.js +0 -2
  109. package/lib/src/constants/k8s.js +0 -203
  110. package/lib/src/constants/state.js +0 -15
  111. package/lib/src/contexts/component.js +0 -3
  112. package/lib/src/contexts/configs.js +0 -3
  113. package/lib/src/contexts/global-store.js +0 -3
  114. package/lib/src/contexts/index.js +0 -3
  115. package/lib/src/hooks/index.js +0 -7
  116. package/lib/src/hooks/useDeleteModal/index.js +0 -1
  117. package/lib/src/hooks/useDeleteModal/useDeleteManyModal.js +0 -31
  118. package/lib/src/hooks/useDeleteModal/useDeleteModal.js +0 -38
  119. package/lib/src/hooks/useDownloadYAML.js +0 -10
  120. package/lib/src/hooks/useEagleForm.js +0 -177
  121. package/lib/src/hooks/useEagleTable/columns.js +0 -246
  122. package/lib/src/hooks/useEagleTable/index.js +0 -2
  123. package/lib/src/hooks/useEagleTable/useEagleTable.js +0 -63
  124. package/lib/src/hooks/useEdit.js +0 -19
  125. package/lib/src/hooks/useGlobalStore.js +0 -5
  126. package/lib/src/hooks/useK8sYamlEditor.js +0 -37
  127. package/lib/src/hooks/useOpenForm.js +0 -43
  128. package/lib/src/hooks/useSchema.js +0 -37
  129. package/lib/src/hooks/useSubmitForm.js +0 -42
  130. package/lib/src/i18n.js +0 -19
  131. package/lib/src/index.js +0 -13
  132. package/lib/src/locales/en-US/dovetail.json +0 -18
  133. package/lib/src/locales/en-US/index.js +0 -4
  134. package/lib/src/locales/index.js +0 -6
  135. package/lib/src/locales/zh-CN/dovetail.json +0 -91
  136. package/lib/src/locales/zh-CN/index.js +0 -4
  137. package/lib/src/main.js +0 -12
  138. package/lib/src/models/cronjob-model.js +0 -32
  139. package/lib/src/models/daemonset-model.js +0 -17
  140. package/lib/src/models/deployment-model.js +0 -17
  141. package/lib/src/models/event-model.js +0 -11
  142. package/lib/src/models/index.js +0 -14
  143. package/lib/src/models/ingress-model.js +0 -24
  144. package/lib/src/models/job-model.js +0 -56
  145. package/lib/src/models/network-policy-model.js +0 -10
  146. package/lib/src/models/pod-metrics-model.js +0 -34
  147. package/lib/src/models/pod-model.js +0 -78
  148. package/lib/src/models/resource-model.js +0 -34
  149. package/lib/src/models/server-instance-model.d.ts +0 -10
  150. package/lib/src/models/service-model.js +0 -17
  151. package/lib/src/models/statefulset-model.js +0 -17
  152. package/lib/src/models/types/index.js +0 -1
  153. package/lib/src/models/types/metric.js +0 -1
  154. package/lib/src/models/workload-base-model.js +0 -22
  155. package/lib/src/models/workload-model.js +0 -51
  156. package/lib/src/pages/configmaps/index.js +0 -15
  157. package/lib/src/pages/cronjobs/create/index.js +0 -6
  158. package/lib/src/pages/cronjobs/index.js +0 -3
  159. package/lib/src/pages/cronjobs/list/index.js +0 -42
  160. package/lib/src/pages/cronjobs/show/index.js +0 -16
  161. package/lib/src/pages/daemonsets/create/index.js +0 -6
  162. package/lib/src/pages/daemonsets/index.js +0 -3
  163. package/lib/src/pages/daemonsets/list/index.js +0 -32
  164. package/lib/src/pages/daemonsets/show/index.js +0 -16
  165. package/lib/src/pages/deployments/create/index.js +0 -7
  166. package/lib/src/pages/deployments/index.js +0 -3
  167. package/lib/src/pages/deployments/list/index.js +0 -26
  168. package/lib/src/pages/deployments/show/index.js +0 -16
  169. package/lib/src/pages/ingresses/index.js +0 -26
  170. package/lib/src/pages/jobs/index.js +0 -34
  171. package/lib/src/pages/networkPolicies/index.js +0 -67
  172. package/lib/src/pages/pods/create/index.js +0 -6
  173. package/lib/src/pages/pods/index.js +0 -3
  174. package/lib/src/pages/pods/list/index.js +0 -81
  175. package/lib/src/pages/pods/show/index.js +0 -54
  176. package/lib/src/pages/secrets/index.js +0 -15
  177. package/lib/src/pages/services/index.js +0 -26
  178. package/lib/src/pages/statefulsets/create/index.js +0 -6
  179. package/lib/src/pages/statefulsets/index.js +0 -3
  180. package/lib/src/pages/statefulsets/list/index.js +0 -26
  181. package/lib/src/pages/statefulsets/show/index.js +0 -16
  182. package/lib/src/plugins/index.js +0 -3
  183. package/lib/src/plugins/model-plugin.js +0 -46
  184. package/lib/src/plugins/relation-plugin.js +0 -81
  185. package/lib/src/plugins/type.js +0 -1
  186. package/lib/src/providers/index.js +0 -1
  187. package/lib/src/providers/router-provider/index.js +0 -100
  188. package/lib/src/types/index.js +0 -1
  189. package/lib/src/types/resource.js +0 -12
  190. package/lib/src/utils/addId.js +0 -8
  191. package/lib/src/utils/download.js +0 -9
  192. package/lib/src/utils/error.js +0 -53
  193. package/lib/src/utils/form.js +0 -9
  194. package/lib/src/utils/k8s.js +0 -6
  195. package/lib/src/utils/labels.js +0 -15
  196. package/lib/src/utils/match-selector.js +0 -12
  197. package/lib/src/utils/openapi.js +0 -33
  198. package/lib/src/utils/schema.js +0 -117
  199. package/lib/src/utils/selector.js +0 -12
  200. package/lib/src/utils/string.js +0 -6
  201. package/lib/src/utils/time.js +0 -46
  202. package/lib/src/utils/unit.js +0 -69
  203. package/lib/src/utils/yaml.js +0 -44
  204. package/lib/vite.config.js +0 -60
@@ -1,203 +0,0 @@
1
- export const BASE_INIT_VALUE = {
2
- metadata: {
3
- name: '',
4
- namespace: 'default',
5
- annotations: {},
6
- labels: {},
7
- },
8
- };
9
- const DEFAULT_MATCH_LABEL = 'sks.user.kubesmart.smtx.io/app';
10
- const BASE_CONTAINER_INIT_VALUE = {
11
- name: 'container-0',
12
- imagePullPolicy: 'Always',
13
- image: '',
14
- };
15
- const BASE_WORKLOAD_SPEC_INIT_VALUE = {
16
- affinity: {},
17
- imagePullSecrets: [],
18
- initContainers: [],
19
- volumes: [],
20
- };
21
- export const DEPLOYMENT_INIT_VALUE = {
22
- apiVersion: 'apps/v1',
23
- kind: 'Deployment',
24
- ...BASE_INIT_VALUE,
25
- spec: {
26
- replicas: 1,
27
- selector: {
28
- matchLabels: {
29
- [DEFAULT_MATCH_LABEL]: '',
30
- },
31
- },
32
- template: {
33
- metadata: {
34
- labels: {
35
- [DEFAULT_MATCH_LABEL]: '',
36
- },
37
- },
38
- spec: {
39
- containers: [BASE_CONTAINER_INIT_VALUE],
40
- restartPolicy: 'Always',
41
- ...BASE_WORKLOAD_SPEC_INIT_VALUE,
42
- },
43
- },
44
- },
45
- };
46
- export const CRONJOB_INIT_VALUE = {
47
- apiVersion: 'batch/v1',
48
- kind: 'CronJob',
49
- ...BASE_INIT_VALUE,
50
- spec: {
51
- schedule: '',
52
- jobTemplate: {
53
- metadata: {
54
- labels: {},
55
- },
56
- spec: {
57
- template: {
58
- spec: {
59
- containers: [BASE_CONTAINER_INIT_VALUE],
60
- restartPolicy: 'Never',
61
- ...BASE_WORKLOAD_SPEC_INIT_VALUE,
62
- },
63
- },
64
- },
65
- },
66
- },
67
- };
68
- export const DAEMONSET_INIT_VALUE = {
69
- apiVersion: 'apps/v1',
70
- kind: 'DaemonSet',
71
- ...BASE_INIT_VALUE,
72
- spec: {
73
- replicas: 1,
74
- selector: {
75
- matchLabels: {
76
- [DEFAULT_MATCH_LABEL]: '',
77
- },
78
- },
79
- template: {
80
- metadata: {
81
- labels: {
82
- [DEFAULT_MATCH_LABEL]: '',
83
- },
84
- },
85
- spec: {
86
- containers: [BASE_CONTAINER_INIT_VALUE],
87
- restartPolicy: 'Always',
88
- ...BASE_WORKLOAD_SPEC_INIT_VALUE,
89
- },
90
- },
91
- },
92
- };
93
- export const JOB_INIT_VALUE = {
94
- apiVersion: 'batch/v1',
95
- kind: 'Job',
96
- ...BASE_INIT_VALUE,
97
- spec: {
98
- replicas: 1,
99
- selector: {
100
- matchLabels: {},
101
- },
102
- template: {
103
- metadata: {
104
- labels: {},
105
- },
106
- spec: {
107
- containers: [BASE_CONTAINER_INIT_VALUE],
108
- restartPolicy: 'Never',
109
- ...BASE_WORKLOAD_SPEC_INIT_VALUE,
110
- },
111
- },
112
- },
113
- };
114
- export const STATEFULSET_INIT_VALUE = {
115
- apiVersion: 'apps/v1',
116
- kind: 'StatefulSet',
117
- ...BASE_INIT_VALUE,
118
- spec: {
119
- replicas: 1,
120
- selector: {
121
- matchLabels: {
122
- [DEFAULT_MATCH_LABEL]: '',
123
- },
124
- },
125
- template: {
126
- metadata: {
127
- labels: {
128
- [DEFAULT_MATCH_LABEL]: '',
129
- },
130
- },
131
- spec: {
132
- containers: [BASE_CONTAINER_INIT_VALUE],
133
- restartPolicy: 'Always',
134
- ...BASE_WORKLOAD_SPEC_INIT_VALUE,
135
- },
136
- },
137
- },
138
- };
139
- export const POD_INIT_VALUE = {
140
- apiVersion: 'v1',
141
- kind: 'Pod',
142
- ...BASE_INIT_VALUE,
143
- spec: {
144
- containers: [BASE_CONTAINER_INIT_VALUE],
145
- },
146
- };
147
- export const SERVICE_INIT_VALUE = {
148
- apiVersion: 'v1',
149
- kind: 'Service',
150
- ...BASE_INIT_VALUE,
151
- spec: {
152
- selector: {},
153
- clusterIP: null,
154
- ports: [
155
- {
156
- name: '',
157
- port: null,
158
- protocol: 'TCP',
159
- targetPort: null,
160
- },
161
- ],
162
- sessionAffinity: 'None',
163
- type: 'ClusterIP',
164
- },
165
- };
166
- export const INGRESS_INIT_VALUE = {
167
- apiVersion: 'networking.k8s.io/v1',
168
- kind: 'Ingress',
169
- ...BASE_INIT_VALUE,
170
- spec: {
171
- rules: [
172
- {
173
- host: 'example.com',
174
- http: {
175
- paths: [
176
- {
177
- path: '/testpath',
178
- pathType: 'Prefix',
179
- backend: {
180
- service: {
181
- name: 'test',
182
- port: {
183
- number: 80,
184
- },
185
- },
186
- },
187
- },
188
- ],
189
- },
190
- },
191
- ],
192
- },
193
- };
194
- export const NETWORK_POLICY_INIT_VALUE = {
195
- apiVersion: 'networking.k8s.io/v1',
196
- kind: 'NetworkPolicy',
197
- ...BASE_INIT_VALUE,
198
- spec: {
199
- podSelector: {},
200
- policyTypes: [],
201
- },
202
- };
203
- export const TIMESTAMP_LABEL = 'sks.user.kubesmart.smtx.io/timestamp';
@@ -1,15 +0,0 @@
1
- export var WorkloadState;
2
- (function (WorkloadState) {
3
- WorkloadState["UPDATEING"] = "updating";
4
- WorkloadState["READY"] = "ready";
5
- WorkloadState["COMPLETED"] = "completed";
6
- WorkloadState["FAILED"] = "failed";
7
- WorkloadState["SUSPENDED"] = "suspended";
8
- WorkloadState["RUNNING"] = "running";
9
- WorkloadState["SUCCEEDED"] = "succeeded";
10
- WorkloadState["UNKNOWN"] = "unknown";
11
- WorkloadState["TERMINATING"] = "terminating";
12
- WorkloadState["PENDING"] = "pending";
13
- WorkloadState["WAITING"] = "waiting";
14
- })(WorkloadState || (WorkloadState = {}));
15
- // export type DeploymentState = WorkloadState.UPDATEING | WorkloadState.READY;
@@ -1,3 +0,0 @@
1
- import { createContext } from 'react';
2
- const ComponentContext = createContext({});
3
- export default ComponentContext;
@@ -1,3 +0,0 @@
1
- import { createContext } from 'react';
2
- const ConfigsContext = createContext({});
3
- export default ConfigsContext;
@@ -1,3 +0,0 @@
1
- import { createContext } from 'react';
2
- const GlobalStoreContext = createContext({});
3
- export default GlobalStoreContext;
@@ -1,3 +0,0 @@
1
- export { default as ComponentContext } from './component';
2
- export { default as GlobalStoreContext } from './global-store';
3
- export { default as ConfigsContext } from './configs';
@@ -1,7 +0,0 @@
1
- export * from './useEagleForm';
2
- export * from './useSchema';
3
- export * from './useDeleteModal';
4
- export * from './useEagleTable';
5
- export * from './useDownloadYAML';
6
- export * from './useEdit';
7
- export * from './useGlobalStore';
@@ -1 +0,0 @@
1
- export * from './useDeleteModal';
@@ -1,31 +0,0 @@
1
- import { useDeleteMany } from '@refinedev/core';
2
- import { useState } from 'react';
3
- import { useD2Translation } from '../../i18n';
4
- export const useDeleteManyModal = (resource, ids) => {
5
- const { mutate } = useDeleteMany();
6
- const [visible, setVisible] = useState(false);
7
- const { t } = useD2Translation();
8
- const modalProps = {
9
- title: t('dovetail.delete'),
10
- okText: t('dovetail.delete'),
11
- okButtonProps: {
12
- danger: true,
13
- },
14
- cancelText: t('dovetail.cancel'),
15
- children: t('dovetail.confirm_delete_text', {
16
- target: ids,
17
- interpolation: { escapeValue: false },
18
- }),
19
- onOk() {
20
- mutate({
21
- resource,
22
- ids,
23
- });
24
- setVisible(false);
25
- },
26
- onCancel() {
27
- setVisible(false);
28
- },
29
- };
30
- return { modalProps, visible, setVisible };
31
- };
@@ -1,38 +0,0 @@
1
- import { useDelete, useNavigation } from '@refinedev/core';
2
- import { useState } from 'react';
3
- import { useD2Translation } from '../../i18n';
4
- export const useDeleteModal = (resource) => {
5
- const { mutate } = useDelete();
6
- const [visible, setVisible] = useState(false);
7
- const navigation = useNavigation();
8
- const [id, setId] = useState('');
9
- const { t } = useD2Translation();
10
- const modalProps = {
11
- title: t('dovetail.delete'),
12
- okText: t('dovetail.delete'),
13
- okButtonProps: {
14
- danger: true,
15
- },
16
- cancelText: t('dovetail.cancel'),
17
- children: t('dovetail.confirm_delete_text', {
18
- target: id,
19
- interpolation: { escapeValue: false },
20
- }),
21
- onOk() {
22
- mutate({
23
- resource,
24
- id,
25
- });
26
- setVisible(false);
27
- navigation.list(resource);
28
- },
29
- onCancel() {
30
- setVisible(false);
31
- },
32
- };
33
- function openDeleteConfirmModal(id) {
34
- setId(id);
35
- setVisible(true);
36
- }
37
- return { modalProps, visible, openDeleteConfirmModal };
38
- };
@@ -1,10 +0,0 @@
1
- import yaml from 'js-yaml';
2
- import { download } from '../utils/download';
3
- export function useDownloadYAML() {
4
- return function (options) {
5
- const { name, item } = options;
6
- console.log('download', item);
7
- const content = yaml.dump(item);
8
- download(`${name}.yaml`, content);
9
- };
10
- }
@@ -1,177 +0,0 @@
1
- import { useUIKit } from '@cloudtower/eagle';
2
- import { useForm as useFormCore, useWarnAboutChange, pickNotDeprecated, useResource, } from '@refinedev/core';
3
- import yaml from 'js-yaml';
4
- import React, { useRef, useState, useMemo } from 'react';
5
- import useK8sYamlEditor from 'src/hooks/useK8sYamlEditor';
6
- import { useSchema } from 'src/hooks/useSchema';
7
- import { pruneBeforeEdit } from 'src/utils/k8s';
8
- import { generateYamlBySchema } from 'src/utils/yaml';
9
- import { useForm as useFormSF } from 'sunflower-antd';
10
- import { useD2Translation } from '../i18n';
11
- import { useGlobalStore } from './useGlobalStore';
12
- const useEagleForm = ({ action, resource, onMutationSuccess: onMutationSuccessProp, onMutationError, submitOnEnter = false, warnWhenUnsavedChanges: warnWhenUnsavedChangesProp, redirect, successNotification, errorNotification, meta, metaData, queryMeta, mutationMeta, liveMode, liveParams, mutationMode, dataProviderName, onLiveEvent, invalidates, undoableTimeout, queryOptions, createMutationOptions, updateMutationOptions, id: idFromProps, overtimeOptions, editorOptions, initialValuesForCreate, } = {}) => {
13
- const editor = useRef(null);
14
- const isFoldRef = useRef(false);
15
- const { t } = useD2Translation();
16
- const [enableEditor, setEnableEditor] = useState(false);
17
- const [isYamlValid, setIsYamlValid] = useState(true);
18
- const [isSchemaValid, setIsSchemaValid] = useState(true);
19
- const [editorErrors, setEditorErrors] = useState([]);
20
- const [errorResponseBody, setErrorResponseBody] = useState(null);
21
- const useResourceResult = useResource();
22
- const { globalStore } = useGlobalStore();
23
- const kit = useUIKit();
24
- const { schema, loading: isLoadingSchema, error: loadSchemaError, fetchSchema, } = useSchema({
25
- skip: editorOptions?.isSkipSchema,
26
- });
27
- const [formAnt] = kit.form.useForm();
28
- const formSF = useFormSF({
29
- form: formAnt,
30
- });
31
- const { form } = formSF;
32
- const { fold } = useK8sYamlEditor();
33
- const useFormCoreResult = useFormCore({
34
- onMutationSuccess: onMutationSuccessProp ? onMutationSuccessProp : undefined,
35
- onMutationError,
36
- redirect,
37
- action,
38
- resource,
39
- successNotification,
40
- errorNotification,
41
- meta: pickNotDeprecated(meta, metaData),
42
- metaData: pickNotDeprecated(meta, metaData),
43
- queryMeta,
44
- mutationMeta,
45
- liveMode,
46
- liveParams,
47
- mutationMode,
48
- dataProviderName,
49
- onLiveEvent,
50
- invalidates,
51
- undoableTimeout,
52
- queryOptions,
53
- createMutationOptions,
54
- updateMutationOptions,
55
- id: idFromProps,
56
- overtimeOptions,
57
- });
58
- const { formLoading, onFinish, queryResult, id } = useFormCoreResult;
59
- const { warnWhenUnsavedChanges: warnWhenUnsavedChangesRefine, setWarnWhen } = useWarnAboutChange();
60
- const warnWhenUnsavedChanges = warnWhenUnsavedChangesProp ?? warnWhenUnsavedChangesRefine;
61
- const initialValues = useMemo(() => {
62
- const initialValues = queryResult?.data?.data
63
- ? globalStore?.restoreItem(queryResult.data.data)
64
- : initialValuesForCreate;
65
- if (initialValues) {
66
- pruneBeforeEdit(initialValues);
67
- }
68
- return initialValues;
69
- }, [queryResult, globalStore, initialValuesForCreate]);
70
- // Init the editor after the resource value is fetched
71
- React.useEffect(() => {
72
- form.resetFields();
73
- if (editor.current) {
74
- const editorValue = yaml.dump(initialValues);
75
- const editorInstance = editor.current.getEditorInstance();
76
- editor.current.setEditorValue(editorValue);
77
- editor.current.setValue(editorValue);
78
- if (queryResult?.data?.data && editorInstance && !isFoldRef.current) {
79
- fold(editorInstance);
80
- isFoldRef.current = true;
81
- }
82
- }
83
- }, [initialValues, queryResult?.data?.data, id, form, fold]);
84
- React.useEffect(() => {
85
- const response = useFormCoreResult.mutationResult.error?.response;
86
- if (response && !response?.bodyUsed) {
87
- response.json?.().then((body) => {
88
- setErrorResponseBody(body);
89
- });
90
- }
91
- }, [useFormCoreResult.mutationResult]);
92
- const onKeyUp = (event) => {
93
- if (submitOnEnter && event.key === 'Enter') {
94
- form.submit();
95
- }
96
- };
97
- const onValuesChange = (changeValues) => {
98
- if (changeValues && warnWhenUnsavedChanges) {
99
- setWarnWhen(true);
100
- }
101
- return changeValues;
102
- };
103
- const saveButtonProps = useMemo(() => ({
104
- loading: formLoading,
105
- onClick: () => {
106
- form.submit();
107
- },
108
- }), [formLoading, form]);
109
- const editorProps = useMemo(() => ({
110
- ref: editor,
111
- defaultValue: schema && editorOptions?.isGenerateAnnotations
112
- ? generateYamlBySchema(initialValues || {}, schema)
113
- : yaml.dump(initialValues),
114
- schema: schema,
115
- id: useResourceResult.resource?.name || '',
116
- errorMsgs: editorErrors,
117
- onValidate(yamlValid, schemaValid) {
118
- setIsYamlValid(yamlValid);
119
- setIsSchemaValid(schemaValid);
120
- if (yamlValid && schemaValid) {
121
- setEditorErrors([]);
122
- }
123
- },
124
- onEditorCreate(editor) {
125
- if (queryResult?.data?.data && !isFoldRef.current) {
126
- fold(editor);
127
- isFoldRef.current = true;
128
- }
129
- },
130
- }), [editorErrors, editorOptions, fold, initialValues, queryResult?.data?.data, schema, useResourceResult.resource?.name]);
131
- return {
132
- form: formSF.form,
133
- formProps: {
134
- ...formSF.formProps,
135
- onFinish: values => {
136
- const errors = [
137
- !isYamlValid ? t('dovetail.yaml_format_wrong') : '',
138
- !isSchemaValid ? t('dovetail.yaml_value_wrong') : '',
139
- ].filter(error => !!error);
140
- if (errors.length) {
141
- setEditorErrors(errors);
142
- return;
143
- }
144
- const finalValues = editor.current
145
- ? yaml.load(editor.current?.getEditorValue() || '')
146
- : values;
147
- return onFinish(finalValues);
148
- },
149
- onKeyUp,
150
- onValuesChange,
151
- initialValues,
152
- },
153
- saveButtonProps,
154
- ...useFormCoreResult,
155
- editorProps,
156
- enableEditor,
157
- errorResponseBody,
158
- schema,
159
- isLoadingSchema,
160
- loadSchemaError,
161
- fetchSchema,
162
- switchEditor() {
163
- if (enableEditor && editor.current?.getEditorValue()) {
164
- const value = yaml.load(editor.current?.getEditorValue());
165
- form?.setFieldsValue(value);
166
- }
167
- setEnableEditor(!enableEditor);
168
- },
169
- onFinish: async (values) => {
170
- const finalValues = enableEditor
171
- ? yaml.load(editor.current?.getEditorValue() || '')
172
- : values ?? formSF.form.getFieldsValue(true);
173
- return await onFinish(finalValues);
174
- },
175
- };
176
- };
177
- export default useEagleForm;