// @flow import type { Node } from 'react'; import React from 'react'; import classnames from 'classnames'; import Button from 'component/button'; type ChannelInfo = { uri: string, name: string }; type Props = { uri: string, channelInfo: ?ChannelInfo, // Direct channel info to use, bypassing the need to resolve 'uri'. link: ?boolean, external?: boolean, focusable?: boolean, // Defaults to 'true' if not provided. hideAnonymous?: boolean, inline?: boolean, className?: string, children: ?Node, // to allow for other elements to be nested within the UriIndicator (commit: 1e82586f). // --- redux --- claim: ?Claim, isResolvingUri: boolean, resolveUri: (string) => void, }; class UriIndicator extends React.PureComponent { componentDidMount() { this.resolveClaim(this.props); } componentDidUpdate() { this.resolveClaim(this.props); } resolveClaim = (props: Props) => { const { isResolvingUri, resolveUri, claim, uri, channelInfo } = props; if (!channelInfo && !isResolvingUri && claim === undefined && uri) { resolveUri(uri); } }; resolveState = (channelInfo: ?ChannelInfo, claim: ?Claim, isLinkType: ?boolean) => { if (channelInfo) { return { hasChannelData: true, isAnonymous: false, channelName: channelInfo.name, channelLink: isLinkType ? channelInfo.uri : false, }; } else if (claim) { const signingChannel = claim.signing_channel && claim.signing_channel.amount; const isChannelClaim = claim.value_type === 'channel'; const channelClaim = isChannelClaim ? claim : claim.signing_channel; return { hasChannelData: Boolean(channelClaim), isAnonymous: !signingChannel && !isChannelClaim, channelName: channelClaim?.name, channelLink: isLinkType ? channelClaim?.canonical_url || channelClaim?.permanent_url : false, }; } else { return { hasChannelData: false, isAnonymous: undefined, channelName: undefined, channelLink: undefined, }; } }; render() { const { channelInfo, link, isResolvingUri, claim, children, inline, focusable = true, external = false, hideAnonymous = false, className, } = this.props; if (!channelInfo && !claim) { return ( {isResolvingUri || claim === undefined ? __('Validating...') : __('[Removed]')} ); } const data = this.resolveState(channelInfo, claim, link); if (data.isAnonymous) { if (hideAnonymous) { return null; } return ( Anonymous ); } if (data.hasChannelData) { const { channelName, channelLink } = data; const inner = ( {channelName} ); if (!channelLink) { return inner; } if (children) { return ( ); } else { return ( ); } } else { return null; } } } export default UriIndicator;