import { Grid } from '@mui/material'; import { FC, useState } from 'react'; import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import NETWORK_TYPES from '../../lib/consts/NETWORK_TYPES'; import FlexBox from '../FlexBox'; import List from '../List'; import { ExpandablePanel } from '../Panels'; import periodicFetch from '../../lib/fetchers/periodicFetch'; import State from '../State'; import { BodyText, MonoText, SmallText } from '../Text'; import useProtect from '../../hooks/useProtect'; import useProtectedState from '../../hooks/useProtectedState'; type InboundConnections = { [ipAddress: string]: { dbPort: number; dbUser: string; ipAddress: string; networkLinkNumber: number; networkNumber: number; networkType: string; }; }; type PeerConnections = { [peer: string]: { dbPort: number; dbUser: string; ipAddress: string; isDelete?: boolean; isEdit?: boolean; isNew?: boolean; isPingTest?: boolean; }; }; const ConfigPeersForm: FC = () => { const { protect } = useProtect(); const [inboundConnections, setInboundConnections] = useProtectedState({}, protect); const [peerConnections, setPeerConnections] = useProtectedState({}, protect); const [isEditPeerConnections, setIsEditPeerConnections] = useState(false); const { isLoading } = periodicFetch<{ local: { inbound: { ipAddress: { [ipAddress: string]: { hostUUID: string; ipAddress: string; ipAddressUUID: string; networkLinkNumber: number; networkNumber: number; networkType: string; }; }; port: number; user: string; }; peer: { [ipAddress: string]: { hostUUID: string; ipAddress: string; isPing: boolean; port: number; user: string; }; }; }; }>(`${API_BASE_URL}/host/connection`, { onSuccess: ({ local: { inbound: { ipAddress: ipAddressList, port: dbPort, user: dbUser }, peer, }, }) => { setInboundConnections( Object.entries(ipAddressList).reduce( ( previous, [ipAddress, { networkLinkNumber, networkNumber, networkType }], ) => { previous[ipAddress] = { dbPort, dbUser, ipAddress, networkLinkNumber, networkNumber, networkType, }; return previous; }, {}, ), ); setPeerConnections( Object.entries(peer).reduce( ( previous, [ peerIPAddress, { isPing: isPingTest, port: peerDBPort, user: peerDBUser }, ], ) => { previous[`${peerDBUser}@${peerIPAddress}:${peerDBPort}`] = { dbPort: peerDBPort, dbUser: peerDBUser, ipAddress: peerIPAddress, isPingTest, }; return previous; }, {}, ), ); }, }); return ( Configure striker peers} loading={isLoading} > ( {`${dbUser}@${ipAddress}:${dbPort}`} {`${NETWORK_TYPES[networkType]} ${networkNumber}`} )} /> { setIsEditPeerConnections((previous) => !previous); }} renderListItem={(peer, { isPingTest = false }) => ( {peer} )} /> ); }; export default ConfigPeersForm;