|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Box, Button, Dialog, DialogContent, DialogProps, DialogTitle, Tab, Tabs } from '@mui/material'; |
| 9 | +import { FunctionComponent, useCallback, useMemo, useState } from 'react'; |
| 10 | +import { useIntl } from 'react-intl'; |
| 11 | +import { useLoadflowModifications } from './use-loadflow-modifications'; |
| 12 | +import { CustomAGGrid } from '@gridsuite/commons-ui'; |
| 13 | +import { AGGRID_LOCALES } from 'translations/not-intl/aggrid-locales'; |
| 14 | +import { GridReadyEvent, RowDataUpdatedEvent, ValueFormatterParams } from 'ag-grid-community'; |
| 15 | +import { SortWay } from 'types/custom-aggrid-types'; |
| 16 | + |
| 17 | +const styles = { |
| 18 | + container: { |
| 19 | + display: 'flex', |
| 20 | + position: 'relative', |
| 21 | + }, |
| 22 | + tabs: { |
| 23 | + position: 'relative', |
| 24 | + top: 0, |
| 25 | + left: 0, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +interface LoadflowModificationsProps extends DialogProps { |
| 30 | + onClose: () => void; |
| 31 | +} |
| 32 | + |
| 33 | +export const LoadflowModifications: FunctionComponent<LoadflowModificationsProps> = ({ open, onClose }) => { |
| 34 | + const intl = useIntl(); |
| 35 | + const [tabIndex, setTabIndex] = useState(0); |
| 36 | + const [data, isLoading] = useLoadflowModifications(); |
| 37 | + |
| 38 | + const makeAggridColumnDef = useCallback( |
| 39 | + (field: string, translationId: string) => ({ |
| 40 | + headerName: intl.formatMessage({ id: translationId }), |
| 41 | + field: field, |
| 42 | + colId: field, |
| 43 | + headerComponentParams: { displayName: intl.formatMessage({ id: translationId }) }, |
| 44 | + }), |
| 45 | + [intl] |
| 46 | + ); |
| 47 | + |
| 48 | + const twtColumnDefs = useMemo(() => { |
| 49 | + return [ |
| 50 | + { ...makeAggridColumnDef('twoWindingsTransformerId', 'Id'), sort: SortWay.ASC }, |
| 51 | + makeAggridColumnDef('initialTapPosition', 'loadflowModificationsTapIn'), |
| 52 | + makeAggridColumnDef('solvedTapPosition', 'loadflowModificationsTapOut'), |
| 53 | + { |
| 54 | + ...makeAggridColumnDef('type', 'Type'), |
| 55 | + valueFormatter: (params: ValueFormatterParams) => intl.formatMessage({ id: params.value }), |
| 56 | + }, |
| 57 | + ]; |
| 58 | + }, [intl, makeAggridColumnDef]); |
| 59 | + |
| 60 | + const scColumnDefs = useMemo(() => { |
| 61 | + return [ |
| 62 | + { ...makeAggridColumnDef('shuntCompensatorId', 'Id'), sort: SortWay.ASC }, |
| 63 | + makeAggridColumnDef('initialSectionCount', 'loadflowModificationsSectionCountIn'), |
| 64 | + makeAggridColumnDef('solvedSectionCount', 'loadflowModificationsSectionCountOut'), |
| 65 | + ]; |
| 66 | + }, [makeAggridColumnDef]); |
| 67 | + |
| 68 | + const onGridReady = useCallback(({ api }: GridReadyEvent) => { |
| 69 | + api?.sizeColumnsToFit(); |
| 70 | + }, []); |
| 71 | + |
| 72 | + const onRowDataUpdated = useCallback((params: RowDataUpdatedEvent) => { |
| 73 | + params.api.sizeColumnsToFit(); |
| 74 | + }, []); |
| 75 | + |
| 76 | + const displayedData = useMemo(() => { |
| 77 | + return tabIndex === 0 ? data?.twoWindingsTransformerModifications : data?.shuntCompensatorModifications; |
| 78 | + }, [data?.shuntCompensatorModifications, data?.twoWindingsTransformerModifications, tabIndex]); |
| 79 | + |
| 80 | + const displayedDataColDef = useMemo(() => { |
| 81 | + return tabIndex === 0 ? twtColumnDefs : scColumnDefs; |
| 82 | + }, [scColumnDefs, tabIndex, twtColumnDefs]); |
| 83 | + |
| 84 | + const defaultColDef = { |
| 85 | + resizable: false, |
| 86 | + suppressMovable: true, |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <Dialog |
| 91 | + PaperProps={{ |
| 92 | + sx: { |
| 93 | + height: '90vh', |
| 94 | + }, |
| 95 | + }} |
| 96 | + fullWidth |
| 97 | + maxWidth="md" |
| 98 | + open={true} |
| 99 | + > |
| 100 | + <DialogTitle> |
| 101 | + {intl.formatMessage({ |
| 102 | + id: 'loadflowModifications', |
| 103 | + })} |
| 104 | + </DialogTitle> |
| 105 | + <DialogContent style={{ display: 'flex', flexDirection: 'column' }}> |
| 106 | + <Box sx={styles.container}> |
| 107 | + <Box sx={styles.tabs}> |
| 108 | + <Tabs value={tabIndex} onChange={(_event, newTabIndex) => setTabIndex(newTabIndex)}> |
| 109 | + <Tab label={intl.formatMessage({ id: 'Transformers' })} /> |
| 110 | + <Tab label={intl.formatMessage({ id: 'SHUNT_COMPENSATOR' })} /> |
| 111 | + </Tabs> |
| 112 | + </Box> |
| 113 | + </Box> |
| 114 | + <Box mt={1} style={{ flexGrow: 1 }}> |
| 115 | + <CustomAGGrid |
| 116 | + defaultColDef={defaultColDef} |
| 117 | + rowData={displayedData} |
| 118 | + columnDefs={displayedDataColDef} |
| 119 | + rowSelection="single" |
| 120 | + overrideLocales={AGGRID_LOCALES} |
| 121 | + loading={isLoading} |
| 122 | + onGridReady={onGridReady} |
| 123 | + onRowDataUpdated={onRowDataUpdated} |
| 124 | + /> |
| 125 | + </Box> |
| 126 | + <Button onClick={onClose}>Fermer</Button> |
| 127 | + </DialogContent> |
| 128 | + </Dialog> |
| 129 | + ); |
| 130 | +}; |
0 commit comments