2018-03-26 14:32:43 -07:00
|
|
|
// @flow
|
2020-02-18 17:48:10 -05:00
|
|
|
import { CHANNEL_NEW, CHANNEL_ANONYMOUS } from 'constants/claim';
|
2019-07-21 17:31:22 -04:00
|
|
|
import React, { Fragment } from 'react';
|
2019-02-13 12:27:20 -04:00
|
|
|
import { FormField } from 'component/common/form';
|
2019-12-03 20:02:34 -05:00
|
|
|
import ChannelCreate from 'component/channelCreate';
|
2019-10-11 21:55:54 -04:00
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
type Props = {
|
|
|
|
channel: string, // currently selected channel
|
2019-10-23 15:39:51 -04:00
|
|
|
channels: ?Array<ChannelClaim>,
|
2018-03-26 14:32:43 -07:00
|
|
|
balance: number,
|
2019-06-12 10:53:27 -04:00
|
|
|
onChannelChange: string => void,
|
2018-03-26 14:32:43 -07:00
|
|
|
createChannel: (string, number) => Promise<any>,
|
|
|
|
fetchChannelListMine: () => void,
|
|
|
|
fetchingChannels: boolean,
|
2020-02-18 17:48:10 -05:00
|
|
|
hideAnon: boolean,
|
2020-03-18 13:11:37 -04:00
|
|
|
hideNew: boolean,
|
2020-01-14 15:44:07 -05:00
|
|
|
label?: string,
|
|
|
|
injected?: Array<string>,
|
2020-01-14 09:57:34 -05:00
|
|
|
emailVerified: boolean,
|
2018-03-26 14:32:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
addingChannel: boolean,
|
|
|
|
};
|
|
|
|
|
2020-08-04 15:56:15 +08:00
|
|
|
const ID_FF_SELECT_CHANNEL = 'ID_FF_SELECT_CHANNEL';
|
|
|
|
|
2020-04-15 17:58:59 -04:00
|
|
|
class ChannelSelection extends React.PureComponent<Props, State> {
|
2018-03-26 14:32:43 -07:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-07-16 15:13:49 +08:00
|
|
|
addingChannel: props.channel === CHANNEL_NEW,
|
2018-03-26 14:32:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
(this: any).handleChannelChange = this.handleChannelChange.bind(this);
|
2019-12-03 20:02:34 -05:00
|
|
|
(this: any).handleChangeToNewChannel = this.handleChangeToNewChannel.bind(this);
|
2018-03-26 14:32:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-08-04 15:56:15 +08:00
|
|
|
const { channel, channels, fetchChannelListMine, fetchingChannels, emailVerified, onChannelChange } = this.props;
|
2019-10-28 14:53:59 -04:00
|
|
|
if (IS_WEB && !emailVerified) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:57:55 -04:00
|
|
|
if ((!channels || !channels.length) && !fetchingChannels) {
|
2018-03-26 14:32:43 -07:00
|
|
|
fetchChannelListMine();
|
|
|
|
}
|
2020-08-04 15:56:15 +08:00
|
|
|
|
2020-08-06 10:17:21 +08:00
|
|
|
if (channels && channels.length && !channels.find(chan => chan.name === channel)) {
|
|
|
|
const elem = document.getElementById(ID_FF_SELECT_CHANNEL);
|
|
|
|
// $FlowFixMe
|
|
|
|
if (elem && elem.value && elem.value !== channel) {
|
|
|
|
setTimeout(() => {
|
|
|
|
// $FlowFixMe
|
|
|
|
onChannelChange(elem.value);
|
|
|
|
}, 250);
|
|
|
|
}
|
2020-08-04 15:56:15 +08:00
|
|
|
}
|
2018-03-26 14:32:43 -07:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:58:59 -04:00
|
|
|
componentDidUpdate() {
|
2020-06-09 18:09:24 -04:00
|
|
|
const { channels, fetchingChannels, hideAnon } = this.props;
|
|
|
|
if (!fetchingChannels && !channels && hideAnon) {
|
2020-04-15 17:58:59 -04:00
|
|
|
this.setState({ addingChannel: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 20:47:23 -04:00
|
|
|
handleChannelChange(event: SyntheticInputEvent<*>) {
|
2019-06-12 10:53:27 -04:00
|
|
|
const { onChannelChange } = this.props;
|
|
|
|
const channel = event.target.value;
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2019-06-12 10:53:27 -04:00
|
|
|
if (channel === CHANNEL_NEW) {
|
2018-03-26 14:32:43 -07:00
|
|
|
this.setState({ addingChannel: true });
|
2019-06-12 10:53:27 -04:00
|
|
|
onChannelChange(channel);
|
2018-03-26 14:32:43 -07:00
|
|
|
} else {
|
|
|
|
this.setState({ addingChannel: false });
|
2019-06-12 10:53:27 -04:00
|
|
|
onChannelChange(channel);
|
2018-03-26 14:32:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:02:34 -05:00
|
|
|
handleChangeToNewChannel(props: Object) {
|
|
|
|
const { onChannelChange } = this.props;
|
|
|
|
const { newChannelName } = props;
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2019-12-03 20:02:34 -05:00
|
|
|
this.setState({ addingChannel: false });
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2019-07-24 14:21:34 -04:00
|
|
|
const channelName = `@${newChannelName.trim()}`;
|
2019-12-03 20:02:34 -05:00
|
|
|
onChannelChange(channelName);
|
2018-03-26 14:32:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-07-16 15:13:49 +08:00
|
|
|
const channel = this.state.addingChannel ? CHANNEL_NEW : this.props.channel;
|
2020-03-18 13:11:37 -04:00
|
|
|
const { fetchingChannels, channels = [], hideAnon, hideNew, label, injected = [] } = this.props;
|
2019-12-03 20:02:34 -05:00
|
|
|
const { addingChannel } = this.state;
|
2018-03-26 14:32:43 -07:00
|
|
|
|
|
|
|
return (
|
2019-07-21 17:31:22 -04:00
|
|
|
<Fragment>
|
2020-02-06 13:49:05 -05:00
|
|
|
<FormField
|
2020-08-04 15:56:15 +08:00
|
|
|
id={ID_FF_SELECT_CHANNEL}
|
2020-02-06 13:49:05 -05:00
|
|
|
name="channel"
|
|
|
|
label={label || __('Channel')}
|
|
|
|
type="select"
|
|
|
|
onChange={this.handleChannelChange}
|
|
|
|
value={channel}
|
|
|
|
>
|
2020-02-18 17:48:10 -05:00
|
|
|
{!hideAnon && <option value={CHANNEL_ANONYMOUS}>{__('Anonymous')}</option>}
|
2020-02-06 13:49:05 -05:00
|
|
|
{channels &&
|
|
|
|
channels.map(({ name, claim_id: claimId }) => (
|
|
|
|
<option key={claimId} value={name}>
|
|
|
|
{name}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
{injected &&
|
|
|
|
injected.map(item => (
|
|
|
|
<option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</option>
|
|
|
|
))}
|
2020-03-18 13:11:37 -04:00
|
|
|
{!fetchingChannels && !hideNew && <option value={CHANNEL_NEW}>{__('New channel...')}</option>}
|
2020-02-06 13:49:05 -05:00
|
|
|
</FormField>
|
|
|
|
|
|
|
|
{addingChannel && <ChannelCreate onSuccess={this.handleChangeToNewChannel} />}
|
2019-07-21 17:31:22 -04:00
|
|
|
</Fragment>
|
2018-03-26 14:32:43 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:58:59 -04:00
|
|
|
export default ChannelSelection;
|