Refactor SourceIndicator to use context API
parent
239e4b9656
commit
fc361aa57d
|
@ -1,50 +1,46 @@
|
||||||
import React, {SFC} from 'react'
|
import React, {SFC} from 'react'
|
||||||
import PropTypes from 'prop-types'
|
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
import uuid from 'uuid'
|
import uuid from 'uuid'
|
||||||
|
|
||||||
import ReactTooltip from 'react-tooltip'
|
import ReactTooltip from 'react-tooltip'
|
||||||
|
import {SourceContext} from 'src/CheckSources'
|
||||||
import {Source} from 'src/types'
|
import {Source} from 'src/types'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
sourceOverride?: Source
|
sourceOverride?: Source
|
||||||
}
|
}
|
||||||
|
|
||||||
const SourceIndicator: SFC<Props> = ({sourceOverride}, {source}) => {
|
const getTooltipText = (source: Source, sourceOverride: Source): string => {
|
||||||
if (!source) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const {name, url} = source
|
const {name, url} = source
|
||||||
const sourceName: string = _.get(sourceOverride, 'name', name)
|
const sourceName: string = _.get(sourceOverride, 'name', name)
|
||||||
const sourceUrl: string = _.get(sourceOverride, 'url', url)
|
const sourceUrl: string = _.get(sourceOverride, 'url', url)
|
||||||
|
|
||||||
const sourceNameTooltip: string = `<h1>Connected to Source:</h1><p><code>${sourceName} @ ${sourceUrl}</code></p>`
|
return `<h1>Connected to Source:</h1><p><code>${sourceName} @ ${sourceUrl}</code></p>`
|
||||||
|
}
|
||||||
|
|
||||||
|
const SourceIndicator: SFC<Props> = ({sourceOverride}) => {
|
||||||
const uuidTooltip: string = uuid.v4()
|
const uuidTooltip: string = uuid.v4()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<SourceContext.Consumer>
|
||||||
className="source-indicator"
|
{(source: Source) => (
|
||||||
data-for={uuidTooltip}
|
<div
|
||||||
data-tip={sourceNameTooltip}
|
className="source-indicator"
|
||||||
>
|
data-for={uuidTooltip}
|
||||||
<span className="icon disks" />
|
data-tip={getTooltipText(source, sourceOverride)}
|
||||||
<ReactTooltip
|
>
|
||||||
id={uuidTooltip}
|
<span className="icon disks" />
|
||||||
effect="solid"
|
<ReactTooltip
|
||||||
html={true}
|
id={uuidTooltip}
|
||||||
place="left"
|
effect="solid"
|
||||||
class="influx-tooltip"
|
html={true}
|
||||||
/>
|
place="left"
|
||||||
</div>
|
class="influx-tooltip"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</SourceContext.Consumer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {shape} = PropTypes
|
|
||||||
|
|
||||||
SourceIndicator.contextTypes = {
|
|
||||||
source: shape({}),
|
|
||||||
}
|
|
||||||
|
|
||||||
export default SourceIndicator
|
export default SourceIndicator
|
||||||
|
|
Loading…
Reference in New Issue