// @flow import { SITE_NAME, DOMAIN } from 'config'; import * as PAGES from 'constants/pages'; import SUPPORTED_LANGUAGES from 'constants/supported_languages'; 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 'util/lbryURI'; import { Lbryio } from 'lbryinc'; import { useHistory } from 'react-router'; import YoutubeTransferStatus from 'component/youtubeTransferStatus'; import Nag from 'component/common/nag'; import { getDefaultLanguage, sortLanguageMap } from 'util/default-languages'; 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 [language, setLanguage] = React.useState(getDefaultLanguage()); 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, channel_language: language, 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)) { 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 ? ( ) : (
@
setLanguage(event.target.value)} value={language} > {sortLanguageMap(SUPPORTED_LANGUAGES).map(([langKey, langName]) => ( ))} setAcknowledgedTerms(!acknowledgedTerms)} label={ ), faq: (
), }} > This will verify you are an active YouTuber with over 300 subscribers and original content. Channel names cannot be changed once chosen, please be extra careful. %learn_more%.
} nag={errorMessage && } /> )}
); }