commons/client/src/components/molecules/VersionNumbers/index.tsx

182 lines
4.9 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react'
import {
OceanPlatformVersions,
OceanPlatformTechStatus,
Logger
} from '@oceanprotocol/squid'
import axios from 'axios'
import { version } from '../../../../package.json'
import styles from './index.module.scss'
import { nodeUri, faucetUri } from '../../../config'
import { User, Market } from '../../../context'
import VersionTable from './VersionTable'
2019-06-18 16:08:37 +02:00
import VersionStatus from './VersionStatus'
interface VersionNumbersProps {
minimal?: boolean
account: string
}
export interface VersionNumbersState extends OceanPlatformVersions {
commons: {
name: string
version: string
network: string
}
faucet: {
name: string
version: string
network: string
status: OceanPlatformTechStatus
}
}
export default class VersionNumbers extends PureComponent<
VersionNumbersProps,
VersionNumbersState
> {
public static contextType = User
// construct values which are not part of any response
public commonsVersion =
process.env.NODE_ENV === 'production' ? version : `${version}-dev`
2019-09-02 13:25:46 +02:00
2019-07-18 14:10:45 +02:00
public commonsNetwork = faucetUri.includes('localhost')
? 'Spree'
: new URL(nodeUri).hostname.split('.')[0]
2019-09-02 13:25:46 +02:00
// define a minimal default state to fill UI
public state: VersionNumbersState = {
commons: {
name: 'Commons',
network: this.commonsNetwork,
version: this.commonsVersion
},
squid: {
name: 'Squid-js',
status: OceanPlatformTechStatus.Loading
},
aquarius: {
name: 'Aquarius',
status: OceanPlatformTechStatus.Loading
},
brizo: {
name: 'Brizo',
status: OceanPlatformTechStatus.Loading
2019-06-18 13:28:35 +02:00
},
faucet: {
name: 'Faucet',
version: '',
2019-09-10 16:04:42 +02:00
network: '',
2019-06-18 13:28:35 +02:00
status: OceanPlatformTechStatus.Loading
},
status: {
ok: false,
2019-06-18 16:08:37 +02:00
network: false,
contracts: false
}
}
// for canceling axios requests
public signal = axios.CancelToken.source()
public componentDidMount() {
2019-07-16 12:16:16 +02:00
this.getOceanVersions()
this.getFaucetVersion()
}
public async componentDidUpdate(prevProps: any) {
// Workaround: Using account prop instead of getting it from
// context to be able to compare. Cause there is no `prevContext`.
if (prevProps.account !== this.props.account) {
this.getOceanVersions()
this.getFaucetVersion()
}
}
public componentWillUnmount() {
this.signal.cancel()
}
private async getOceanVersions() {
const { ocean } = this.context
2019-06-18 13:28:35 +02:00
// wait until ocean object is properly populated
if (ocean.versions === undefined) return
const response = await ocean.versions.get()
const { squid, brizo, aquarius, status } = response
this.setState({
2019-06-18 13:37:38 +02:00
...this.state,
squid,
brizo,
2019-06-18 13:28:35 +02:00
aquarius,
status
})
}
2019-06-18 13:37:38 +02:00
private async getFaucetVersion() {
try {
2019-06-18 13:37:38 +02:00
const response = await axios.get(faucetUri, {
headers: { Accept: 'application/json' },
cancelToken: this.signal.token
})
2019-06-11 22:15:14 +02:00
// fail silently
if (response.status !== 200) return
2019-09-10 16:04:42 +02:00
const { version, network } = response.data
2019-06-18 13:37:38 +02:00
this.setState({
...this.state,
faucet: {
...this.state.faucet,
2019-09-10 16:04:42 +02:00
version,
network,
2019-06-18 13:37:38 +02:00
status: OceanPlatformTechStatus.Working
}
})
} catch (error) {
!axios.isCancel(error) && Logger.error(error.message)
}
}
2019-06-18 14:35:18 +02:00
private MinimalOutput = () => {
const { commons, squid, brizo, aquarius } = this.state
2019-06-18 14:35:18 +02:00
return (
<Market.Consumer>
2020-05-19 10:36:18 +02:00
{(market) => (
<p className={styles.versionsMinimal}>
<a
title={`${squid.name} v${squid.version}\n${brizo.name} v${brizo.version}\n${aquarius.name} v${aquarius.version}`}
2019-09-02 13:25:46 +02:00
href="/about"
>
v{commons.version}{' '}
{market.network && `(${market.network})`}
</a>
</p>
)}
</Market.Consumer>
2019-06-18 14:35:18 +02:00
)
}
public render() {
const { minimal } = this.props
return minimal ? (
<this.MinimalOutput />
) : (
<>
<h2 className={styles.versionsTitle} id="#oceanversions">
2019-06-18 16:08:37 +02:00
Ocean Components Status
</h2>
2019-06-18 16:08:37 +02:00
<VersionStatus status={this.state.status} />
<VersionTable data={this.state} />
</>
)
}
}