/** ****************************************************************************** * Copyright (c) 2022 Precies. Software and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 * ****************************************************************************** */ import { ChangeEvent, FunctionComponent, useContext, useEffect, useState, useRef } from 'react'; import { Button, Dialog, DialogTitle, DialogContent, Box, TextField, DialogActions } from '@mui/material'; import { ButtonWithProgress } from '../../components/button-with-progress'; import { isError } from '../../extension-registry-types'; import { MainContext } from '../../context'; const NAMESPACE_NAME_SIZE = 255; export const CreateNamespaceDialog: FunctionComponent = props => { const [open, setOpen] = useState(false); const [posted, setPosted] = useState(false); const [name, setName] = useState(''); const [nameError, setNameError] = useState(); const context = useContext(MainContext); const abortController = useRef(new AbortController()); useEffect(() => { document.addEventListener('keydown', handleEnter); return () => { abortController.current.abort(); document.removeEventListener('keydown', handleEnter); }; }, []); const handleOpenDialog = () => { setOpen(true); setPosted(false); }; const handleCancel = () => { setOpen(false); setName(''); }; const handleNameChange = (event: ChangeEvent) => { const name = event.target.value; let nameError: string | undefined; if (name.length > NAMESPACE_NAME_SIZE) { nameError = `The namespace name must not be longer than ${NAMESPACE_NAME_SIZE} characters.`; } setName(name); setNameError(nameError); }; const handleCreateNamespace = async () => { if (!context.user) { return; } setPosted(true); try { const response = await context.service.createNamespace(abortController.current, name); if (isError(response)) { throw response; } setOpen(false); props.namespaceCreated(); } catch (err) { context.handleError(err); } setPosted(false); }; const handleEnter = (e: KeyboardEvent) => { if (open && e.code === 'Enter') { handleCreateNamespace(); } }; return <> Create new namespace Create Namespace ; }; export interface CreateNamespaceDialogProps { namespaceCreated: () => void; }