2018-03-26 23:32:43 +02:00
// @flow
2019-10-11 02:37:18 +02:00
/ *
On submit , this component calls publish , which dispatches doPublishDesktop .
doPublishDesktop calls lbry - redux Lbry publish method using lbry - redux publish state as params .
Publish simply instructs the SDK to find the file path on disk and publish it with the provided metadata .
On web , the Lbry publish method call is overridden in platform / web / api - setup , using a function in platform / web / publish .
File upload is carried out in the background by that function .
* /
2020-07-28 21:10:07 +02:00
2020-07-24 18:20:25 +02:00
import { SITE _NAME } from 'config' ;
2021-02-09 17:05:56 +01:00
import React , { useEffect } from 'react' ;
2019-07-24 20:21:34 +02:00
import { buildURI , isURIValid , isNameValid , THUMBNAIL _STATUSES } from 'lbry-redux' ;
2018-03-26 23:32:43 +02:00
import Button from 'component/button' ;
2021-02-09 17:05:56 +01:00
import ChannelSelect from 'component/channelSelector' ;
2018-03-26 23:32:43 +02:00
import classnames from 'classnames' ;
2019-10-08 22:40:10 +02:00
import TagsSelect from 'component/tagsSelect' ;
2020-07-28 01:12:59 +02:00
import PublishDescription from 'component/publishDescription' ;
2019-06-28 09:27:55 +02:00
import PublishPrice from 'component/publishPrice' ;
import PublishFile from 'component/publishFile' ;
2021-02-09 17:05:56 +01:00
import PublishBid from 'component/publishBid' ;
2019-06-28 09:27:55 +02:00
import PublishAdditionalOptions from 'component/publishAdditionalOptions' ;
import PublishFormErrors from 'component/publishFormErrors' ;
import SelectThumbnail from 'component/selectThumbnail' ;
2019-09-27 20:56:15 +02:00
import Card from 'component/common/card' ;
2020-05-26 06:10:05 +02:00
import I18nMessage from 'component/i18nMessage' ;
2020-07-28 01:12:59 +02:00
import * as PUBLISH _MODES from 'constants/publish_types' ;
2020-07-28 21:10:07 +02:00
// @if TARGET='app'
import fs from 'fs' ;
2020-07-29 06:00:47 +02:00
import tempy from 'tempy' ;
2020-07-28 21:10:07 +02:00
// @endif
2020-07-28 01:12:59 +02:00
const MODES = Object . values ( PUBLISH _MODES ) ;
2018-03-26 23:32:43 +02:00
2020-10-09 08:58:46 +02:00
const MODE _TO _I18N _STR = {
[ PUBLISH _MODES . FILE ] : 'File' ,
[ PUBLISH _MODES . POST ] : 'Post --[noun, markdown post tab button]--' ,
2021-03-11 17:26:11 +01:00
[ PUBLISH _MODES . LIVESTREAM ] : 'Livestream --[noun, livestream tab button]--' ,
2020-10-09 08:58:46 +02:00
} ;
2018-03-26 23:32:43 +02:00
type Props = {
2019-10-28 19:53:59 +01:00
disabled : boolean ,
2019-06-28 09:27:55 +02:00
tags : Array < Tag > ,
2020-07-31 15:33:49 +02:00
publish : ( source ? : string | File , ? boolean ) => void ,
2020-08-11 03:26:44 +02:00
filePath : string | File ,
fileText : string ,
2018-03-26 23:32:43 +02:00
bid : ? number ,
2020-03-02 18:11:14 +01:00
bidError : ? string ,
2018-03-26 23:32:43 +02:00
editingURI : ? string ,
title : ? string ,
thumbnail : ? string ,
2018-04-02 15:53:29 +02:00
uploadThumbnailStatus : ? string ,
2018-06-08 06:05:45 +02:00
thumbnailPath : ? string ,
2018-03-26 23:32:43 +02:00
description : ? string ,
language : string ,
nsfw : boolean ,
contentIsFree : boolean ,
2019-05-10 01:26:03 +02:00
fee : {
2019-05-11 00:27:07 +02:00
amount : string ,
2018-03-26 23:32:43 +02:00
currency : string ,
} ,
name : ? string ,
nameError : ? string ,
isResolvingUri : boolean ,
winningBidForClaimUri : number ,
2019-04-24 16:02:08 +02:00
myClaimForUri : ? StreamClaim ,
2018-03-26 23:32:43 +02:00
licenseType : string ,
otherLicenseDescription : ? string ,
licenseUrl : ? string ,
2020-07-03 00:42:04 +02:00
useLBRYUploader : ? boolean ,
2018-03-26 23:32:43 +02:00
publishing : boolean ,
balance : number ,
2018-06-12 07:11:17 +02:00
isStillEditing : boolean ,
2018-03-26 23:32:43 +02:00
clearPublish : ( ) => void ,
2021-03-11 17:26:11 +01:00
resolveUri : ( string ) => void ,
2018-03-26 23:32:43 +02:00
scrollToTop : ( ) => void ,
2018-10-13 17:49:47 +02:00
prepareEdit : ( claim : any , uri : string ) => void ,
2018-04-02 15:53:29 +02:00
resetThumbnailStatus : ( ) => void ,
2018-09-25 02:17:08 +02:00
amountNeededForTakeover : ? number ,
2019-06-28 09:27:55 +02:00
// Add back type
2021-03-11 17:26:11 +01:00
updatePublishForm : ( any ) => void ,
checkAvailability : ( string ) => void ,
2020-07-03 00:42:04 +02:00
ytSignupPending : boolean ,
2020-08-03 09:57:55 +02:00
modal : { id : string , modalProps : { } } ,
2020-08-11 11:54:09 +02:00
enablePublishPreview : boolean ,
2021-02-09 17:05:56 +01:00
activeChannelClaim : ? ChannelClaim ,
incognito : boolean ,
2018-03-26 23:32:43 +02:00
} ;
2019-06-28 09:27:55 +02:00
function PublishForm ( props : Props ) {
2020-07-28 01:12:59 +02:00
const [ mode , setMode ] = React . useState ( PUBLISH _MODES . FILE ) ;
const [ autoSwitchMode , setAutoSwitchMode ] = React . useState ( true ) ;
2020-07-30 06:03:56 +02:00
// Used to checl if the url name has changed:
// A new file needs to be provided
const [ prevName , setPrevName ] = React . useState ( false ) ;
2020-07-28 01:12:59 +02:00
// Used to checl if the file has been modified by user
const [ fileEdited , setFileEdited ] = React . useState ( false ) ;
const [ prevFileText , setPrevFileText ] = React . useState ( '' ) ;
2019-06-28 09:27:55 +02:00
const {
thumbnail ,
name ,
editingURI ,
2020-06-21 16:51:02 +02:00
myClaimForUri ,
2019-06-28 09:27:55 +02:00
resolveUri ,
title ,
bid ,
2020-03-02 18:11:14 +01:00
bidError ,
2019-06-28 09:27:55 +02:00
uploadThumbnailStatus ,
resetThumbnailStatus ,
updatePublishForm ,
filePath ,
2020-07-28 01:12:59 +02:00
fileText ,
2019-06-28 09:27:55 +02:00
publishing ,
clearPublish ,
isStillEditing ,
tags ,
publish ,
2019-10-28 19:53:59 +01:00
disabled = false ,
2020-04-24 15:51:00 +02:00
checkAvailability ,
2020-07-03 00:42:04 +02:00
ytSignupPending ,
2020-08-03 09:57:55 +02:00
modal ,
2020-08-11 11:54:09 +02:00
enablePublishPreview ,
2021-02-09 17:05:56 +01:00
activeChannelClaim ,
incognito ,
2019-06-28 09:27:55 +02:00
} = props ;
2020-07-28 01:12:59 +02:00
2020-03-05 22:35:01 +01:00
const TAGS _LIMIT = 5 ;
2021-03-11 17:26:11 +01:00
const fileFormDisabled = ( mode === PUBLISH _MODES . FILE || mode === PUBLISH _MODES . LIVESTREAM ) && ! filePath ;
2020-07-29 22:30:26 +02:00
const emptyPostError = mode === PUBLISH _MODES . POST && ( ! fileText || fileText . trim ( ) === '' ) ;
const formDisabled = ( fileFormDisabled && ! editingURI ) || emptyPostError || publishing ;
2019-12-26 16:37:26 +01:00
const isInProgress = filePath || editingURI || name || title ;
2021-02-09 17:05:56 +01:00
const activeChannelName = activeChannelClaim && activeChannelClaim . name ;
2020-07-29 04:56:07 +02:00
// Editing content info
const uri = myClaimForUri ? myClaimForUri . permanent _url : undefined ;
2020-10-26 19:56:38 +01:00
const fileMimeType =
myClaimForUri && myClaimForUri . value && myClaimForUri . value . source
? myClaimForUri . value . source . media _type
: undefined ;
2020-07-30 06:03:56 +02:00
const nameEdited = isStillEditing && name !== prevName ;
2019-12-26 16:37:26 +01:00
2019-06-28 09:27:55 +02:00
// If they are editing, they don't need a new file chosen
2019-07-24 20:21:34 +02:00
const formValidLessFile =
2020-03-02 18:11:14 +01:00
name &&
isNameValid ( name , false ) &&
title &&
bid &&
! bidError &&
2020-07-29 22:30:26 +02:00
! emptyPostError &&
2020-03-02 18:11:14 +01:00
! ( uploadThumbnailStatus === THUMBNAIL _STATUSES . IN _PROGRESS ) ;
2020-07-28 04:19:00 +02:00
2020-06-21 16:51:02 +02:00
const isOverwritingExistingClaim = ! editingURI && myClaimForUri ;
2020-07-28 04:19:00 +02:00
2020-06-21 16:51:02 +02:00
const formValid = isOverwritingExistingClaim
? false
: editingURI && ! filePath
? isStillEditing && formValidLessFile
: formValidLessFile ;
2019-06-28 09:27:55 +02:00
2020-08-03 09:57:55 +02:00
const [ previewing , setPreviewing ] = React . useState ( false ) ;
useEffect ( ( ) => {
if ( ! modal ) {
setTimeout ( ( ) => {
setPreviewing ( false ) ;
} , 250 ) ;
}
} , [ modal ] ) ;
2019-06-28 09:27:55 +02:00
let submitLabel ;
if ( isStillEditing ) {
2020-04-29 00:30:28 +02:00
submitLabel = ! publishing ? _ _ ( 'Save' ) : _ _ ( 'Saving...' ) ;
2020-08-03 09:57:55 +02:00
} else if ( previewing ) {
submitLabel = _ _ ( 'Preparing...' ) ;
2019-06-28 09:27:55 +02:00
} else {
2020-07-23 19:02:07 +02:00
submitLabel = ! publishing ? _ _ ( 'Upload' ) : _ _ ( 'Uploading...' ) ;
2018-03-26 23:32:43 +02:00
}
2019-06-28 09:27:55 +02:00
useEffect ( ( ) => {
2018-07-17 19:43:43 +02:00
if ( ! thumbnail ) {
2019-06-28 09:27:55 +02:00
resetThumbnailStatus ( ) ;
2018-06-13 06:19:39 +02:00
}
2019-06-28 09:27:55 +02:00
} , [ thumbnail , resetThumbnailStatus ] ) ;
2018-04-02 15:53:29 +02:00
2020-07-30 06:03:56 +02:00
// Save current name of the editing claim
useEffect ( ( ) => {
if ( isStillEditing && ( ! prevName || ! prevName . trim ( ) === '' ) ) {
if ( name !== prevName ) {
setPrevName ( name ) ;
}
}
} , [ name , prevName , setPrevName , isStillEditing ] ) ;
2020-07-28 01:12:59 +02:00
// Check for content changes on the text editor
useEffect ( ( ) => {
if ( ! fileEdited && fileText !== prevFileText && fileText !== '' ) {
setFileEdited ( true ) ;
} else if ( fileEdited && fileText === prevFileText ) {
setFileEdited ( false ) ;
}
} , [ fileText , prevFileText , fileEdited ] ) ;
2019-06-28 09:27:55 +02:00
// Every time the channel or name changes, resolve the uris to find winning bid amounts
useEffect ( ( ) => {
2018-09-25 02:17:08 +02:00
// We are only going to store the full uri, but we need to resolve the uri with and without the channel name
2018-04-04 01:17:40 +02:00
let uri ;
try {
2021-02-09 17:05:56 +01:00
uri = name && buildURI ( { streamName : name , activeChannelName } ) ;
2019-06-28 09:27:55 +02:00
} catch ( e ) { }
2021-02-09 17:05:56 +01:00
if ( activeChannelName && name ) {
2019-06-28 09:27:55 +02:00
// resolve without the channel name so we know the winning bid for it
2019-07-02 06:49:21 +02:00
try {
2019-08-30 01:18:06 +02:00
const uriLessChannel = buildURI ( { streamName : name } ) ;
2019-07-02 06:49:21 +02:00
resolveUri ( uriLessChannel ) ;
} catch ( e ) { }
2017-06-30 10:45:54 +02:00
}
2019-07-03 16:49:28 +02:00
const isValid = isURIValid ( uri ) ;
2020-04-24 15:51:00 +02:00
if ( uri && isValid && checkAvailability && name ) {
2018-04-04 01:17:40 +02:00
resolveUri ( uri ) ;
2020-04-24 15:51:00 +02:00
checkAvailability ( name ) ;
2019-06-28 09:27:55 +02:00
updatePublishForm ( { uri } ) ;
2017-06-30 10:45:54 +02:00
}
2021-02-09 17:05:56 +01:00
} , [ name , activeChannelName , resolveUri , updatePublishForm , checkAvailability ] ) ;
2019-06-28 09:27:55 +02:00
2020-10-01 09:52:52 +02:00
useEffect ( ( ) => {
2021-03-11 17:26:11 +01:00
updatePublishForm ( {
isMarkdownPost : mode === PUBLISH _MODES . POST ,
isLivestreamPublish : mode === PUBLISH _MODES . LIVESTREAM ,
} ) ;
2020-10-01 09:52:52 +02:00
} , [ mode , updatePublishForm ] ) ;
2021-02-09 17:05:56 +01:00
useEffect ( ( ) => {
2021-02-11 07:01:07 +01:00
if ( incognito ) {
2021-02-09 17:05:56 +01:00
updatePublishForm ( { channel : undefined } ) ;
2021-03-11 17:26:11 +01:00
// Anonymous livestreams aren't supported
if ( mode === PUBLISH _MODES . LIVESTREAM ) {
setMode ( PUBLISH _MODES . FILE ) ;
}
2021-02-11 07:01:07 +01:00
} else if ( activeChannelName ) {
updatePublishForm ( { channel : activeChannelName } ) ;
2021-02-09 17:05:56 +01:00
}
} , [ activeChannelName , incognito , updatePublishForm ] ) ;
2020-06-16 11:58:25 +02:00
2020-07-28 21:10:07 +02:00
// @if TARGET='web'
2020-07-28 01:12:59 +02:00
function createWebFile ( ) {
if ( fileText ) {
2020-07-29 22:30:26 +02:00
const fileName = name || title ;
2020-08-11 03:26:44 +02:00
if ( fileName ) {
return new File ( [ fileText ] , ` ${ fileName } .md ` , { type : 'text/markdown' } ) ;
}
2020-07-28 01:12:59 +02:00
}
}
2020-07-28 21:10:07 +02:00
// @endif
2020-07-28 01:12:59 +02:00
2020-07-28 21:10:07 +02:00
// @if TARGET='app'
// Save file changes locally ( desktop )
2020-07-29 06:00:47 +02:00
function saveFileChanges ( ) {
2020-08-11 03:26:44 +02:00
let output ;
2020-07-28 01:12:59 +02:00
if ( ! output || output === '' ) {
2020-07-29 06:00:47 +02:00
// Generate a temporary file:
2020-07-29 22:30:26 +02:00
output = tempy . file ( { name : 'post.md' } ) ;
2020-08-11 03:26:44 +02:00
} else if ( typeof filePath === 'string' ) {
// Use current file
output = filePath ;
2020-07-28 01:12:59 +02:00
}
2020-07-29 06:00:47 +02:00
// Create a temporary file and save file changes
2020-08-11 03:26:44 +02:00
if ( output && output !== '' ) {
2020-07-28 01:12:59 +02:00
// Save file changes
return new Promise ( ( resolve , reject ) => {
fs . writeFile ( output , fileText , ( error , data ) => {
// Handle error, cant save changes or create file
error ? reject ( error ) : resolve ( output ) ;
} ) ;
} ) ;
}
}
2020-07-28 21:10:07 +02:00
// @endif
2020-07-28 01:12:59 +02:00
async function handlePublish ( ) {
2020-08-12 03:59:23 +02:00
let outputFile = filePath ;
let runPublish = false ;
2020-07-29 22:30:26 +02:00
// Publish post:
2020-07-28 01:12:59 +02:00
// If here is no file selected yet on desktop, show file dialog and let the
// user choose a file path. On web a new File is created
2020-08-05 03:55:41 +02:00
if ( mode === PUBLISH _MODES . POST && ! emptyPostError ) {
2020-07-30 06:03:56 +02:00
// If user modified content on the text editor or editing name has changed:
2020-07-30 06:07:00 +02:00
// Save changes and update file path
2020-07-30 06:03:56 +02:00
if ( fileEdited || nameEdited ) {
2020-07-28 01:12:59 +02:00
// @if TARGET='app'
outputFile = await saveFileChanges ( ) ;
// @endif
// @if TARGET='web'
outputFile = createWebFile ( ) ;
// @endif
// New content stored locally and is not empty
2020-08-05 03:55:41 +02:00
if ( outputFile ) {
2020-07-28 01:12:59 +02:00
updatePublishForm ( { filePath : outputFile } ) ;
2020-08-12 03:59:23 +02:00
runPublish = true ;
2020-07-28 01:12:59 +02:00
}
2020-08-05 03:55:41 +02:00
} else {
// Only metadata has changed.
2020-08-12 03:59:23 +02:00
runPublish = true ;
2020-07-28 01:12:59 +02:00
}
}
// Publish file
2021-03-11 17:26:11 +01:00
if ( mode === PUBLISH _MODES . FILE || mode === PUBLISH _MODES . LIVESTREAM ) {
2020-08-12 03:59:23 +02:00
runPublish = true ;
}
if ( runPublish ) {
2020-08-12 03:24:41 +02:00
if ( enablePublishPreview ) {
2020-08-03 09:57:55 +02:00
setPreviewing ( true ) ;
2020-08-12 03:59:23 +02:00
publish ( outputFile , true ) ;
2020-08-12 03:24:41 +02:00
} else {
2020-08-12 03:59:23 +02:00
publish ( outputFile , false ) ;
2020-07-31 15:33:49 +02:00
}
2020-07-28 01:12:59 +02:00
}
}
// Update mode on editing
useEffect ( ( ) => {
if ( autoSwitchMode && editingURI && myClaimForUri ) {
2020-07-29 22:30:26 +02:00
// Change publish mode to "post" if editing content type is markdown
if ( fileMimeType === 'text/markdown' && mode !== PUBLISH _MODES . POST ) {
setMode ( PUBLISH _MODES . POST ) ;
2020-07-28 01:12:59 +02:00
// Prevent forced mode
setAutoSwitchMode ( false ) ;
}
}
2020-07-29 04:56:07 +02:00
} , [ autoSwitchMode , editingURI , fileMimeType , myClaimForUri , mode , setMode , setAutoSwitchMode ] ) ;
2020-07-28 01:12:59 +02:00
// Editing claim uri
2019-06-28 09:27:55 +02:00
return (
2020-04-01 20:43:50 +02:00
< div className = "card-stack" >
2021-02-09 17:05:56 +01:00
< ChannelSelect disabled = { disabled } / >
2021-01-25 23:08:39 +01:00
2020-07-28 01:12:59 +02:00
< PublishFile
uri = { uri }
mode = { mode }
2020-07-29 04:56:07 +02:00
fileMimeType = { fileMimeType }
2020-07-28 01:12:59 +02:00
disabled = { disabled || publishing }
inProgress = { isInProgress }
setPublishMode = { setMode }
setPrevFileText = { setPrevFileText }
2020-08-05 19:06:24 +02:00
header = {
2020-08-05 19:19:15 +02:00
< >
2020-08-05 19:06:24 +02:00
{ MODES . map ( ( modeName , index ) => (
< Button
key = { index }
icon = { modeName }
2020-10-09 08:58:46 +02:00
label = { _ _ ( MODE _TO _I18N _STR [ String ( modeName ) ] || '---' ) }
2020-08-05 19:06:24 +02:00
button = "alt"
onClick = { ( ) => {
setMode ( modeName ) ;
} }
className = { classnames ( 'button-toggle' , { 'button-toggle--active' : mode === modeName } ) }
/ >
) ) }
2020-08-05 19:19:15 +02:00
< / >
2020-08-05 19:06:24 +02:00
}
2020-07-28 01:12:59 +02:00
/ >
2021-02-09 17:05:56 +01:00
2020-03-24 18:57:17 +01:00
{ ! publishing && (
< div className = { classnames ( { 'card--disabled' : formDisabled } ) } >
2020-07-28 01:12:59 +02:00
{ mode === PUBLISH _MODES . FILE && < PublishDescription disabled = { formDisabled } / > }
2020-03-24 18:57:17 +01:00
< Card actions = { < SelectThumbnail / > } / >
2020-04-01 20:43:50 +02:00
< TagsSelect
suggestMature
disableAutoFocus
hideHeader
label = { _ _ ( 'Selected Tags' ) }
empty = { _ _ ( 'No tags added' ) }
limitSelect = { TAGS _LIMIT }
help = { _ _ (
2020-07-23 19:11:53 +02:00
"Add tags that are relevant to your content so those who're looking for it can find it more easily. If mature content, ensure it is tagged mature. Tag abuse and missing mature tags will not be tolerated."
2020-04-01 20:43:50 +02:00
) }
placeholder = { _ _ ( 'gaming, crypto' ) }
2021-03-11 17:26:11 +01:00
onSelect = { ( newTags ) => {
2020-04-01 20:43:50 +02:00
const validatedTags = [ ] ;
2021-03-11 17:26:11 +01:00
newTags . forEach ( ( newTag ) => {
if ( ! tags . some ( ( tag ) => tag . name === newTag . name ) ) {
2020-04-01 20:43:50 +02:00
validatedTags . push ( newTag ) ;
}
} ) ;
updatePublishForm ( { tags : [ ... tags , ... validatedTags ] } ) ;
} }
2021-03-11 17:26:11 +01:00
onRemove = { ( clickedTag ) => {
const newTags = tags . slice ( ) . filter ( ( tag ) => tag . name !== clickedTag . name ) ;
2020-04-01 20:43:50 +02:00
updatePublishForm ( { tags : newTags } ) ;
} }
tagsChosen = { tags }
/ >
2019-09-27 20:56:15 +02:00
2021-02-09 17:05:56 +01:00
< PublishBid disabled = { isStillEditing || formDisabled } / >
2020-03-24 18:57:17 +01:00
< PublishPrice disabled = { formDisabled } / >
< PublishAdditionalOptions disabled = { formDisabled } / >
< / div >
) }
< section >
< div className = "card__actions" >
< Button
button = "primary"
2020-07-28 01:12:59 +02:00
onClick = { handlePublish }
2020-03-24 18:57:17 +01:00
label = { submitLabel }
2020-07-03 00:42:04 +02:00
disabled = {
2020-08-03 09:57:55 +02:00
formDisabled ||
! formValid ||
uploadThumbnailStatus === THUMBNAIL _STATUSES . IN _PROGRESS ||
ytSignupPending ||
previewing
2020-07-03 00:42:04 +02:00
}
2020-03-24 18:57:17 +01:00
/ >
< Button button = "link" onClick = { clearPublish } label = { _ _ ( 'Cancel' ) } / >
< / div >
< p className = "help" >
2020-07-08 00:24:24 +02:00
{ ! formDisabled && ! formValid ? (
2020-07-28 04:19:00 +02:00
< PublishFormErrors mode = { mode } / >
2020-07-08 00:24:24 +02:00
) : (
< I18nMessage
tokens = { {
lbry _terms _of _service : (
< Button
button = "link"
href = "https://www.lbry.com/termsofservice"
2020-07-24 18:20:25 +02:00
label = { _ _ ( '%site_name% Terms of Service' , { site _name : SITE _NAME } ) }
2020-07-08 00:24:24 +02:00
/ >
) ,
} }
>
By continuing , you accept the % lbry _terms _of _service % .
< / I18nMessage >
) }
2020-03-24 18:57:17 +01:00
< / p >
< / section >
2020-04-01 20:43:50 +02:00
< / div >
2019-06-28 09:27:55 +02:00
) ;
2017-06-30 10:45:54 +02:00
}
export default PublishForm ;