// @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: any, resolvingUris: Array, resolveUris: (Array) => void, }; function InviteNew(props: Props) { const { inviteNew, errorMessage, isPending, referralCode = '', channels, resolveUris, resolvingUris } = 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); /* Canonical Referral links * We need to make sure our channels are resolved so that canonical_url is present */ function handleReferralChange(code) { setReferralSource(code); // TODO: keep track of this in an array? if (code !== referralCode) { analytics.apiLogPublish(channels.find(ch => ch.name === code)); } } const [resolveStarted, setResolveStarted] = useState(false); const [hasResolved, setHasResolved] = useState(false); // join them so that useEffect doesn't update on new objects const uris = channels && channels.map(channel => channel.permanent_url).join(','); const channelCount = channels && channels.length; const resolvingCount = resolvingUris && resolvingUris.length; const topChannel = channels && channels.reduce((top, channel) => (top && top.meta && top.meta.claims_in_channel) > channel.meta.claims_in_channel ? top : channel ); const referralString = channels && channels.length && referralSource !== referralCode ? lookupUrlByClaimName(referralSource, channels) : referralSource; const referral = `${URL}/$/invite/${referralString.replace('#', ':')}`; useEffect(() => { // resolve once, after we have channel list if (!hasResolved && !resolveStarted && channelCount) { setResolveStarted(true); resolveUris(uris.split(',')); } }, [channelCount, resolveStarted, hasResolved, resolvingCount, uris]); useEffect(() => { // once resolving count is 0, we know we're done if (resolveStarted && !hasResolved && resolvingCount === 0) { setHasResolved(true); } }, [resolveStarted, hasResolved, resolvingCount]); useEffect(() => { // set default channel if (topChannel && hasResolved) { handleReferralChange(topChannel.name); } }, [topChannel, hasResolved]); function lookupUrlByClaimName(name, channels) { const claim = channels.find(channel => channel.name === name); return claim ? claim.canonical_url.replace('lbry://', '') : name; } return (
handleReferralChange(channel)} label={'Code or Channel'} injected={[referralCode]} /> } onChange={event => { handleEmailChanged(event); }} />

, referral_faq_link: