// @flow import { SITE_NAME, DOMAIN } from 'config'; import * as PAGES from 'constants/pages'; import React from 'react'; import Page from 'component/page'; import Button from 'component/button'; import Card from 'component/common/card'; import I18nMessage from 'component/i18nMessage'; import { Form, FormField } from 'component/common/form'; import { INVALID_NAME_ERROR } from 'constants/claim'; import { isNameValid } from 'lbry-redux'; import { Lbryio } from 'lbryinc'; import { useHistory } from 'react-router'; import YoutubeTransferStatus from 'component/youtubeTransferStatus'; import Nag from 'component/common/nag'; const STATUS_TOKEN_PARAM = 'status_token'; const ERROR_MESSAGE_PARAM = 'error_message'; const NEW_CHANNEL_PARAM = 'new_channel'; type Props = { youtubeChannels: ?Array<{ transfer_state: string, sync_status: string }>, doUserFetch: () => void, inSignUpFlow?: boolean, doToggleInterestedInYoutubeSync: () => void, }; export default function YoutubeSync(props: Props) { const { youtubeChannels, doUserFetch, inSignUpFlow = false, doToggleInterestedInYoutubeSync } = props; const { location: { search, pathname }, push, replace, } = useHistory(); const urlParams = new URLSearchParams(search); const statusToken = urlParams.get(STATUS_TOKEN_PARAM); const errorMessage = urlParams.get(ERROR_MESSAGE_PARAM); const newChannelParam = urlParams.get(NEW_CHANNEL_PARAM); const [channel, setChannel] = React.useState(''); const [nameError, setNameError] = React.useState(undefined); const [acknowledgedTerms, setAcknowledgedTerms] = React.useState(false); const [addingNewChannel, setAddingNewChannel] = React.useState(newChannelParam); const hasYoutubeChannels = youtubeChannels && youtubeChannels.length > 0; React.useEffect(() => { const urlParamsInEffect = new URLSearchParams(search); if (!urlParamsInEffect.get('reset_scroll')) { urlParamsInEffect.append('reset_scroll', 'youtube'); } replace(`?${urlParamsInEffect.toString()}`); }, [pathname, search]); React.useEffect(() => { if (statusToken && !hasYoutubeChannels) { doUserFetch(); } }, [statusToken, hasYoutubeChannels, doUserFetch]); React.useEffect(() => { if (!newChannelParam) { setAddingNewChannel(false); } }, [newChannelParam]); function handleCreateChannel() { Lbryio.call('yt', 'new', { type: 'sync', immediate_sync: true, desired_lbry_channel_name: `@${channel}`, return_url: `https://${DOMAIN}/$/${inSignUpFlow ? PAGES.AUTH : PAGES.YOUTUBE_SYNC}`, }).then(ytAuthUrl => { // react-router isn't needed since it's a different domain window.location.href = ytAuthUrl; }); } function handleChannelChange(e) { const { value } = e.target; setChannel(value); if (!isNameValid(value, 'false')) { setNameError(INVALID_NAME_ERROR); } else { setNameError(); } } function handleNewChannel() { urlParams.append('new_channel', 'true'); push(`${pathname}?${urlParams.toString()}`); setAddingNewChannel(true); } const Wrapper = (props: { children: any }) => { return inSignUpFlow ? ( <>{props.children} ) : ( {props.children} ); }; return (
{hasYoutubeChannels && !addingNewChannel ? ( ) : (
@
setAcknowledgedTerms(!acknowledgedTerms)} label={ ), faq: (
, }} > This will verify you are an active YouTuber. Channel names cannot be changed once chosen, please be extra careful. Additional instructions will be emailed to you after you verify your email on the next page. %learn_more%.
} nag={errorMessage && } /> )}
); }