|
| 1 | +import React from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import { Modal, H3, Body, css, spacing } from '@mongodb-js/compass-components'; |
| 4 | +import { ServerType, TopologyType } from 'mongodb-instance-model'; |
| 5 | +import type { MongoDBInstance } from 'mongodb-instance-model'; |
| 6 | +import type { ConnectionOptions } from '../modules/connection-options'; |
| 7 | +import { ENTERPRISE, COMMUNITY } from '../constants/server-version'; |
| 8 | + |
| 9 | +type ConnectionInfo = { |
| 10 | + term: string; |
| 11 | + description: React.ReactChild; |
| 12 | +}; |
| 13 | + |
| 14 | +const infoContainer = css({ |
| 15 | + margin: `${spacing[3]}px 0`, |
| 16 | +}); |
| 17 | + |
| 18 | +function InfoTerm({ children }: { children: React.ReactChild }) { |
| 19 | + return <Body weight="medium">{children}</Body>; |
| 20 | +} |
| 21 | +function InfoDescription({ children }: { children: React.ReactChild }) { |
| 22 | + return <Body>{children}</Body>; |
| 23 | +} |
| 24 | + |
| 25 | +function Info({ |
| 26 | + term, |
| 27 | + children, |
| 28 | +}: { |
| 29 | + term: React.ReactChild; |
| 30 | + children: React.ReactChild; |
| 31 | +}) { |
| 32 | + return ( |
| 33 | + <div className={infoContainer}> |
| 34 | + <dt> |
| 35 | + <InfoTerm>{term}</InfoTerm> |
| 36 | + </dt> |
| 37 | + <dd> |
| 38 | + <InfoDescription>{children}</InfoDescription> |
| 39 | + </dd> |
| 40 | + </div> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +export function ConnectionInfoModal({ |
| 45 | + isVisible, |
| 46 | + close, |
| 47 | + infos, |
| 48 | +}: { |
| 49 | + isVisible: boolean; |
| 50 | + close: () => void; |
| 51 | + infos: ConnectionInfo[]; |
| 52 | +}) { |
| 53 | + return ( |
| 54 | + <Modal open={isVisible} setOpen={close} size="small"> |
| 55 | + <H3>Connection info</H3> |
| 56 | + |
| 57 | + <dl> |
| 58 | + {infos.map((info, i) => ( |
| 59 | + <Info key={i} term={info.term}> |
| 60 | + {info.description} |
| 61 | + </Info> |
| 62 | + ))} |
| 63 | + </dl> |
| 64 | + </Modal> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function getVersionDistro(isEnterprise?: boolean): string { |
| 69 | + // it is unknown until instance details are loaded |
| 70 | + if (typeof isEnterprise === 'undefined') { |
| 71 | + return ''; |
| 72 | + } |
| 73 | + |
| 74 | + return isEnterprise ? ENTERPRISE : COMMUNITY; |
| 75 | +} |
| 76 | + |
| 77 | +type InfoParameters = { |
| 78 | + instance: MongoDBInstance; |
| 79 | + connectionInfo: ConnectionInfo; |
| 80 | + connectionOptions: ConnectionOptions; |
| 81 | +}; |
| 82 | + |
| 83 | +function getHostInfo({ instance }: InfoParameters): ConnectionInfo { |
| 84 | + const { type, servers } = instance.topologyDescription; |
| 85 | + |
| 86 | + let heading = servers.length === 1 ? 'Host' : 'Hosts'; |
| 87 | + if (type === TopologyType.LOAD_BALANCED) { |
| 88 | + heading += ' (Load Balancer)'; |
| 89 | + } |
| 90 | + |
| 91 | + const hosts = |
| 92 | + servers.length === 1 ? ( |
| 93 | + servers[0].address |
| 94 | + ) : ( |
| 95 | + <div> |
| 96 | + {servers.map((server, i) => ( |
| 97 | + <div key={i}>{server.address}</div> |
| 98 | + ))} |
| 99 | + </div> |
| 100 | + ); |
| 101 | + |
| 102 | + return { |
| 103 | + term: heading, |
| 104 | + description: hosts, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +function makeNodesInfo( |
| 109 | + numNodes: number, |
| 110 | + single: string, |
| 111 | + plural: string |
| 112 | +): string { |
| 113 | + return numNodes === 1 ? `1 ${single}` : `${numNodes} ${plural}`; |
| 114 | +} |
| 115 | + |
| 116 | +function getClusterInfo({ instance }: InfoParameters): ConnectionInfo { |
| 117 | + const { type, setName, servers } = instance.topologyDescription; |
| 118 | + |
| 119 | + let clusterType: string; |
| 120 | + let nodesInfo; |
| 121 | + switch (type) { |
| 122 | + case TopologyType.SHARDED: |
| 123 | + clusterType = 'Sharded'; |
| 124 | + nodesInfo = makeNodesInfo(servers.length, 'Mongos', 'Mongoses'); |
| 125 | + break; |
| 126 | + |
| 127 | + case TopologyType.REPLICA_SET_NO_PRIMARY: |
| 128 | + case TopologyType.REPLICA_SET_WITH_PRIMARY: |
| 129 | + clusterType = `Replica Set ${setName}`; |
| 130 | + nodesInfo = makeNodesInfo(servers.length, 'Node', 'Nodes'); |
| 131 | + break; |
| 132 | + |
| 133 | + default: |
| 134 | + clusterType = ServerType.humanize(servers[0].type); |
| 135 | + break; |
| 136 | + } |
| 137 | + |
| 138 | + return { |
| 139 | + term: 'Cluster', |
| 140 | + description: nodesInfo ? ( |
| 141 | + <div> |
| 142 | + <div>{clusterType}</div> |
| 143 | + <div>{nodesInfo}</div> |
| 144 | + </div> |
| 145 | + ) : ( |
| 146 | + clusterType |
| 147 | + ), |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +function getVersionInfo({ instance }: InfoParameters): ConnectionInfo { |
| 152 | + return { |
| 153 | + term: 'Edition', |
| 154 | + description: instance.dataLake.isDataLake |
| 155 | + ? `Atlas Data Federation ${instance.dataLake.version ?? ''}` |
| 156 | + : `MongoDB ${instance.build.version} ${getVersionDistro( |
| 157 | + instance.build.isEnterprise |
| 158 | + )}`, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +function getSSHTunnelInfo({ |
| 163 | + connectionOptions, |
| 164 | +}: InfoParameters): ConnectionInfo { |
| 165 | + const { sshTunnelHostPortString } = connectionOptions; |
| 166 | + return { |
| 167 | + term: 'SSH Connection Via', |
| 168 | + description: sshTunnelHostPortString, |
| 169 | + }; |
| 170 | +} |
| 171 | + |
| 172 | +function getInfos(infoParameters: InfoParameters) { |
| 173 | + const infos: ConnectionInfo[] = []; |
| 174 | + |
| 175 | + const { instance, connectionOptions } = infoParameters; |
| 176 | + |
| 177 | + if (!instance) { |
| 178 | + return infos; |
| 179 | + } |
| 180 | + |
| 181 | + infos.push(getHostInfo(infoParameters)); |
| 182 | + |
| 183 | + if ( |
| 184 | + instance.dataLake.isDataLake === false && |
| 185 | + instance.topologyDescription.type !== TopologyType.LOAD_BALANCED |
| 186 | + ) { |
| 187 | + infos.push(getClusterInfo(infoParameters)); |
| 188 | + } |
| 189 | + |
| 190 | + infos.push(getVersionInfo(infoParameters)); |
| 191 | + |
| 192 | + if (connectionOptions.sshTunnel) { |
| 193 | + infos.push(getSSHTunnelInfo(infoParameters)); |
| 194 | + } |
| 195 | + |
| 196 | + return infos; |
| 197 | +} |
| 198 | + |
| 199 | +const mapStateToProps = (state: { |
| 200 | + instance: MongoDBInstance; |
| 201 | + connectionInfo: { |
| 202 | + connectionInfo: ConnectionInfo; |
| 203 | + }; |
| 204 | + connectionOptions: ConnectionOptions; |
| 205 | +}) => { |
| 206 | + const { instance, connectionOptions } = state; |
| 207 | + const { connectionInfo } = state.connectionInfo; |
| 208 | + |
| 209 | + return { |
| 210 | + infos: getInfos({ instance, connectionInfo, connectionOptions }), |
| 211 | + }; |
| 212 | +}; |
| 213 | + |
| 214 | +const MappedConnectionInfoModal = connect( |
| 215 | + mapStateToProps, |
| 216 | + {} |
| 217 | +)(ConnectionInfoModal); |
| 218 | + |
| 219 | +export default MappedConnectionInfoModal; |
0 commit comments