import { forwardRef, Fragment, ReactElement, Ref, SyntheticEvent, useContext, useEffect, useState, } from 'react'; import Button from '@mui/material/Button'; import Dialog from '@mui/material/Dialog'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; import IconButton from '@mui/material/IconButton'; import Typography from '@mui/material/Typography'; import CloseIcon from '@mui/icons-material/Close'; import ExpandLess from '@mui/icons-material/ExpandLess'; import ExpandMore from '@mui/icons-material/ExpandMore'; import Slide from '@mui/material/Slide'; import { TransitionProps } from '@mui/material/transitions'; import { Box, Collapse, Input, MenuItem, Select, SelectChangeEvent, Tab, Tabs, styled, useTheme, } from '@mui/material'; import { AddGroupList } from './AddGroupList'; import { UserListOfInvites } from './UserListOfInvites'; import { CustomizedSnackbars } from '../Snackbar/Snackbar'; import { getFee } from '../../background/background.ts'; import { QORTAL_APP_CONTEXT } from '../../App'; import { subscribeToEvent, unsubscribeFromEvent } from '../../utils/events'; import { useTranslation } from 'react-i18next'; import { useSetAtom } from 'jotai'; import { txListAtom } from '../../atoms/global'; export const Label = styled('label')` display: block; font-family: 'IBM Plex Sans', sans-serif; font-size: 14px; font-weight: 400; margin-bottom: 4px; `; const Transition = forwardRef(function Transition( props: TransitionProps & { children: ReactElement; }, ref: Ref ) { return ; }); export const AddGroup = ({ address, open, setOpen }) => { const { show } = useContext(QORTAL_APP_CONTEXT); const setTxList = useSetAtom(txListAtom); const [openAdvance, setOpenAdvance] = useState(false); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [groupType, setGroupType] = useState('1'); const [approvalThreshold, setApprovalThreshold] = useState('40'); const [minBlock, setMinBlock] = useState('5'); const [maxBlock, setMaxBlock] = useState('21600'); const [value, setValue] = useState(0); const [openSnack, setOpenSnack] = useState(false); const [infoSnack, setInfoSnack] = useState(null); const handleChange = (event: SyntheticEvent, newValue: number) => { setValue(newValue); }; const handleClose = () => { setOpen(false); }; const handleChangeGroupType = (event: SelectChangeEvent) => { setGroupType(event.target.value as string); }; const handleChangeApprovalThreshold = (event: SelectChangeEvent) => { setApprovalThreshold(event.target.value as string); }; const handleChangeMinBlock = (event: SelectChangeEvent) => { setMinBlock(event.target.value as string); }; const handleChangeMaxBlock = (event: SelectChangeEvent) => { setMaxBlock(event.target.value as string); }; const { t } = useTranslation([ 'auth', 'core', 'group', 'question', 'tutorial', ]); const theme = useTheme(); const handleCreateGroup = async () => { try { if (!name) throw new Error( t('group:message.error.name_required', { postProcess: 'capitalizeFirstChar', }) ); if (!description) throw new Error( t('group:message.error.description_required', { postProcess: 'capitalizeFirstChar', }) ); const fee = await getFee('CREATE_GROUP'); await show({ message: t('core:message.question.perform_transaction', { action: 'CREATE_GROUP', postProcess: 'capitalizeFirstChar', }), publishFee: fee.fee + ' QORT', }); await new Promise((res, rej) => { window .sendMessage('createGroup', { groupName: name, groupDescription: description, groupType: +groupType, groupApprovalThreshold: +approvalThreshold, minBlock: +minBlock, maxBlock: +maxBlock, }) .then((response) => { if (!response?.error) { setInfoSnack({ type: 'success', message: t('group:message.success.group_creation', { postProcess: 'capitalizeFirstChar', }), }); setOpenSnack(true); setTxList((prev) => [ { ...response, type: 'created-group', label: t('group:message.success.group_creation_name', { group_name: name, postProcess: 'capitalizeFirstChar', }), labelDone: t('group:message.success.group_creation_label', { group_name: name, postProcess: 'capitalizeFirstChar', }), done: false, }, ...prev, ]); setName(''); setDescription(''); setGroupType('1'); res(response); return; } rej({ message: response.error }); }) .catch((error) => { rej({ message: error.message || t('core:message.error.generic', { postProcess: 'capitalizeFirstChar', }), }); }); }); } catch (error) { setInfoSnack({ type: 'error', message: error?.message, }); setOpenSnack(true); } }; function a11yProps(index: number) { return { id: `simple-tab-${index}`, 'aria-controls': `simple-tabpanel-${index}`, }; } const openGroupInvitesRequestFunc = () => { setValue(2); }; useEffect(() => { subscribeToEvent('openGroupInvitesRequest', openGroupInvitesRequestFunc); return () => { unsubscribeFromEvent( 'openGroupInvitesRequest', openGroupInvitesRequestFunc ); }; }, []); if (!open) return null; return ( {t('group:group.management', { postProcess: 'capitalizeFirstChar', })} {value === 0 && ( setName(e.target.value)} /> setDescription(e.target.value)} /> setOpenAdvance((prev) => !prev)} > {t('group:advanced_options', { postProcess: 'capitalizeFirstChar', })} {openAdvance ? : } )} {value === 1 && ( )} {value === 2 && ( )} ); };