import React, { useRef, useState } from 'react'; import { message, Button, Input, Modal, Typography, Form, Collapse } from 'antd'; import styled from 'styled-components'; import { useCreateGroupMutation } from '../../../graphql/group.generated'; import { useEnterKeyListener } from '../../shared/useEnterKeyListener'; import { validateCustomUrnId } from '../../shared/textUtil'; import analytics, { EventType } from '../../analytics'; import { CorpGroup, EntityType } from '../../../types.generated'; import { Editor as MarkdownEditor } from '../../entity/shared/tabs/Documentation/components/editor/Editor'; import { ANTD_GRAY } from '../../entity/shared/constants'; type Props = { onClose: () => void; onCreate: (group: CorpGroup) => void; }; const StyledEditor = styled(MarkdownEditor)` border: 1px solid ${ANTD_GRAY[4]}; `; export default function CreateGroupModal({ onClose, onCreate }: Props) { const [stagedName, setStagedName] = useState(''); const [stagedDescription, setStagedDescription] = useState(''); const [stagedId, setStagedId] = useState(undefined); const [createGroupMutation] = useCreateGroupMutation(); const [createButtonEnabled, setCreateButtonEnabled] = useState(true); const [form] = Form.useForm(); // Reference to the styled editor for handling focus const styledEditorRef = useRef(null); const onCreateGroup = () => { // Check if the Enter key was pressed inside the styled editor to prevent unintended form submission const isEditorNewlineKeypress = document.activeElement !== styledEditorRef.current && !styledEditorRef.current?.contains(document.activeElement); if (isEditorNewlineKeypress) { createGroupMutation({ variables: { input: { id: stagedId, name: stagedName, description: stagedDescription, }, }, }) .then(({ data, errors }) => { if (!errors) { analytics.event({ type: EventType.CreateGroupEvent, }); message.success({ content: `Created group!`, duration: 3, }); // TODO: Get a full corp group back from create endpoint. onCreate({ urn: data?.createGroup || '', type: EntityType.CorpGroup, name: stagedName, info: { description: stagedDescription, }, }); } }) .catch((e) => { message.destroy(); message.error({ content: `Failed to create group!: \n ${e.message || ''}`, duration: 3 }); }) .finally(() => { setStagedName(''); setStagedDescription(''); }); onClose(); } }; // Handle the Enter press useEnterKeyListener({ querySelectorToExecuteClick: '#createGroupButton', }); function updateDescription(description: string) { setStagedDescription(description); } return ( } >
setCreateButtonEnabled(form.getFieldsError().some((field) => field.errors.length > 0)) } > Name}> Give your new group a name. setStagedName(event.target.value)} /> Description}> An optional description for your new group. {/* Styled editor for the group description */}
Advanced} key="1"> Group Id}> By default, a random UUID will be generated to uniquely identify this group. If you'd like to provide a custom id instead to more easily keep track of this group, you may provide it here. Be careful, you cannot easily change the group id after creation. ({ validator(_, value) { if (value && validateCustomUrnId(value)) { return Promise.resolve(); } return Promise.reject(new Error('Please enter correct Group name')); }, }), ]} > setStagedId(event.target.value)} />
); }