import { forwardRef, Fragment, ReactElement, Ref, SyntheticEvent, useCallback, 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 Slide from '@mui/material/Slide'; import { TransitionProps } from '@mui/material/transitions'; import ListOfMembers from './ListOfMembers'; import { InviteMember } from './InviteMember'; import { ListOfInvites } from './ListOfInvites'; import { ListOfBans } from './ListOfBans'; import { ListOfJoinRequests } from './ListOfJoinRequests'; import { Box, ButtonBase, Card, Tab, Tabs, useTheme } from '@mui/material'; import { CustomizedSnackbars } from '../Snackbar/Snackbar'; import { MyContext, getBaseApiReact } from '../../App'; import { getGroupMembers, getNames } from './Group'; import { LoadingSnackbar } from '../Snackbar/LoadingSnackbar'; import { getFee } from '../../background'; import { LoadingButton } from '@mui/lab'; import { subscribeToEvent, unsubscribeFromEvent } from '../../utils/events'; import { Spacer } from '../../common/Spacer'; import InsertLinkIcon from '@mui/icons-material/InsertLink'; import { useSetAtom } from 'jotai'; import { txListAtom } from '../../atoms/global'; import { useTranslation } from 'react-i18next'; function a11yProps(index: number) { return { id: `simple-tab-${index}`, 'aria-controls': `simple-tabpanel-${index}`, }; } const Transition = forwardRef(function Transition( props: TransitionProps & { children: ReactElement; }, ref: Ref ) { return ; }); export const ManageMembers = ({ open, setOpen, selectedGroup, isAdmin, isOwner, }) => { const [membersWithNames, setMembersWithNames] = useState([]); const [value, setValue] = useState(0); const [openSnack, setOpenSnack] = useState(false); const [infoSnack, setInfoSnack] = useState(null); const [isLoadingMembers, setIsLoadingMembers] = useState(false); const [isLoadingLeave, setIsLoadingLeave] = useState(false); const [groupInfo, setGroupInfo] = useState(null); const handleChange = (event: SyntheticEvent, newValue: number) => { setValue(newValue); }; const theme = useTheme(); const { t } = useTranslation(['auth', 'core', 'group']); const { show } = useContext(MyContext); const setTxList = useSetAtom(txListAtom); const handleClose = () => { setOpen(false); }; const handleLeaveGroup = async () => { try { setIsLoadingLeave(true); const fee = await getFee('LEAVE_GROUP'); await show({ message: t('core:message.question.perform_transaction', { action: 'LEAVE_GROUP', postProcess: 'capitalizeFirst', }), publishFee: fee.fee + ' QORT', }); await new Promise((res, rej) => { window .sendMessage('leaveGroup', { groupId: selectedGroup?.groupId, }) .then((response) => { if (!response?.error) { setTxList((prev) => [ { ...response, type: 'leave-group', label: t('group:message.success.group_leave_name', { group_name: selectedGroup?.groupName, postProcess: 'capitalizeFirst', }), labelDone: t('group:message.success.group_leave_label', { group_name: selectedGroup?.groupName, postProcess: 'capitalizeFirst', }), done: false, groupId: selectedGroup?.groupId, }, ...prev, ]); res(response); setInfoSnack({ type: 'success', message: t('group:message.success.group_leave', { postProcess: 'capitalizeFirst', }), }); setOpenSnack(true); return; } rej(response.error); }) .catch((error) => { rej( error.message || t('core:message.error.generic', { postProcess: 'capitalizeFirst', }) ); }); }); } catch (error) { console.log(error); } finally { setIsLoadingLeave(false); } }; const getMembersWithNames = useCallback(async (groupId) => { try { setIsLoadingMembers(true); const res = await getGroupMembers(groupId); const resWithNames = await getNames(res.members); setMembersWithNames(resWithNames); setIsLoadingMembers(false); } catch (error) { console.log(error); } }, []); const getMembers = async (groupId) => { try { const res = await getGroupMembers(groupId); setMembersWithNames(res?.members || []); } catch (error) { console.log(error); } }; const getGroupInfo = async (groupId) => { try { const response = await fetch(`${getBaseApiReact()}/groups/${groupId}`); const groupData = await response.json(); setGroupInfo(groupData); } catch (error) { console.log(error); } }; useEffect(() => { if (selectedGroup?.groupId) { getMembers(selectedGroup?.groupId); getGroupInfo(selectedGroup?.groupId); } }, [selectedGroup?.groupId]); const openGroupJoinRequestFunc = () => { setValue(4); }; useEffect(() => { subscribeToEvent('openGroupJoinRequest', openGroupJoinRequestFunc); return () => { unsubscribeFromEvent('openGroupJoinRequest', openGroupJoinRequestFunc); }; }, []); return ( {t('group:action.manage_members', { postProcess: 'capitalizeFirst', })} {t('group:group.id', { postProcess: 'capitalizeFirst' })}:{' '} {groupInfo?.groupId} {t('group:group.name', { postProcess: 'capitalizeFirst' })}:{' '} {groupInfo?.groupName} {t('group:group.member_number', { postProcess: 'capitalizeFirst', })} : {groupInfo?.memberCount} { const link = `qortal://use-group/action-join/groupid-${groupInfo?.groupId}`; await navigator.clipboard.writeText(link); }} > {t('group:join_link', { postProcess: 'capitalizeFirst' })} {selectedGroup?.groupId && !isOwner && ( {t('group:action.leave_group', { postProcess: 'capitalizeFirst', })} )} {value === 0 && ( )} {value === 1 && ( )} {value === 2 && ( )} {value === 3 && ( )} {value === 4 && ( )} ); };