// @flow import React, { useEffect, useState } from 'react'; import Button from 'component/button'; import { Form, FormField } from 'component/common/form'; import CopyableText from 'component/copyableText'; import Card from 'component/common/card'; import { URL } from 'config'; import SelectChannel from 'component/selectChannel'; import analytics from 'analytics'; import I18nMessage from 'component/i18nMessage'; type Props = { errorMessage: ?string, inviteNew: string => void, isPending: boolean, referralLink: string, referralCode: string, channels: ?Array, }; function InviteNew(props: Props) { const { inviteNew, errorMessage, isPending, referralCode = '', channels } = props; // Email const [email, setEmail] = useState(''); function handleSubmit() { inviteNew(email); } function handleEmailChanged(event: any) { setEmail(event.target.value); } // 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); } } 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) { handleReferralChange(topChannel.name); } }, [topChannel]); function lookupUrlByClaimName(name, channels) { const claim = channels.find(channel => channel.name === name); return claim && claim.canonical_url ? claim.canonical_url.replace('lbry://', '') : name; } return (
handleReferralChange(channel)} label={__('Customize link')} hideAnon injected={[referralCode]} />

, referral_faq_link:

); } export default InviteNew;