|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @author [email protected] (Liz Looney) |
| 20 | + */ |
| 21 | + |
| 22 | +import React, { useState, useRef } from 'react'; |
| 23 | + |
| 24 | +import { |
| 25 | + Alert, |
| 26 | + Button, |
| 27 | + Flex, |
| 28 | + Input, |
| 29 | + Modal, |
| 30 | + Typography, |
| 31 | +} from 'antd'; |
| 32 | +import type { InputRef } from 'antd'; |
| 33 | + |
| 34 | +import * as commonStorage from '../storage/common_storage'; |
| 35 | + |
| 36 | + |
| 37 | +export type NewModuleNameModalProps = { |
| 38 | + title: string; |
| 39 | + isOpen: boolean; |
| 40 | + initialValue: string; |
| 41 | + getCurrentProjectName: () => string; |
| 42 | + getModuleNames: (projectName: string) => string[]; |
| 43 | + onOk: (newName: string) => void; |
| 44 | + onCancel: () => void; |
| 45 | +} |
| 46 | + |
| 47 | +export const NewModuleNameModal: React.FC<NewModuleNameModalProps> = ({ title, isOpen, initialValue, getCurrentProjectName, getModuleNames, onOk, onCancel }) => { |
| 48 | + const inputRef = useRef<InputRef>(null); |
| 49 | + const [value, setValue] = useState(''); |
| 50 | + const [projectName, setProjectName] = useState(''); |
| 51 | + const [moduleNames, setModuleNames] = useState<string[]>([]); |
| 52 | + const [alertErrorMessage, setAlertErrorMessage] = useState(''); |
| 53 | + const [alertErrorVisible, setAlertErrorVisible] = useState(false); |
| 54 | + |
| 55 | + const afterOpenChange = (open: boolean) => { |
| 56 | + if (open) { |
| 57 | + setValue(initialValue); |
| 58 | + const currentProjectName = getCurrentProjectName(); |
| 59 | + setProjectName(currentProjectName); |
| 60 | + setModuleNames(getModuleNames(currentProjectName)); |
| 61 | + if (inputRef.current) { |
| 62 | + inputRef.current.focus(); |
| 63 | + } |
| 64 | + } else { |
| 65 | + setValue(''); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const handleOk = () => { |
| 70 | + if (!commonStorage.isValidPythonModuleName(value)) { |
| 71 | + setAlertErrorMessage(value + ' is not a valid blocks module name'); |
| 72 | + setAlertErrorVisible(true); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (projectName === value) { |
| 76 | + setAlertErrorMessage('The project is already named ' + value); |
| 77 | + setAlertErrorVisible(true); |
| 78 | + return; |
| 79 | + } |
| 80 | + if (moduleNames.includes(value)) { |
| 81 | + setAlertErrorMessage('Another module is already named ' + value); |
| 82 | + setAlertErrorVisible(true); |
| 83 | + return; |
| 84 | + } |
| 85 | + onOk(value); |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <Modal |
| 90 | + title={title} |
| 91 | + open={isOpen} |
| 92 | + afterOpenChange={afterOpenChange} |
| 93 | + onCancel={onCancel} |
| 94 | + footer={[ |
| 95 | + <Button key="cancel" onClick={onCancel}> Cancel </Button>, |
| 96 | + <Button key="ok" onClick={handleOk}> OK </Button> |
| 97 | + ]} |
| 98 | + > |
| 99 | + <Flex vertical gap="small"> |
| 100 | + <Typography.Paragraph> Name </Typography.Paragraph> |
| 101 | + <Input |
| 102 | + ref={inputRef} |
| 103 | + value={value} |
| 104 | + onPressEnter={handleOk} |
| 105 | + onChange={(e) => setValue(e.target.value.toLowerCase())} |
| 106 | + /> |
| 107 | + {alertErrorVisible && ( |
| 108 | + <Alert |
| 109 | + type="error" |
| 110 | + message={alertErrorMessage} |
| 111 | + closable |
| 112 | + afterClose={() => setAlertErrorVisible(false)} |
| 113 | + /> |
| 114 | + )} |
| 115 | + </Flex> |
| 116 | + </Modal> |
| 117 | + ); |
| 118 | +}; |
0 commit comments