lbry-desktop/ui/component/inviteNew/view.jsx

139 lines
4.5 KiB
React
Raw Normal View History

// @flow
2020-01-14 15:44:07 -05:00
import React, { useEffect, useState } from 'react';
2018-04-11 03:58:34 +07:00
import Button from 'component/button';
2019-06-17 16:32:38 -04:00
import { Form, FormField } from 'component/common/form';
2019-02-20 11:18:59 -05:00
import CopyableText from 'component/copyableText';
2019-09-26 12:07:11 -04:00
import Card from 'component/common/card';
2020-01-14 15:44:07 -05:00
import { URL } from 'config';
import SelectChannel from 'component/selectChannel';
import analytics from 'analytics';
2020-01-03 10:06:26 -05:00
import I18nMessage from 'component/i18nMessage';
2017-08-17 23:31:44 -04:00
2019-11-22 16:13:00 -05:00
type Props = {
errorMessage: ?string,
inviteNew: string => void,
isPending: boolean,
referralLink: string,
2020-01-14 15:44:07 -05:00
referralCode: string,
channels: ?Array<ChannelClaim>,
2019-11-22 16:13:00 -05:00
};
2020-01-14 15:44:07 -05:00
function InviteNew(props: Props) {
const { inviteNew, errorMessage, isPending, referralCode = '', channels } = props;
2020-01-14 15:44:07 -05:00
// Email
const [email, setEmail] = useState('');
function handleSubmit() {
inviteNew(email);
}
2020-01-14 15:44:07 -05:00
function handleEmailChanged(event: any) {
setEmail(event.target.value);
}
2020-01-14 15:44:07 -05:00
// Referral link
const [referralSource, setReferralSource] = useState(referralCode);
function handleReferralChange(code) {
setReferralSource(code);
// TODO: keep track of this in an array?
const matchingChannel = channels && channels.find(ch => ch.name === code);
if (matchingChannel) {
analytics.apiLogPublish(matchingChannel);
}
2017-08-17 23:31:44 -04:00
}
2020-01-14 15:44:07 -05:00
const topChannel =
channels &&
channels.reduce((top, channel) => {
const topClaimCount = (top && top.meta && top.meta.claims_in_channel) || 0;
const currentClaimCount = (channel && channel.meta && channel.meta.claims_in_channel) || 0;
return topClaimCount >= currentClaimCount ? top : channel;
});
const referralString =
channels && channels.length && referralSource !== referralCode
? lookupUrlByClaimName(referralSource, channels)
: referralSource;
const referral = `${URL}/$/invite/${referralString.replace('#', ':')}`;
useEffect(() => {
// set default channel
if (topChannel) {
2020-01-14 15:44:07 -05:00
handleReferralChange(topChannel.name);
}
}, [topChannel]);
2020-01-14 15:44:07 -05:00
function lookupUrlByClaimName(name, channels) {
const claim = channels.find(channel => channel.name === name);
return claim && claim.canonical_url ? claim.canonical_url.replace('lbry://', '') : name;
2020-01-14 15:01:37 -05:00
}
2020-01-14 12:42:30 -05:00
2020-01-14 15:44:07 -05:00
return (
<div className={'columns'}>
<Card
title={__('Invite Link')}
subtitle={__('Share this link with friends (or enemies) and earn LBC when they join lbry.tv')}
2020-01-14 15:44:07 -05:00
actions={
<React.Fragment>
<CopyableText label={__('Your invite link')} copyable={referral} />
<SelectChannel
channel={referralSource}
onChannelChange={channel => handleReferralChange(channel)}
label={'Customize link'}
hideAnon
injected={[referralCode]}
/>
<p className="help">
<I18nMessage
tokens={{
rewards_link: <Button button="link" navigate="/$/rewards" label={__('rewards')} />,
referral_faq_link: <Button button="link" label={__('FAQ')} href="https://lbry.com/faq/referrals" />,
}}
>
Earn %rewards_link% for inviting your friends. Read our %referral_faq_link% to learn more.
</I18nMessage>
</p>
</React.Fragment>
}
/>
2020-01-14 11:44:40 -05:00
2020-01-14 09:57:34 -05:00
<Card
2020-01-14 15:44:07 -05:00
title={__('Invite by Email')}
2020-05-05 10:56:16 -04:00
subtitle={__('Invite someone you know by email and earn LBC when they join lbry.tv.')}
2020-01-14 09:57:34 -05:00
actions={
<React.Fragment>
2020-01-14 15:44:07 -05:00
<Form onSubmit={handleSubmit}>
2020-01-14 11:44:40 -05:00
<FormField
type="text"
label="Email"
placeholder="youremail@example.org"
name="email"
2020-01-14 15:44:07 -05:00
value={email}
2020-01-14 11:44:40 -05:00
error={errorMessage}
2020-01-14 15:44:07 -05:00
inputButton={<Button button="secondary" type="submit" label="Invite" disabled={isPending || !email} />}
2020-01-14 11:44:40 -05:00
onChange={event => {
2020-01-14 15:44:07 -05:00
handleEmailChanged(event);
2020-01-14 11:44:40 -05:00
}}
2020-01-14 09:57:34 -05:00
/>
<p className="help">
<I18nMessage
tokens={{
rewards_link: <Button button="link" navigate="/$/rewards" label={__('rewards')} />,
referral_faq_link: <Button button="link" label={__('FAQ')} href="https://lbry.com/faq/referrals" />,
}}
>
2020-01-14 15:44:07 -05:00
Earn %rewards_link% for inviting your friends. Read our %referral_faq_link% to learn more.
2020-01-14 09:57:34 -05:00
</I18nMessage>
</p>
</Form>
</React.Fragment>
}
/>
2020-01-14 15:44:07 -05:00
</div>
);
2017-08-17 23:31:44 -04:00
}
export default InviteNew;