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

247 lines
8.1 KiB
React
Raw Normal View History

2019-08-27 16:43:42 +02:00
// @flow
2022-05-02 08:04:20 +02:00
import * as MODALS from 'constants/modal_types';
2019-08-27 16:43:42 +02:00
import React, { useState } from 'react';
import { isNameValid } from 'util/lbryURI';
2019-08-27 16:43:42 +02:00
import Button from 'component/button';
import { Form, FormField } from 'component/common/form';
2019-10-03 23:40:54 +02:00
import { INVALID_NAME_ERROR } from 'constants/claim';
import Card from 'component/common/card';
2020-09-03 22:05:38 +02:00
import I18nMessage from 'component/i18nMessage';
import analytics from 'analytics';
2022-05-02 08:04:20 +02:00
import { sortLanguageMap } from 'util/default-languages';
import SUPPORTED_LANGUAGES from 'constants/supported_languages';
2022-05-02 09:48:28 +02:00
import ThumbnailBrokenImage from 'component/selectThumbnail/thumbnail-broken.png';
2022-05-04 08:15:20 +02:00
import { AVATAR_DEFAULT, THUMBNAIL_CDN_SIZE_LIMIT_BYTES } from 'config';
2022-05-02 08:04:20 +02:00
import * as ICONS from 'constants/icons';
export const DEFAULT_BID_FOR_FIRST_CHANNEL = 0.01;
2019-08-27 16:43:42 +02:00
type Props = {
2022-05-02 09:48:28 +02:00
createChannel: (string, number, any) => Promise<ChannelClaim>,
2019-08-27 16:43:42 +02:00
creatingChannel: boolean,
2019-09-26 18:07:11 +02:00
createChannelError: string,
claimingReward: boolean,
user: User,
doToggleInterestedInYoutubeSync: () => void,
2022-05-02 08:04:20 +02:00
openModal: (
id: string,
{ onUpdate: (string, boolean) => void, assetName: string, helpText: string, currentValue: string, title: string }
) => void,
2019-08-27 16:43:42 +02:00
};
function UserFirstChannel(props: Props) {
const {
createChannel,
creatingChannel,
claimingReward,
user,
createChannelError,
doToggleInterestedInYoutubeSync,
2022-05-02 08:04:20 +02:00
openModal,
} = props;
2019-09-26 18:07:11 +02:00
const { primary_email: primaryEmail } = user;
const initialChannel = primaryEmail ? primaryEmail.split('@')[0] : '';
2019-09-26 18:07:11 +02:00
const [channel, setChannel] = useState(initialChannel);
2022-05-02 08:04:20 +02:00
const [title, setTitle] = useState(initialChannel);
const [isUpload, setIsUpload] = React.useState({ cover: false, thumbnail: false });
const [thumbError, setThumbError] = React.useState(false);
const [params, setParams]: [any, (any) => void] = React.useState(getChannelParams());
const LANG_NONE = 'none';
2022-05-04 08:15:20 +02:00
const [languageParam, setLanguageParam] = useState([]);
2022-05-02 08:04:20 +02:00
const primaryLanguage = Array.isArray(languageParam) && languageParam.length && languageParam[0];
2019-09-26 18:07:11 +02:00
const [nameError, setNameError] = useState(undefined);
2019-08-27 16:43:42 +02:00
2022-05-02 08:04:20 +02:00
function getChannelParams() {
// fill this in with sdk data
const channelParams: {
title: string,
languages: ?Array<string>,
} = {
title,
2022-05-04 08:15:20 +02:00
languages: languageParam || [],
2022-05-02 08:04:20 +02:00
};
return channelParams;
}
let thumbnailPreview;
if (!params.thumbnailUrl) {
2022-05-04 08:15:20 +02:00
thumbnailPreview = AVATAR_DEFAULT;
2022-05-02 08:04:20 +02:00
} else if (thumbError) {
thumbnailPreview = ThumbnailBrokenImage;
} else {
thumbnailPreview = params.thumbnailUrl;
}
function handleThumbnailChange(thumbnailUrl: string, uploadSelected: boolean) {
setParams({ ...params, thumbnailUrl });
setIsUpload({ ...isUpload, thumbnail: uploadSelected });
setThumbError(false);
}
2022-05-02 09:48:28 +02:00
function handleLanguageChange(index, code) {
let langs = [...languageParam];
if (index === 0) {
if (code === LANG_NONE) {
// clear all
langs = [];
} else {
langs[0] = code;
}
} else {
if (code === LANG_NONE || code === langs[0]) {
langs.splice(1, 1);
} else {
langs[index] = code;
}
}
2022-05-04 08:15:20 +02:00
setLanguageParam(langs);
// setParams({ ...params, languages: langs });
2022-05-02 09:48:28 +02:00
}
2019-08-27 16:43:42 +02:00
function handleCreateChannel() {
2022-05-04 08:15:20 +02:00
createChannel(`@${channel}`, DEFAULT_BID_FOR_FIRST_CHANNEL, {
title: title,
thumbnailUrl: params.thumbnailUrl,
languages: primaryLanguage,
}).then((channelClaim) => {
if (channelClaim) {
analytics.apiLogPublish(channelClaim);
}
});
2019-08-27 16:43:42 +02:00
}
function handleChannelChange(e) {
const { value } = e.target;
setChannel(value);
if (!isNameValid(value)) {
2019-10-03 23:40:54 +02:00
setNameError(INVALID_NAME_ERROR);
2019-08-27 16:43:42 +02:00
} else {
setNameError();
}
}
2022-05-02 09:48:28 +02:00
function handleTitleChange(e) {
const { value } = e.target;
setTitle(value);
}
2019-08-27 16:43:42 +02:00
return (
<div className="main__channel-creation">
<Card
2020-08-25 00:23:38 +02:00
title={__('Create a Channel')}
subtitle={
<React.Fragment>
2020-08-25 00:23:38 +02:00
<p>{__('Your channel will be used for publishing and commenting.')}</p>
<p>{__('You can have more than one or remove it later.')}</p>
</React.Fragment>
}
actions={
<Form onSubmit={handleCreateChannel}>
2022-05-02 09:48:28 +02:00
<fieldset-section>
2022-05-04 08:15:20 +02:00
<label>{__('Avatar')}</label>
2022-05-02 09:48:28 +02:00
<div className="form-field__avatar_upload">
<img className="form-field__avatar" src={thumbnailPreview} />
<Button
button="alt"
title={__('Edit')}
onClick={() =>
openModal(MODALS.IMAGE_UPLOAD, {
onUpdate: (thumbnailUrl, isUpload) => handleThumbnailChange(thumbnailUrl, isUpload),
title: __('Edit Thumbnail Image'),
helpText: __('(1:1 ratio, %max_size%MB max)', {
max_size: THUMBNAIL_CDN_SIZE_LIMIT_BYTES / (1024 * 1024),
}),
assetName: __('Thumbnail'),
currentValue: params.thumbnailUrl,
})
}
icon={ICONS.CAMERA}
iconSize={18}
/>
</div>
</fieldset-section>
<fieldset-section>
<FormField
autoFocus
type="text"
name="channel_title2"
2022-05-04 08:15:20 +02:00
label={__('Display Name')}
2022-05-02 09:48:28 +02:00
placeholder={__('My Awesome Channel')}
value={title}
onChange={handleTitleChange}
/>
</fieldset-section>
<fieldset-group class="fieldset-group--smushed fieldset-group--disabled-prefix">
<fieldset-section>
<label htmlFor="auth_first_channel">
{createChannelError || nameError ? (
<span className="error__text">{createChannelError || nameError}</span>
) : (
2022-05-04 08:15:20 +02:00
__('Username')
)}
</label>
<div className="form-field__prefix">@</div>
</fieldset-section>
2019-09-26 18:07:11 +02:00
<FormField
placeholder={__('channel')}
type="text"
name="auth_first_channel"
2022-05-02 08:04:20 +02:00
className="form-field"
value={channel}
onChange={handleChannelChange}
/>
</fieldset-group>
2022-05-02 08:04:20 +02:00
<fieldset-section>
<FormField
name="language_select"
type="select"
label={__('Primary Language')}
onChange={(event) => handleLanguageChange(0, event.target.value)}
value={primaryLanguage}
helper={__('Your main content language')}
>
<option key={'pri-langNone'} value={LANG_NONE}>
{__('None selected')}
</option>
{sortLanguageMap(SUPPORTED_LANGUAGES).map(([langKey, langName]) => (
<option key={langKey} value={langKey}>
{langName}
</option>
))}
</FormField>
</fieldset-section>
<div className="section__actions">
<Button
button="primary"
type="submit"
disabled={nameError || !channel || creatingChannel || claimingReward}
label={creatingChannel || claimingReward ? __('Creating') : __('Create')}
/>
</div>
2020-09-03 22:05:38 +02:00
<div className="help--card-actions">
<I18nMessage
tokens={{
sync_channel: (
<Button
button="link"
label={__('Sync it and skip this step')}
onClick={() => doToggleInterestedInYoutubeSync()}
2020-09-03 22:05:38 +02:00
/>
),
}}
>
Have a YouTube channel? %sync_channel%.
</I18nMessage>
</div>
</Form>
}
/>
</div>
2019-08-27 16:43:42 +02:00
);
}
export default UserFirstChannel;