-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsecurity-analysis-parameters-inline.tsx
More file actions
167 lines (154 loc) · 7.63 KB
/
security-analysis-parameters-inline.tsx
File metadata and controls
167 lines (154 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { useCallback, useEffect, useState } from 'react';
import { Box, Grid } from '@mui/material';
import { FormattedMessage, useIntl } from 'react-intl';
import type { UUID } from 'node:crypto';
import { ElementType, mergeSx, UseParametersBackendReturnProps } from '../../../utils';
import { ComputingType, CreateParameterDialog, LabelledButton, LineSeparator } from '../common';
import { useSnackMessage } from '../../../hooks';
import { TreeViewFinderNodeProps } from '../../treeViewFinder';
import { SubmitButton } from '../../inputs';
import { parametersStyles } from '../parameters-style';
import { DirectoryItemSelector } from '../../directoryItemSelector';
import { fetchSecurityAnalysisParameters } from '../../../services/security-analysis';
import { useSecurityAnalysisParametersForm } from './use-security-analysis-parameters-form';
import { SecurityAnalysisParametersForm } from './security-analysis-parameters-form';
import { PopupConfirmationDialog } from '../../dialogs';
import { snackWithFallback } from '../../../utils/error';
import { SAParameters } from './types';
import { toFormValueSaParameters } from './columns-definitions';
import { ContingencyCount } from '../common/contingency-table/types';
export function SecurityAnalysisParametersInline({
studyUuid,
parametersBackend,
fetchContingencyCount,
isBuiltCurrentNode,
setHaveDirtyFields,
isDeveloperMode,
}: Readonly<{
studyUuid: UUID | null;
parametersBackend: UseParametersBackendReturnProps<ComputingType.SECURITY_ANALYSIS>;
fetchContingencyCount: (contingencyListIds: string[] | null) => Promise<ContingencyCount>;
isBuiltCurrentNode: boolean;
setHaveDirtyFields: (isDirty: boolean) => void;
isDeveloperMode: boolean;
}>) {
const securityAnalysisMethods = useSecurityAnalysisParametersForm(parametersBackend, null, null, null);
const { resetParameters } = parametersBackend;
const intl = useIntl();
const [openCreateParameterDialog, setOpenCreateParameterDialog] = useState(false);
const [openSelectParameterDialog, setOpenSelectParameterDialog] = useState(false);
const [openResetConfirmation, setOpenResetConfirmation] = useState(false);
const { snackError } = useSnackMessage();
const { handleSubmit, formState, reset, getValues } = securityAnalysisMethods.formMethods;
const executeResetAction = useCallback(() => {
resetParameters();
setOpenResetConfirmation(false);
}, [resetParameters]);
const handleResetAllClick = useCallback(() => {
setOpenResetConfirmation(true);
}, []);
const handleCancelReset = useCallback(() => {
setOpenResetConfirmation(false);
}, []);
const handleLoadParameter = useCallback(
(newParams: TreeViewFinderNodeProps[]) => {
if (newParams && newParams.length > 0) {
setOpenSelectParameterDialog(false);
fetchSecurityAnalysisParameters(newParams[0].id)
.then((parameters: SAParameters) => {
console.info(`loading the following security analysis parameters : ${parameters.uuid}`);
reset(toFormValueSaParameters(parameters), {
keepDefaultValues: true,
});
})
.catch((error) => {
snackWithFallback(snackError, error, { headerId: 'paramsRetrievingError' });
});
}
setOpenSelectParameterDialog(false);
},
[reset, snackError]
);
useEffect(() => {
setHaveDirtyFields(!!Object.keys(formState.dirtyFields).length);
}, [formState, setHaveDirtyFields]);
return (
<SecurityAnalysisParametersForm
securityAnalysisMethods={securityAnalysisMethods}
showContingencyCount
fetchContingencyCount={fetchContingencyCount}
isBuiltCurrentNode={isBuiltCurrentNode}
isDeveloperMode={isDeveloperMode}
renderActions={() => {
return (
<>
<Box sx={{ flexGrow: 0 }}>
<LineSeparator />
<Grid
container
item
sx={mergeSx(parametersStyles.controlParametersItem, parametersStyles.marginTopButton, {
paddingBottom: 0,
})}
>
<LabelledButton
callback={() => setOpenSelectParameterDialog(true)}
label="settings.button.chooseSettings"
/>
<LabelledButton callback={() => setOpenCreateParameterDialog(true)} label="save" />
<LabelledButton callback={handleResetAllClick} label="resetToDefault" />
<SubmitButton
onClick={handleSubmit(securityAnalysisMethods.onSaveInline)}
variant="outlined"
>
<FormattedMessage id="validate" />
</SubmitButton>
</Grid>
</Box>
{openCreateParameterDialog && (
<CreateParameterDialog
studyUuid={studyUuid}
open={openCreateParameterDialog}
onClose={() => setOpenCreateParameterDialog(false)}
parameterValues={() => securityAnalysisMethods.formatNewParams(getValues())}
parameterFormatter={(newParams) => newParams}
parameterType={ElementType.SECURITY_ANALYSIS_PARAMETERS}
/>
)}
{openSelectParameterDialog && (
<DirectoryItemSelector
open={openSelectParameterDialog}
onClose={handleLoadParameter}
types={[ElementType.SECURITY_ANALYSIS_PARAMETERS]}
title={intl.formatMessage({
id: 'showSelectParameterDialog',
})}
onlyLeaves
multiSelect={false}
validationButtonText={intl.formatMessage({
id: 'validate',
})}
/>
)}
{/* Reset Confirmation Dialog */}
{openResetConfirmation && (
<PopupConfirmationDialog
message="resetParamsConfirmation"
validateButtonLabel="validate"
openConfirmationPopup={openResetConfirmation}
setOpenConfirmationPopup={handleCancelReset}
handlePopupConfirmation={executeResetAction}
/>
)}
</>
);
}}
/>
);
}