Skip to content

Commit 5d2af7c

Browse files
committed
refactor
1 parent 7309a85 commit 5d2af7c

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

src/components/ControlPlane/ManagedResources.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTranslation } from 'react-i18next';
2-
import { Fragment, useMemo, useState, useContext, useRef, useCallback } from 'react';
2+
import { Fragment, useMemo, useState, useRef, useCallback } from 'react';
33
import {
44
AnalyticalTable,
55
AnalyticalTableColumnDefinition,
@@ -32,12 +32,9 @@ import {
3232
import { useResourcePluralNames } from '../../hooks/useResourcePluralNames';
3333
import { useSplitter } from '../Splitter/SplitterContext.tsx';
3434
import { YamlSidePanel } from '../Yaml/YamlSidePanel.tsx';
35-
36-
import { ApiConfigContext } from '../Shared/k8s';
3735
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
3836
import { APIError } from '../../lib/api/error.ts';
39-
40-
import { handleResourcePatch } from '../../lib/api/types/crossplane/handleResourcePatch.ts';
37+
import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandleResourcePatch.ts';
4138

4239
interface StatusFilterColumn {
4340
filterValue?: string;
@@ -64,9 +61,9 @@ export function ManagedResources() {
6461
const { t } = useTranslation();
6562
const toast = useToast();
6663
const { openInAside } = useSplitter();
67-
const apiConfig = useContext(ApiConfigContext);
6864
const [pendingDeleteItem, setPendingDeleteItem] = useState<ManagedResourceItem | null>(null);
6965
const errorDialogRef = useRef<ErrorDialogHandle>(null);
66+
const handlePatch = useHandleResourcePatch(errorDialogRef);
7067

7168
const {
7269
data: managedResources,
@@ -104,22 +101,12 @@ export function ManagedResources() {
104101
isEdit={true}
105102
resource={item as unknown as Resource}
106103
filename={`${item.kind}_${item.metadata.name}`}
107-
onApply={async (parsed) =>
108-
await handleResourcePatch({
109-
item,
110-
parsed,
111-
getPluralKind,
112-
apiConfig,
113-
t,
114-
toast,
115-
errorDialogRef,
116-
})
117-
}
104+
onApply={async (parsed) => await handlePatch(item, parsed)}
118105
/>
119106
</Fragment>,
120107
);
121108
},
122-
[openInAside, getPluralKind, apiConfig, t, toast, errorDialogRef],
109+
[openInAside, handlePatch],
123110
);
124111

125112
const columns = useMemo<AnalyticalTableColumnDefinition[]>(

src/components/ControlPlane/ProvidersConfig.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ import { formatDateAsTimeAgo } from '../../utils/i18n/timeAgo';
1515

1616
import { YamlViewButton } from '../Yaml/YamlViewButton.tsx';
1717

18-
import { Fragment, useCallback, useContext, useMemo, useRef } from 'react';
18+
import { Fragment, useCallback, useMemo, useRef } from 'react';
1919
import { Resource } from '../../utils/removeManagedFieldsAndFilterData.ts';
2020
import { ProviderConfigItem } from '../../lib/shared/types.ts';
2121
import { ProviderConfigsRowActionsMenu } from './ProviderConfigsActionMenu.tsx';
2222
import { useSplitter } from '../Splitter/SplitterContext.tsx';
2323
import { YamlSidePanel } from '../Yaml/YamlSidePanel.tsx';
24-
import { handleResourcePatch } from '../../lib/api/types/crossplane/handleResourcePatch.ts';
25-
import { useToast } from '../../context/ToastContext.tsx';
26-
import { useResourcePluralNames } from '../../hooks/useResourcePluralNames';
27-
import { ApiConfigContext } from '../Shared/k8s';
24+
import { useHandleResourcePatch } from '../../lib/api/types/crossplane/useHandleResourcePatch.ts';
2825
import { ErrorDialog, ErrorDialogHandle } from '../Shared/ErrorMessageBox.tsx';
2926

3027
type Rows = {
@@ -42,18 +39,15 @@ interface CellRow<T> {
4239
export function ProvidersConfig() {
4340
const { t } = useTranslation();
4441
const { openInAside } = useSplitter();
45-
const toast = useToast();
46-
const apiConfig = useContext(ApiConfigContext);
4742
const errorDialogRef = useRef<ErrorDialogHandle>(null);
43+
const handlePatch = useHandleResourcePatch(errorDialogRef);
4844

4945
const rows: Rows[] = [];
5046

5147
const { data: providerConfigsList, isLoading } = useProvidersConfigResource({
5248
refreshInterval: 60000, // Resources are quite expensive to fetch, so we refresh every 60 seconds
5349
});
5450

55-
const { getPluralKind } = useResourcePluralNames();
56-
5751
if (providerConfigsList) {
5852
providerConfigsList.forEach((provider) => {
5953
provider.items.forEach((config) => {
@@ -77,25 +71,15 @@ export function ProvidersConfig() {
7771
isEdit={true}
7872
resource={item as unknown as Resource}
7973
filename={`${item.kind}_${item.metadata.name}`}
80-
onApply={async (parsed) =>
81-
await handleResourcePatch({
82-
item,
83-
parsed,
84-
getPluralKind,
85-
apiConfig,
86-
t,
87-
toast,
88-
errorDialogRef,
89-
})
90-
}
74+
onApply={async (parsed) => await handlePatch(item, parsed)}
9175
/>
9276
</Fragment>,
9377
);
9478
},
95-
[openInAside, getPluralKind, apiConfig, t, toast],
79+
[openInAside, handlePatch],
9680
);
9781

98-
const columns = useMemo(
82+
const columns = useMemo<AnalyticalTableColumnDefinition[]>(
9983
() =>
10084
[
10185
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useContext } from 'react';
2+
import type { RefObject } from 'react';
3+
import { useTranslation } from 'react-i18next';
4+
import { ApiConfigContext } from '../../../../components/Shared/k8s';
5+
import { useToast } from '../../../../context/ToastContext.tsx';
6+
import { useResourcePluralNames } from '../../../../hooks/useResourcePluralNames';
7+
import { ErrorDialogHandle } from '../../../../components/Shared/ErrorMessageBox.tsx';
8+
import { fetchApiServerJson } from '../../fetch.ts';
9+
import { APIError } from '../../error.ts';
10+
11+
export type PatchableResourceRef = {
12+
kind: string;
13+
apiVersion?: string;
14+
metadata: {
15+
name: string;
16+
namespace?: string;
17+
};
18+
};
19+
20+
export const useHandleResourcePatch = (errorDialogRef?: RefObject<ErrorDialogHandle | null>) => {
21+
const { t } = useTranslation();
22+
const toast = useToast();
23+
const apiConfig = useContext(ApiConfigContext);
24+
const { getPluralKind } = useResourcePluralNames();
25+
26+
return async (item: PatchableResourceRef, parsed: unknown): Promise<boolean> => {
27+
const resourceName = item?.metadata?.name ?? '';
28+
const apiVersion = item?.apiVersion ?? '';
29+
const pluralKind = getPluralKind(item.kind);
30+
const namespace = item?.metadata?.namespace;
31+
32+
toast.show(t('ManagedResources.patchStarted', { resourceName }));
33+
34+
try {
35+
const basePath = `/apis/${apiVersion}`;
36+
const path = namespace
37+
? `${basePath}/namespaces/${namespace}/${pluralKind}/${resourceName}`
38+
: `${basePath}/${pluralKind}/${resourceName}`;
39+
40+
await fetchApiServerJson(path, apiConfig, undefined, 'PATCH', JSON.stringify(parsed));
41+
toast.show(t('ManagedResources.patchSuccess', { resourceName }));
42+
return true;
43+
} catch (e) {
44+
toast.show(t('ManagedResources.patchError', { resourceName }));
45+
if (e instanceof APIError && errorDialogRef?.current) {
46+
errorDialogRef.current.showErrorDialog(`${e.message}: ${JSON.stringify(e.info)}`);
47+
}
48+
console.error('Failed to patch resource', e);
49+
return false;
50+
}
51+
};
52+
};

0 commit comments

Comments
 (0)