2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-11-29 22:21:32 +01:00
|
|
|
import type { Node } from 'react';
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2020-01-21 23:38:10 +01:00
|
|
|
import classnames from 'classnames';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
2022-02-23 13:42:43 +01:00
|
|
|
import { stripLeadingAtSign } from 'util/string';
|
2017-05-02 07:25:31 +02:00
|
|
|
|
2022-02-16 04:47:36 +01:00
|
|
|
type ChannelInfo = { uri: string, name: string };
|
2022-02-07 21:59:20 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
uri: string,
|
2022-02-07 21:59:20 +01:00
|
|
|
channelInfo: ?ChannelInfo, // Direct channel info to use, bypassing the need to resolve 'uri'.
|
2022-02-16 04:47:36 +01:00
|
|
|
link: ?boolean,
|
2021-03-24 03:53:33 +01:00
|
|
|
external?: boolean,
|
2022-02-16 04:47:36 +01:00
|
|
|
focusable?: boolean, // Defaults to 'true' if not provided.
|
|
|
|
hideAnonymous?: boolean,
|
|
|
|
inline?: boolean,
|
2022-02-23 13:42:43 +01:00
|
|
|
stripAtSign?: boolean,
|
2021-03-30 18:00:30 +02:00
|
|
|
className?: string,
|
2022-02-16 04:47:36 +01:00
|
|
|
children: ?Node, // to allow for other elements to be nested within the UriIndicator (commit: 1e82586f).
|
|
|
|
// --- redux ---
|
|
|
|
claim: ?Claim,
|
|
|
|
isResolvingUri: boolean,
|
|
|
|
resolveUri: (string) => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class UriIndicator extends React.PureComponent<Props> {
|
2019-06-11 20:10:58 +02:00
|
|
|
componentDidMount() {
|
2022-02-07 21:59:20 +01:00
|
|
|
this.resolveClaim(this.props);
|
2017-05-21 16:42:34 +02:00
|
|
|
}
|
2017-05-02 07:25:31 +02:00
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
componentDidUpdate() {
|
2022-02-07 21:59:20 +01:00
|
|
|
this.resolveClaim(this.props);
|
2017-05-10 05:06:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
resolveClaim = (props: Props) => {
|
|
|
|
const { isResolvingUri, resolveUri, claim, uri, channelInfo } = props;
|
2017-05-10 05:06:48 +02:00
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
if (!channelInfo && !isResolvingUri && claim === undefined && uri) {
|
2017-06-06 23:19:12 +02:00
|
|
|
resolveUri(uri);
|
2017-05-21 16:42:34 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-05-02 07:25:31 +02:00
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-21 16:42:34 +02:00
|
|
|
render() {
|
2021-03-30 18:00:30 +02:00
|
|
|
const {
|
2022-02-07 21:59:20 +01:00
|
|
|
channelInfo,
|
2021-03-30 18:00:30 +02:00
|
|
|
link,
|
|
|
|
isResolvingUri,
|
|
|
|
claim,
|
|
|
|
children,
|
|
|
|
inline,
|
2021-07-16 08:53:38 +02:00
|
|
|
focusable = true,
|
2021-03-30 18:00:30 +02:00
|
|
|
external = false,
|
2021-07-16 08:53:38 +02:00
|
|
|
hideAnonymous = false,
|
2022-02-23 13:42:43 +01:00
|
|
|
stripAtSign,
|
2021-03-30 18:00:30 +02:00
|
|
|
className,
|
|
|
|
} = this.props;
|
2019-04-24 16:02:08 +02:00
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
if (!channelInfo && !claim) {
|
2021-10-14 16:28:43 +02:00
|
|
|
return (
|
|
|
|
<span className={classnames('empty', className)}>
|
|
|
|
{isResolvingUri || claim === undefined ? __('Validating...') : __('[Removed]')}
|
|
|
|
</span>
|
|
|
|
);
|
2017-05-21 16:42:34 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
const data = this.resolveState(channelInfo, claim, link);
|
|
|
|
|
|
|
|
if (data.isAnonymous) {
|
2020-01-29 16:26:39 +01:00
|
|
|
if (hideAnonymous) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-25 19:34:21 +01:00
|
|
|
return (
|
2021-03-30 18:00:30 +02:00
|
|
|
<span dir="auto" className={classnames('channel-name', className, { 'channel-name--inline': inline })}>
|
2021-01-25 19:34:21 +01:00
|
|
|
Anonymous
|
|
|
|
</span>
|
|
|
|
);
|
2017-05-21 16:42:34 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 21:59:20 +01:00
|
|
|
if (data.hasChannelData) {
|
|
|
|
const { channelName, channelLink } = data;
|
2017-09-17 22:33:52 +02:00
|
|
|
|
2021-01-25 19:34:21 +01:00
|
|
|
const inner = (
|
|
|
|
<span dir="auto" className={classnames('channel-name', { 'channel-name--inline': inline })}>
|
2022-02-23 13:42:43 +01:00
|
|
|
{stripAtSign ? stripLeadingAtSign(channelName) : channelName}
|
2021-01-25 19:34:21 +01:00
|
|
|
</span>
|
|
|
|
);
|
2019-06-11 20:10:58 +02:00
|
|
|
|
|
|
|
if (!channelLink) {
|
|
|
|
return inner;
|
|
|
|
}
|
2017-09-17 22:33:52 +02:00
|
|
|
|
2019-12-04 02:30:29 +01:00
|
|
|
if (children) {
|
2021-03-24 03:53:33 +01:00
|
|
|
return (
|
2021-07-16 08:53:38 +02:00
|
|
|
<Button
|
|
|
|
aria-hidden={!focusable}
|
|
|
|
tabIndex={focusable ? 0 : -1}
|
|
|
|
className={className}
|
|
|
|
target={external ? '_blank' : undefined}
|
|
|
|
navigate={channelLink}
|
|
|
|
>
|
2021-03-24 03:53:33 +01:00
|
|
|
{children}
|
|
|
|
</Button>
|
|
|
|
);
|
2019-12-04 02:30:29 +01:00
|
|
|
} else {
|
|
|
|
return (
|
2021-03-30 18:00:30 +02:00
|
|
|
<Button
|
|
|
|
className={classnames(className, 'button--uri-indicator')}
|
|
|
|
navigate={channelLink}
|
|
|
|
target={external ? '_blank' : undefined}
|
2021-07-16 08:53:38 +02:00
|
|
|
aria-hidden={!focusable}
|
|
|
|
tabIndex={focusable ? 0 : -1}
|
2021-03-30 18:00:30 +02:00
|
|
|
>
|
2021-01-25 19:34:21 +01:00
|
|
|
{inner}
|
2019-12-04 02:30:29 +01:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
2019-06-11 20:10:58 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-21 16:42:34 +02:00
|
|
|
}
|
2017-05-02 07:25:31 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
export default UriIndicator;
|