-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontingency-table.tsx
More file actions
115 lines (109 loc) · 4.71 KB
/
contingency-table.tsx
File metadata and controls
115 lines (109 loc) · 4.71 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
/**
* Copyright (c) 2026, 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 { Alert, Stack } from '@mui/material';
import { useCallback, useEffect, useState } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import { FieldValues, useWatch } from 'react-hook-form';
import { useCreateRowData } from '../../../../hooks';
import { ColumnsDef, ACTIVATED, ParameterTable, CONTAINER_ID } from '../parameter-table';
import { CONTINGENCY_LISTS, CONTINGENCY_LISTS_INFOS } from '../constants';
import { COLUMNS_DEFINITIONS_CONTINGENCY_LISTS_INFOS, ParamContingencyLists } from './columns-definitions';
import { ContingencyCount, ContingencyListsInfos } from './types';
export function ContingencyTable({
showContingencyCount = false,
fetchContingencyCount,
isBuiltCurrentNode,
}: Readonly<{
showContingencyCount: boolean;
fetchContingencyCount?: (contingencyLists: string[] | null) => Promise<ContingencyCount>;
isBuiltCurrentNode?: boolean; // necessary if we want to show the contingency count
}>) {
const intl = useIntl();
const [simulatedContingencyCount, setSimulatedContingencyCount] = useState<ContingencyCount | null>(null);
const [rowData, useFieldArrayOutput] = useCreateRowData(ParamContingencyLists);
const contingencyListsInfos: ContingencyListsInfos[] = useWatch({ name: CONTINGENCY_LISTS_INFOS });
const getColumnsDefinition = useCallback(
(columns: ColumnsDef[]) => {
if (columns) {
return columns.map((column) => ({
...column,
label: intl.formatMessage({ id: column.label }),
}));
}
return [];
},
[intl]
);
useEffect(() => {
if (showContingencyCount) {
const hasNoContingencies =
!contingencyListsInfos ||
(contingencyListsInfos.length ?? 0) === 0 ||
contingencyListsInfos.every(
(contingencyList) => (contingencyList[CONTINGENCY_LISTS]?.length ?? 0) === 0
);
if (hasNoContingencies) {
setSimulatedContingencyCount(null);
return;
}
fetchContingencyCount?.(
contingencyListsInfos
.filter((lists) => lists[ACTIVATED])
.flatMap((lists) =>
lists[CONTINGENCY_LISTS]?.map((contingencyList) => contingencyList[CONTAINER_ID])
)
)
.then((contingencyCount) => {
setSimulatedContingencyCount(contingencyCount);
})
.catch(() => {
setSimulatedContingencyCount(null);
});
}
}, [contingencyListsInfos, fetchContingencyCount, showContingencyCount]);
const renderContingencyCount = () => {
if (!isBuiltCurrentNode) {
return (
<Alert variant="standard" severity="warning" sx={{ color: 'text.primary' }}>
<FormattedMessage id="contingencyCountImpossibleOnUnbuiltNode" />
</Alert>
);
}
if (simulatedContingencyCount?.contingencies === 0 && simulatedContingencyCount.notFoundElements === 0) {
return (
<Alert variant="standard" severity="error" sx={{ color: 'text.primary' }}>
<FormattedMessage id="noContingency" />
</Alert>
);
}
return (
<Alert variant="standard" icon={false} severity="info" sx={{ color: 'text.primary' }}>
<FormattedMessage
id="xContingenciesWillBeSimulatedAndYNotFound"
values={{
x: simulatedContingencyCount?.contingencies ?? '...',
y: simulatedContingencyCount?.notFoundElements ?? '...',
}}
/>
</Alert>
);
};
return (
<Stack spacing={0} sx={{ width: '100%' }}>
<ParameterTable
arrayFormName={CONTINGENCY_LISTS_INFOS}
useFieldArrayOutput={useFieldArrayOutput}
columnsDefinition={getColumnsDefinition(COLUMNS_DEFINITIONS_CONTINGENCY_LISTS_INFOS)}
tableHeight={270}
createRows={rowData}
onFormChanged={() => {}}
isValidParameterRow={(row: FieldValues) => row[CONTINGENCY_LISTS]?.length > 0}
/>
{showContingencyCount && renderContingencyCount()}
</Stack>
);
}