|
| 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 { BUILD_STATUS } from 'components/network/constants'; |
| 9 | +import React, { useCallback, useState } from 'react'; |
| 10 | +import { PlayCircleFilled, StopCircleOutlined } from '@mui/icons-material'; |
| 11 | +import { Button, CircularProgress, Theme } from '@mui/material'; |
| 12 | +import { buildNode, unbuildNode } from '../../../services/study'; |
| 13 | +import { UUID } from 'crypto'; |
| 14 | +import { useSnackMessage } from '@gridsuite/commons-ui'; |
| 15 | +import { HTTP_MAX_NODE_BUILDS_EXCEEDED_MESSAGE } from 'components/network-modification-tree-pane'; |
| 16 | + |
| 17 | +type BuildButtonProps = { |
| 18 | + buildStatus?: BUILD_STATUS; |
| 19 | + studyUuid: UUID | null; |
| 20 | + currentRootNetworkUuid: UUID | null; |
| 21 | + nodeUuid: UUID; |
| 22 | +}; |
| 23 | + |
| 24 | +const styles = { |
| 25 | + button: { |
| 26 | + minWidth: '40px', |
| 27 | + }, |
| 28 | + playColor: (theme: Theme) => ({ |
| 29 | + color: theme.palette.mode === 'light' ? 'grey' : 'white', |
| 30 | + }), |
| 31 | +}; |
| 32 | + |
| 33 | +export const BuildButton = ({ buildStatus, studyUuid, currentRootNetworkUuid, nodeUuid }: BuildButtonProps) => { |
| 34 | + const [isLoading, setIsLoading] = useState(false); |
| 35 | + const { snackError } = useSnackMessage(); |
| 36 | + |
| 37 | + const handleClick = useCallback( |
| 38 | + (event: React.MouseEvent<HTMLButtonElement>) => { |
| 39 | + event.stopPropagation(); |
| 40 | + if (!studyUuid || !currentRootNetworkUuid || isLoading) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + setIsLoading(true); |
| 45 | + |
| 46 | + if (buildStatus === BUILD_STATUS.NOT_BUILT) { |
| 47 | + buildNode(studyUuid, nodeUuid, currentRootNetworkUuid) |
| 48 | + .catch((error) => { |
| 49 | + if (error.status === 403 && error.message.includes(HTTP_MAX_NODE_BUILDS_EXCEEDED_MESSAGE)) { |
| 50 | + // retrieve last word of the message (ex: "MAX_NODE_BUILDS_EXCEEDED max allowed built nodes : 2" -> 2) |
| 51 | + let limit = error.message.split(/[: ]+/).pop(); |
| 52 | + snackError({ |
| 53 | + messageId: 'maxBuiltNodeExceededError', |
| 54 | + messageValues: { limit: limit }, |
| 55 | + }); |
| 56 | + } else { |
| 57 | + snackError({ |
| 58 | + messageTxt: error.message, |
| 59 | + headerId: 'NodeBuildingError', |
| 60 | + }); |
| 61 | + } |
| 62 | + }) |
| 63 | + .finally(() => { |
| 64 | + setIsLoading(false); |
| 65 | + }); |
| 66 | + } else { |
| 67 | + unbuildNode(studyUuid, nodeUuid, currentRootNetworkUuid) |
| 68 | + .catch((error) => { |
| 69 | + snackError({ |
| 70 | + messageTxt: error.message, |
| 71 | + headerId: 'NodeUnbuildingError', |
| 72 | + }); |
| 73 | + }) |
| 74 | + .finally(() => { |
| 75 | + setIsLoading(false); |
| 76 | + }); |
| 77 | + } |
| 78 | + }, |
| 79 | + [studyUuid, currentRootNetworkUuid, nodeUuid, buildStatus, isLoading, snackError] |
| 80 | + ); |
| 81 | + |
| 82 | + const getIcon = () => { |
| 83 | + if (isLoading) { |
| 84 | + return <CircularProgress size={24} color="primary" />; |
| 85 | + } |
| 86 | + return buildStatus === BUILD_STATUS.NOT_BUILT ? ( |
| 87 | + <PlayCircleFilled sx={styles.playColor} /> |
| 88 | + ) : ( |
| 89 | + <StopCircleOutlined color="primary" /> |
| 90 | + ); |
| 91 | + }; |
| 92 | + |
| 93 | + const isButtonDisabled = isLoading || !studyUuid || !currentRootNetworkUuid; |
| 94 | + |
| 95 | + return ( |
| 96 | + <Button size="small" onClick={handleClick} sx={styles.button} disabled={isButtonDisabled}> |
| 97 | + {getIcon()} |
| 98 | + </Button> |
| 99 | + ); |
| 100 | +}; |
0 commit comments