import * as React 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'; function a11yProps(index: number) { return { id: `simple-tab-${index}`, 'aria-controls': `simple-tabpanel-${index}`, }; } const Transition = React.forwardRef(function Transition( props: TransitionProps & { children: React.ReactElement; }, ref: React.Ref ) { return ; }); export const ManageMembers = ({ address, open, setOpen, selectedGroup, isAdmin, isOwner, }) => { const [membersWithNames, setMembersWithNames] = React.useState([]); const [tab, setTab] = React.useState('create'); const [value, setValue] = React.useState(0); const [openSnack, setOpenSnack] = React.useState(false); const [infoSnack, setInfoSnack] = React.useState(null); const [isLoadingMembers, setIsLoadingMembers] = React.useState(false); const [isLoadingLeave, setIsLoadingLeave] = React.useState(false); const [groupInfo, setGroupInfo] = React.useState(null); const handleChange = (event: React.SyntheticEvent, newValue: number) => { setValue(newValue); }; const theme = useTheme(); const { show } = React.useContext(MyContext); const setTxList = useSetAtom(txListAtom); const handleClose = () => { setOpen(false); }; const handleLeaveGroup = async () => { try { setIsLoadingLeave(true); const fee = await getFee('LEAVE_GROUP'); // TODO translate await show({ message: t('group:question.perform_transaction', { action: 'LEAVE_GROUP', postProcess: 'capitalize', }), 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: `Left Group ${selectedGroup?.groupName}: awaiting confirmation`, labelDone: `Left Group ${selectedGroup?.groupName}: success!`, done: false, groupId: selectedGroup?.groupId, }, ...prev, ]); res(response); setInfoSnack({ type: 'success', message: 'Successfully requested to leave group. It may take a couple of minutes for the changes to propagate', }); setOpenSnack(true); return; } rej(response.error); }) .catch((error) => { rej(error.message || 'An error occurred'); }); }); } catch (error) { console.log(error); } finally { setIsLoadingLeave(false); } }; const getMembersWithNames = React.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); } }; React.useEffect(() => { if (selectedGroup?.groupId) { getMembers(selectedGroup?.groupId); getGroupInfo(selectedGroup?.groupId); } }, [selectedGroup?.groupId]); const openGroupJoinRequestFunc = () => { setValue(4); }; React.useEffect(() => { subscribeToEvent('openGroupJoinRequest', openGroupJoinRequestFunc); return () => { unsubscribeFromEvent('openGroupJoinRequest', openGroupJoinRequestFunc); }; }, []); return ( Manage Members GroupId: {groupInfo?.groupId} GroupName: {groupInfo?.groupName} Number of members: {groupInfo?.memberCount} { const link = `qortal://use-group/action-join/groupid-${groupInfo?.groupId}`; await navigator.clipboard.writeText(link); }} > Join Group Link {selectedGroup?.groupId && !isOwner && ( Leave Group )} {value === 0 && ( )} {value === 1 && ( )} {value === 2 && ( )} {value === 3 && ( )} {value === 4 && ( )} ); };