Skip to content

Commit 19bd0a0

Browse files
committed
ARTESCA-15484: Extract the logic to the hook to avoid rerendering
1 parent 37971d6 commit 19bd0a0

3 files changed

Lines changed: 102 additions & 53 deletions

File tree

src/react/endpoint/DeleteEndpoint.tsx

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@ import { useInstanceId } from '../next-architecture/ui/AuthProvider';
1010
import DeleteConfirmation from '../ui-elements/DeleteConfirmation';
1111
import * as T from '../ui-elements/Table';
1212
import { useShellHooks } from '@scality/module-federation';
13-
import { ArtescaLibraryNotAvailable } from '../next-architecture/ui/ArtescaLibraryProvider';
14-
import { useArtescaLibrary } from '../next-architecture/ui/ArtescaLibraryProvider';
1513

1614
export const DeleteEndpoint = ({
1715
hostname,
18-
isBuiltin,
16+
endpointsDeletionDisabledMap,
17+
endpointsDeletionDisabledStatus,
1918
}: {
2019
hostname: string;
21-
isBuiltin: boolean;
20+
endpointsDeletionDisabledMap: Record<string, boolean>;
21+
endpointsDeletionDisabledStatus: 'idle' | 'loading' | 'error' | 'success';
2222
}) => {
2323
const [isConfirmDeleteOpen, setIsConfirmDeleteOpen] = useState(false);
2424
const accountsLocationsEndpointsAdapter =
2525
useAccountsLocationsEndpointsAdapter();
26-
const {
27-
refetchAccountsLocationsEndpointsMutation,
28-
accountsLocationsAndEndpoints,
29-
status: accountsLocationsEndpointsStatus,
30-
} = useAccountsLocationsAndEndpoints({
31-
accountsLocationsEndpointsAdapter,
32-
});
26+
const { refetchAccountsLocationsEndpointsMutation } =
27+
useAccountsLocationsAndEndpoints({
28+
accountsLocationsEndpointsAdapter,
29+
});
3330
const instanceId = useInstanceId();
3431
const managementClient = useManagementClient();
3532
const { useAuth } = useShellHooks();
@@ -47,37 +44,6 @@ export const DeleteEndpoint = ({
4744
status: waiterStatus,
4845
} = useWaitForRunningConfigurationVersionToBeUpdated();
4946

50-
const artescaLibrary = useArtescaLibrary();
51-
const {
52-
useArtescaPlusVeeamDefaultOrOpenMode,
53-
ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME,
54-
} =
55-
artescaLibrary instanceof ArtescaLibraryNotAvailable
56-
? {
57-
useArtescaPlusVeeamDefaultOrOpenMode: undefined,
58-
ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME: undefined,
59-
}
60-
: artescaLibrary;
61-
const {
62-
artescaPlusVeeamDefaultOrOpenMode,
63-
artescaPlusVeeamDefaultOrOpenModeStatus,
64-
} = useArtescaPlusVeeamDefaultOrOpenMode();
65-
66-
const isDisabledForArtescaPlusVeeam =
67-
hostname === ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME;
68-
69-
// Disable endpoint deletion when there is only one non-Veeam, non-builtin endpoint remaining
70-
// to avoid going back to default mode
71-
const isLastNonVeeamEndpoint =
72-
accountsLocationsEndpointsStatus === 'success' &&
73-
accountsLocationsAndEndpoints.endpoints.filter(
74-
(endpoint) =>
75-
endpoint.hostname !== ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME &&
76-
endpoint.isBuiltin === false,
77-
).length === 1;
78-
const isDisabledForOpenMode =
79-
artescaPlusVeeamDefaultOrOpenMode === 'open' && isLastNonVeeamEndpoint;
80-
8147
const handleDeleteApprove = () => {
8248
setReferenceVersion({
8349
onRefTaken: () => {
@@ -97,12 +63,8 @@ export const DeleteEndpoint = ({
9763
}
9864
}, [waiterStatus, refetchAccountsLocationsEndpointsMutation]);
9965

100-
const tooltipMessage = isBuiltin
66+
const tooltipMessage = endpointsDeletionDisabledMap[hostname]
10167
? 'This Data Service can not be deleted'
102-
: isDisabledForArtescaPlusVeeam
103-
? 'This is the Data Service created for Artesca + Veeam deployment and it should not be deleted'
104-
: isDisabledForOpenMode
105-
? 'The deletion of Data Services has been disabled for Open Mode'
10668
: 'Delete Data Service';
10769

10870
return (
@@ -117,10 +79,8 @@ export const DeleteEndpoint = ({
11779
titleText={`Are you sure you want to delete Data Service: ${hostname} ?`}
11880
/>
11981
<T.ActionButton
120-
disabled={
121-
isBuiltin || isDisabledForArtescaPlusVeeam || isDisabledForOpenMode
122-
}
123-
isLoading={artescaPlusVeeamDefaultOrOpenModeStatus === 'loading'}
82+
disabled={endpointsDeletionDisabledMap[hostname]}
83+
isLoading={endpointsDeletionDisabledStatus === 'loading'}
12484
icon={<Icon name="Delete" />}
12585
tooltip={{
12686
overlay: tooltipMessage,

src/react/endpoint/EndpointList.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ArtescaLibraryNotAvailable,
1919
TOOLTIP_ARTESCA_PLUS_VEEAM_DEFAULT_MODE,
2020
} from '../next-architecture/ui/ArtescaLibraryProvider';
21+
import useEndpointsDeletionDisabled from './useEndpointsDeletionDisabled';
2122

2223
type CellProps = {
2324
row: {
@@ -61,6 +62,11 @@ function EndpointList({ endpoints, locations }: Props) {
6162
artescaPlusVeeamDefaultOrOpenModeStatus,
6263
} = useArtescaPlusVeeamDefaultOrOpenMode();
6364

65+
const {
66+
endpointsDeletionDisabledMap,
67+
status: endpointsDeletionDisabledStatus,
68+
} = useEndpointsDeletionDisabled();
69+
6470
const columns = useMemo(
6571
() => [
6672
{
@@ -114,13 +120,14 @@ function EndpointList({ endpoints, locations }: Props) {
114120
return (
115121
<DeleteEndpoint
116122
hostname={original.hostname}
117-
isBuiltin={original.isBuiltin}
123+
endpointsDeletionDisabledMap={endpointsDeletionDisabledMap}
124+
endpointsDeletionDisabledStatus={endpointsDeletionDisabledStatus}
118125
/>
119126
);
120127
},
121128
},
122129
],
123-
[locations],
130+
[locations, endpointsDeletionDisabledMap, endpointsDeletionDisabledStatus],
124131
);
125132

126133
return (
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { useMemo } from 'react';
2+
import { useAccountsLocationsAndEndpoints } from '../next-architecture/domain/business/accounts';
3+
import { useAccountsLocationsEndpointsAdapter } from '../next-architecture/ui/AccountsLocationsEndpointsAdapterProvider';
4+
import {
5+
ArtescaLibraryNotAvailable,
6+
useArtescaLibrary,
7+
} from '../next-architecture/ui/ArtescaLibraryProvider';
8+
9+
// return the list of the endpoint name and a boolean indicating if the endpoint is deletion disabled
10+
const useEndpointsDeletionDisabled = (): {
11+
endpointsDeletionDisabledMap: Record<string, boolean>;
12+
status: 'idle' | 'loading' | 'error' | 'success';
13+
} => {
14+
const accountsLocationsEndpointsAdapter =
15+
useAccountsLocationsEndpointsAdapter();
16+
const {
17+
accountsLocationsAndEndpoints,
18+
status: accountsLocationsEndpointsStatus,
19+
} = useAccountsLocationsAndEndpoints({
20+
accountsLocationsEndpointsAdapter,
21+
});
22+
const artescaLibrary = useArtescaLibrary();
23+
const {
24+
useArtescaPlusVeeamDefaultOrOpenMode,
25+
ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME,
26+
} =
27+
artescaLibrary instanceof ArtescaLibraryNotAvailable
28+
? {
29+
useArtescaPlusVeeamDefaultOrOpenMode: undefined,
30+
ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME: undefined,
31+
}
32+
: artescaLibrary;
33+
const {
34+
artescaPlusVeeamDefaultOrOpenMode,
35+
artescaPlusVeeamDefaultOrOpenModeStatus,
36+
} = useArtescaPlusVeeamDefaultOrOpenMode();
37+
38+
// Disable endpoint deletion when there is only one non-Veeam, non-builtin endpoint remaining
39+
// to avoid going back to default mode
40+
const isLastNonVeeamEndpoint =
41+
accountsLocationsEndpointsStatus === 'success' &&
42+
accountsLocationsAndEndpoints.endpoints.filter(
43+
(endpoint) =>
44+
endpoint.hostname !== ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME &&
45+
endpoint.isBuiltin === false,
46+
).length === 1;
47+
48+
const isDisabledForOpenMode =
49+
artescaPlusVeeamDefaultOrOpenMode === 'open' && isLastNonVeeamEndpoint;
50+
51+
const endpointsDeletionDisabledMap = useMemo(() => {
52+
const record: Record<string, boolean> = {};
53+
accountsLocationsAndEndpoints.endpoints.forEach((endpoint) => {
54+
const hostname = endpoint.hostname;
55+
record[hostname] =
56+
hostname === ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME ||
57+
isDisabledForOpenMode ||
58+
endpoint.isBuiltin;
59+
});
60+
return record;
61+
}, [
62+
accountsLocationsAndEndpoints.endpoints,
63+
ARTESCA_PLUS_VEEAM_S3_ENDPOINT_NAME,
64+
isDisabledForOpenMode,
65+
]);
66+
67+
const status =
68+
artescaPlusVeeamDefaultOrOpenModeStatus === 'loading' ||
69+
accountsLocationsEndpointsStatus === 'loading'
70+
? 'loading'
71+
: artescaPlusVeeamDefaultOrOpenModeStatus === 'error' ||
72+
accountsLocationsEndpointsStatus === 'error'
73+
? 'error'
74+
: accountsLocationsEndpointsStatus === 'success' &&
75+
artescaPlusVeeamDefaultOrOpenModeStatus === 'success'
76+
? 'success'
77+
: 'idle';
78+
79+
return { endpointsDeletionDisabledMap, status };
80+
};
81+
82+
export default useEndpointsDeletionDisabled;

0 commit comments

Comments
 (0)