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
2021-04-08 21:19:14 +02:00
import { SITE _NAME , ENABLE _NO _SOURCE _CLAIMS , SIMPLE _SITE , CHANNEL _STAKED _LEVEL _LIVESTREAM } from 'config' ;
2021-04-14 06:06:11 +02:00
import React , { useEffect , useState } from 'react' ;
2021-10-17 10:36:14 +02:00
import Lbry from 'lbry' ;
import { buildURI , isURIValid , isNameValid } from 'util/lbryURI' ;
import * as THUMBNAIL _STATUSES from 'constants/thumbnail_upload_statuses' ;
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' ;
2021-03-13 23:51:06 +01:00
import { useHistory } from 'react-router' ;
2021-03-30 04:04:14 +02:00
import Spinner from 'component/spinner' ;
2021-04-14 06:06:11 +02:00
import { toHex } from 'util/hex' ;
2021-07-27 22:35:22 +02:00
import { LIVESTREAM _REPLAY _API } from 'constants/livestream' ;
2021-12-16 22:59:13 +01:00
import PublishStreamReleaseDate from 'component/publishStreamReleaseDate' ;
2021-12-29 23:10:19 +01:00
import { SOURCE _NONE } from 'constants/publish_sources' ;
2020-07-28 01:12:59 +02:00
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
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 ,
2021-04-21 05:31:54 +02:00
thumbnailError : ? boolean ,
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 ,
2021-04-23 18:57:51 +02:00
publishSuccess : boolean ,
2018-03-26 23:32:43 +02:00
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 ,
2021-04-08 21:19:14 +02:00
user : ? User ,
activeChannelStakedLevel : number ,
2021-04-14 06:06:11 +02:00
isLivestreamClaim : boolean ,
isPostClaim : boolean ,
permanentUrl : ? string ,
remoteUrl : ? string ,
2021-08-18 18:34:24 +02:00
isClaimingInitialRewards : boolean ,
claimInitialRewards : ( ) => void ,
hasClaimedInitialRewards : boolean ,
2018-03-26 23:32:43 +02:00
} ;
2019-06-28 09:27:55 +02:00
function PublishForm ( props : Props ) {
2021-03-13 23:51:06 +01:00
// Detect upload type from query in URL
2019-06-28 09:27:55 +02:00
const {
thumbnail ,
2021-04-21 05:31:54 +02:00
thumbnailError ,
2019-06-28 09:27:55 +02:00
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 ,
2021-04-23 18:57:51 +02:00
publishSuccess ,
2019-06-28 09:27:55 +02:00
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 ,
2021-03-23 18:27:30 +01:00
user ,
2021-04-08 21:19:14 +02:00
activeChannelStakedLevel ,
2021-04-14 06:06:11 +02:00
isLivestreamClaim ,
isPostClaim ,
permanentUrl ,
remoteUrl ,
2021-08-18 18:34:24 +02:00
isClaimingInitialRewards ,
claimInitialRewards ,
hasClaimedInitialRewards ,
2019-06-28 09:27:55 +02:00
} = props ;
2020-07-28 01:12:59 +02:00
2021-12-31 21:48:07 +01:00
const inEditMode = Boolean ( editingURI ) ;
2021-03-26 22:11:22 +01:00
const { replace , location } = useHistory ( ) ;
2021-03-24 01:49:29 +01:00
const urlParams = new URLSearchParams ( location . search ) ;
2021-04-14 06:06:11 +02:00
const TYPE _PARAM = 'type' ;
const uploadType = urlParams . get ( TYPE _PARAM ) ;
2021-04-14 17:56:45 +02:00
const _uploadType = uploadType && uploadType . toLowerCase ( ) ;
2021-04-14 06:06:11 +02:00
const enableLivestream =
2021-04-08 21:19:14 +02:00
ENABLE _NO _SOURCE _CLAIMS &&
user &&
! user . odysee _live _disabled &&
( activeChannelStakedLevel >= CHANNEL _STAKED _LEVEL _LIVESTREAM || user . odysee _live _enabled ) ;
2021-03-24 01:49:29 +01:00
// $FlowFixMe
2021-04-14 06:06:11 +02:00
const AVAILABLE _MODES = Object . values ( PUBLISH _MODES ) . filter ( ( mode ) => {
2021-04-14 17:56:45 +02:00
// $FlowFixMe
2021-12-31 21:48:07 +01:00
if ( inEditMode ) {
2021-04-14 06:06:11 +02:00
if ( isPostClaim ) {
return mode === PUBLISH _MODES . POST ;
} else if ( isLivestreamClaim ) {
return mode === PUBLISH _MODES . LIVESTREAM && enableLivestream ;
} else {
return mode === PUBLISH _MODES . FILE ;
}
2021-04-14 17:56:45 +02:00
} else if ( _uploadType ) {
return mode === _uploadType && ( mode !== PUBLISH _MODES . LIVESTREAM || enableLivestream ) ;
2021-04-14 06:06:11 +02:00
} else {
2021-04-14 17:56:45 +02:00
return mode !== PUBLISH _MODES . LIVESTREAM || enableLivestream ;
2021-04-14 06:06:11 +02:00
}
} ) ;
2021-03-24 01:49:29 +01:00
const MODE _TO _I18N _STR = {
2021-04-19 18:45:01 +02:00
[ PUBLISH _MODES . FILE ] : SIMPLE _SITE ? 'Video/Audio' : 'File' ,
2021-03-24 01:49:29 +01:00
[ PUBLISH _MODES . POST ] : 'Post --[noun, markdown post tab button]--' ,
[ PUBLISH _MODES . LIVESTREAM ] : 'Livestream --[noun, livestream tab button]--' ,
} ;
2021-04-14 06:06:11 +02:00
2021-12-22 22:45:47 +01:00
const defaultPublishMode = isLivestreamClaim ? PUBLISH _MODES . LIVESTREAM : PUBLISH _MODES . FILE ;
const [ mode , setMode ] = React . useState ( _uploadType || defaultPublishMode ) ;
2021-04-14 06:06:11 +02:00
const [ isCheckingLivestreams , setCheckingLivestreams ] = React . useState ( false ) ;
let customSubtitle ;
if ( mode === PUBLISH _MODES . LIVESTREAM || isLivestreamClaim ) {
if ( isLivestreamClaim ) {
customSubtitle = _ _ ( 'Update your livestream' ) ;
} else {
customSubtitle = _ _ ( 'Prepare an upcoming livestream' ) ;
}
} else if ( mode === PUBLISH _MODES . POST || isPostClaim ) {
if ( isPostClaim ) {
customSubtitle = _ _ ( 'Edit your post' ) ;
} else {
customSubtitle = _ _ ( 'Craft an epic post clearly explaining... whatever.' ) ;
}
} else {
if ( editingURI ) {
2021-07-23 05:58:15 +02:00
customSubtitle = _ _ ( 'Update your content' ) ;
2021-04-14 06:06:11 +02:00
} else {
2021-07-23 05:58:15 +02:00
customSubtitle = _ _ ( 'Upload that unlabeled video or cassette you found behind the TV in 1991' ) ;
2021-04-14 06:06:11 +02:00
}
}
2021-03-24 01:49:29 +01:00
const [ autoSwitchMode , setAutoSwitchMode ] = React . useState ( true ) ;
// Used to check if the url name has changed:
// A new file needs to be provided
const [ prevName , setPrevName ] = React . useState ( false ) ;
// Used to check if the file has been modified by user
const [ fileEdited , setFileEdited ] = React . useState ( false ) ;
const [ prevFileText , setPrevFileText ] = React . useState ( '' ) ;
2021-04-14 06:06:11 +02:00
const [ waitForFile , setWaitForFile ] = useState ( false ) ;
2022-02-01 23:02:30 +01:00
const [ overMaxBitrate , setOverMaxBitrate ] = useState ( false ) ;
2021-04-14 06:06:11 +02:00
const [ livestreamData , setLivestreamData ] = React . useState ( [ ] ) ;
const [ signedMessage , setSignedMessage ] = React . useState ( { signature : undefined , signing _ts : undefined } ) ;
const signedMessageStr = JSON . stringify ( signedMessage ) ;
2020-03-05 22:35:01 +01:00
const TAGS _LIMIT = 5 ;
2021-04-26 19:45:36 +02:00
const fileFormDisabled = mode === PUBLISH _MODES . FILE && ! filePath && ! remoteUrl ;
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 ;
2021-12-28 19:40:16 +01:00
const activeChannelClaimStr = activeChannelClaim && JSON . stringify ( activeChannelClaim ) ;
2020-07-29 04:56:07 +02:00
// Editing content info
2020-10-26 19:56:38 +01:00
const fileMimeType =
myClaimForUri && myClaimForUri . value && myClaimForUri . value . source
? myClaimForUri . value . source . media _type
: undefined ;
2021-04-14 17:56:45 +02:00
const claimChannelId =
( myClaimForUri && myClaimForUri . signing _channel && myClaimForUri . signing _channel . claim _id ) ||
( activeChannelClaim && activeChannelClaim . claim _id ) ;
2021-04-14 06:06:11 +02:00
2020-07-30 06:03:56 +02:00
const nameEdited = isStillEditing && name !== prevName ;
2021-08-26 16:51:53 +02:00
const thumbnailUploaded = uploadThumbnailStatus === THUMBNAIL _STATUSES . COMPLETE && thumbnail ;
2019-12-26 16:37:26 +01:00
2021-04-14 06:06:11 +02:00
const waitingForFile = waitForFile && ! remoteUrl && ! filePath ;
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 &&
2021-10-17 10:36:14 +02:00
isNameValid ( name ) &&
2020-03-02 18:11:14 +01:00
title &&
2022-02-01 23:02:30 +01:00
! overMaxBitrate &&
2020-03-02 18:11:14 +01:00
bid &&
2021-04-19 18:45:01 +02:00
thumbnail &&
2020-03-02 18:11:14 +01:00
! bidError &&
2020-07-29 22:30:26 +02:00
! emptyPostError &&
2021-08-26 16:51:53 +02:00
! ( thumbnailError && ! thumbnailUploaded ) &&
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
2021-04-14 06:06:11 +02:00
: editingURI && ! filePath // if we're editing we don't need a file
? isStillEditing && formValidLessFile && ! waitingForFile
2020-06-21 16:51:02 +02:00
: formValidLessFile ;
2019-06-28 09:27:55 +02:00
2020-08-03 09:57:55 +02:00
const [ previewing , setPreviewing ] = React . useState ( false ) ;
2021-04-14 06:06:11 +02:00
React . useEffect ( ( ) => {
if ( activeChannelClaimStr ) {
const channelClaim = JSON . parse ( activeChannelClaimStr ) ;
const message = 'get-claim-id-replays' ;
setSignedMessage ( { signature : null , signing _ts : null } ) ;
// ensure we have a channel
if ( channelClaim . claim _id ) {
Lbry . channel _sign ( {
channel _id : channelClaim . claim _id ,
hexdata : toHex ( message ) ,
} )
. then ( ( data ) => {
setSignedMessage ( data ) ;
} )
. catch ( ( error ) => {
setSignedMessage ( { signature : null , signing _ts : null } ) ;
} ) ;
}
}
} , [ activeChannelClaimStr , setSignedMessage ] ) ;
2021-08-18 18:34:24 +02:00
useEffect ( ( ) => {
if ( ! hasClaimedInitialRewards ) {
claimInitialRewards ( ) ;
}
} , [ hasClaimedInitialRewards , claimInitialRewards ] ) ;
2020-08-03 09:57:55 +02:00
useEffect ( ( ) => {
if ( ! modal ) {
setTimeout ( ( ) => {
setPreviewing ( false ) ;
} , 250 ) ;
}
} , [ modal ] ) ;
2021-04-14 06:06:11 +02:00
// move this to lbryinc OR to a file under ui, and/or provide a standardized livestreaming config.
function fetchLivestreams ( channelId , signature , timestamp ) {
setCheckingLivestreams ( true ) ;
2021-07-27 22:35:22 +02:00
fetch ( ` ${ LIVESTREAM _REPLAY _API } / ${ channelId } ?signature= ${ signature || '' } &signing_ts= ${ timestamp || '' } ` ) // claimChannelId
2021-04-14 06:06:11 +02:00
. then ( ( res ) => res . json ( ) )
. then ( ( res ) => {
if ( ! res || ! res . data ) {
setLivestreamData ( [ ] ) ;
}
setLivestreamData ( res . data ) ;
setCheckingLivestreams ( false ) ;
} )
. catch ( ( e ) => {
setLivestreamData ( [ ] ) ;
setCheckingLivestreams ( false ) ;
} ) ;
}
useEffect ( ( ) => {
const signedMessage = JSON . parse ( signedMessageStr ) ;
2021-04-14 17:56:45 +02:00
if ( claimChannelId && signedMessage . signature ) {
2021-04-14 06:06:11 +02:00
fetchLivestreams ( claimChannelId , signedMessage . signature , signedMessage . signing _ts ) ;
}
2021-04-14 17:56:45 +02:00
} , [ claimChannelId , signedMessageStr ] ) ;
2021-04-14 06:06:11 +02:00
const isLivestreamMode = mode === PUBLISH _MODES . LIVESTREAM ;
2019-06-28 09:27:55 +02:00
let submitLabel ;
2021-08-18 18:34:24 +02:00
if ( isClaimingInitialRewards ) {
submitLabel = _ _ ( 'Claiming credits...' ) ;
} else if ( publishing ) {
2021-03-24 06:22:02 +01:00
if ( isStillEditing ) {
submitLabel = _ _ ( 'Saving...' ) ;
2021-04-14 06:06:11 +02:00
} else if ( isLivestreamMode ) {
2021-03-24 06:22:02 +01:00
submitLabel = _ _ ( 'Creating...' ) ;
} else {
submitLabel = _ _ ( 'Uploading...' ) ;
}
2020-08-03 09:57:55 +02:00
} else if ( previewing ) {
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
submitLabel = < Spinner type = "small" / > ;
2019-06-28 09:27:55 +02:00
} else {
2021-03-24 06:22:02 +01:00
if ( isStillEditing ) {
submitLabel = _ _ ( 'Save' ) ;
2021-04-14 06:06:11 +02:00
} else if ( isLivestreamMode ) {
2021-03-24 06:22:02 +01:00
submitLabel = _ _ ( 'Create' ) ;
} else {
submitLabel = _ _ ( 'Upload' ) ;
}
2018-03-26 23:32:43 +02:00
}
2021-04-02 17:55:41 +02:00
// if you enter the page and it is stuck in publishing, "stop it."
useEffect ( ( ) => {
2021-04-23 18:57:51 +02:00
if ( publishing || publishSuccess ) {
2021-04-02 17:55:41 +02:00
clearPublish ( ) ;
}
2021-04-23 18:57:51 +02:00
} , [ clearPublish ] ) ;
2021-04-02 17:55:41 +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
2021-04-14 06:06:11 +02:00
// Save previous name of the editing claim
2020-07-30 06:03:56 +02:00
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
}
2021-10-17 10:36:14 +02:00
const isValid = uri && 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
2021-04-14 06:06:11 +02:00
// because publish editingUri is channel_short/claim_long and we don't have that, resolve it.
2021-03-30 01:05:18 +02:00
useEffect ( ( ) => {
if ( editingURI ) {
resolveUri ( editingURI ) ;
}
} , [ editingURI , resolveUri ] ) ;
2021-04-14 06:06:11 +02:00
// set isMarkdownPost in publish form if so, also update isLivestreamPublish
2020-10-01 09:52:52 +02:00
useEffect ( ( ) => {
2021-03-11 17:26:11 +01:00
updatePublishForm ( {
isMarkdownPost : mode === PUBLISH _MODES . POST ,
2021-04-14 06:06:11 +02:00
isLivestreamPublish : mode === PUBLISH _MODES . LIVESTREAM ,
2021-03-11 17:26:11 +01:00
} ) ;
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
2021-04-14 06:06:11 +02:00
if ( isLivestreamMode ) {
2021-03-11 17:26:11 +01:00
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
}
2021-04-14 06:06:11 +02:00
} , [ activeChannelName , incognito , updatePublishForm , isLivestreamMode ] ) ;
2020-06-16 11:58:25 +02:00
2021-04-14 06:06:11 +02:00
// set mode based on urlParams 'type'
2021-03-13 23:51:06 +01:00
useEffect ( ( ) => {
if ( ! _uploadType ) {
2021-12-22 22:45:47 +01:00
setMode ( defaultPublishMode ) ;
2021-03-13 23:51:06 +01:00
return ;
}
// File publish
if ( _uploadType === PUBLISH _MODES . FILE . toLowerCase ( ) ) {
setMode ( PUBLISH _MODES . FILE ) ;
return ;
}
// Post publish
if ( _uploadType === PUBLISH _MODES . POST . toLowerCase ( ) ) {
setMode ( PUBLISH _MODES . POST ) ;
return ;
}
// LiveStream publish
2021-04-14 06:06:11 +02:00
if ( _uploadType === PUBLISH _MODES . LIVESTREAM . toLowerCase ( ) ) {
if ( enableLivestream ) {
setMode ( PUBLISH _MODES . LIVESTREAM ) ;
} else {
setMode ( PUBLISH _MODES . FILE ) ;
}
2021-03-13 23:51:06 +01:00
return ;
}
2021-12-22 22:45:47 +01:00
setMode ( defaultPublishMode ) ;
} , [ _uploadType , enableLivestream , defaultPublishMode ] ) ;
2021-03-13 23:51:06 +01:00
2021-04-14 06:06:11 +02:00
// if we have a type urlparam, update it? necessary?
2021-03-13 23:51:06 +01:00
useEffect ( ( ) => {
2021-04-14 17:56:45 +02:00
if ( ! _uploadType ) return ;
2021-03-13 23:51:06 +01:00
const newParams = new URLSearchParams ( ) ;
2021-04-14 06:06:11 +02:00
newParams . set ( TYPE _PARAM , mode . toLowerCase ( ) ) ;
2021-03-26 22:11:22 +01:00
replace ( { search : newParams . toString ( ) } ) ;
2021-04-14 17:56:45 +02:00
} , [ mode , _uploadType ] ) ;
2021-03-13 23:51:06 +01: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-04-14 06:06:11 +02:00
if ( mode === PUBLISH _MODES . FILE || isLivestreamMode ) {
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
2021-08-19 18:50:58 +02:00
// When accessing to publishing, make sure to reset file input attributes
// since we can't restore from previous user selection (like we do
// with other properties such as name, title, etc.) for security reasons.
useEffect ( ( ) => {
if ( mode === PUBLISH _MODES . FILE ) {
updatePublishForm ( { filePath : '' , fileDur : 0 , fileSize : 0 } ) ;
}
} , [ mode , updatePublishForm ] ) ;
2021-12-31 21:48:07 +01:00
// FIle Source Selector State.
const [ fileSource , setFileSource ] = useState ( ) ;
const changeFileSource = ( state ) => setFileSource ( state ) ;
2021-12-29 23:10:19 +01:00
const [ showSchedulingOptions , setShowSchedulingOptions ] = useState ( false ) ;
useEffect ( ( ) => {
2021-12-31 21:48:07 +01:00
setShowSchedulingOptions ( isLivestreamMode && fileSource === SOURCE _NONE ) ;
} , [ isLivestreamMode , fileSource ] ) ;
2021-12-29 23:10:19 +01:00
2021-03-30 04:04:14 +02:00
if ( publishing ) {
return (
< div className = "main--empty" >
< h1 className = "section__subtitle" > { _ _ ( 'Publishing...' ) } < / h1 >
< Spinner delayed / >
< / div >
) ;
}
2021-12-29 23:10:19 +01:00
2020-07-28 01:12:59 +02:00
// Editing claim uri
2019-06-28 09:27:55 +02:00
return (
2022-02-11 19:50:55 +01:00
< div className = "card-stack uploadPage-wraper" >
2021-04-14 06:06:11 +02:00
< ChannelSelect hideAnon = { isLivestreamMode } disabled = { disabled } / >
2021-01-25 23:08:39 +01:00
2020-07-28 01:12:59 +02:00
< PublishFile
2021-12-31 21:48:07 +01:00
inEditMode = { inEditMode }
fileSource = { fileSource }
changeFileSource = { changeFileSource }
2021-04-14 06:06:11 +02:00
uri = { permanentUrl }
2020-07-28 01:12:59 +02:00
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 }
2021-04-14 06:06:11 +02:00
livestreamData = { livestreamData }
subtitle = { customSubtitle }
setWaitForFile = { setWaitForFile }
2022-02-01 23:02:30 +01:00
setOverMaxBitrate = { setOverMaxBitrate }
2021-04-14 06:06:11 +02:00
isCheckingLivestreams = { isCheckingLivestreams }
checkLivestreams = { fetchLivestreams }
channelId = { claimChannelId }
channelSignature = { signedMessage }
2020-08-05 19:06:24 +02:00
header = {
2020-08-05 19:19:15 +02:00
< >
2021-04-14 06:06:11 +02:00
{ AVAILABLE _MODES . map ( ( modeName ) => (
2020-08-05 19:06:24 +02:00
< Button
2021-03-24 01:49:29 +01:00
key = { String ( modeName ) }
2020-08-05 19:06:24 +02:00
icon = { modeName }
2021-07-23 05:58:15 +02:00
iconSize = { 18 }
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 = { ( ) => {
2021-03-22 23:12:59 +01:00
// $FlowFixMe
2020-08-05 19:06:24 +02:00
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 } ) } >
2021-12-29 23:10:19 +01:00
{ showSchedulingOptions && < Card className = { 'card--enable-overflow' } body = { < PublishStreamReleaseDate / > } / > }
2021-12-16 22:59:13 +01:00
2021-04-14 17:56:45 +02:00
{ mode !== PUBLISH _MODES . POST && < PublishDescription disabled = { formDisabled } / > }
2021-12-16 22:59:13 +01:00
2021-04-14 06:06:11 +02:00
< Card actions = { < SelectThumbnail livestreamdData = { livestreamData } / > } / >
2021-12-16 22:59:13 +01:00
2022-02-14 12:21:35 +01:00
< label style = { { marginTop : 'var(--spacing-l)' } } > { _ _ ( 'Tags' ) } < / label >
2020-04-01 20:43:50 +02:00
< TagsSelect
2021-03-30 01:05:18 +02:00
suggestMature = { ! SIMPLE _SITE }
2020-04-01 20:43:50 +02:00
disableAutoFocus
hideHeader
label = { _ _ ( 'Selected Tags' ) }
empty = { _ _ ( 'No tags added' ) }
limitSelect = { TAGS _LIMIT }
help = { _ _ (
2021-03-25 08:06:20 +01:00
"Add tags that are relevant to your content so those who're looking for it can find it more easily. If your content is best suited for mature audiences, ensure it is tagged 'mature'."
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 } / >
2021-04-14 06:06:11 +02:00
{ ! isLivestreamMode && < PublishPrice disabled = { formDisabled } / > }
2021-12-16 22:59:13 +01:00
2021-12-29 23:10:19 +01:00
< PublishAdditionalOptions disabled = { formDisabled } showSchedulingOptions = { showSchedulingOptions } / >
2020-03-24 18:57:17 +01:00
< / div >
) }
< section >
2021-04-23 21:59:48 +02:00
< div className = "section__actions" >
2020-03-24 18:57:17 +01:00
< 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 = {
2021-08-18 18:34:24 +02:00
isClaimingInitialRewards ||
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
/ >
2021-06-13 09:12:28 +02:00
< Button button = "link" onClick = { clearPublish } label = { _ _ ( 'New --[clears Publish Form]--' ) } / >
2020-03-24 18:57:17 +01:00
< / div >
< p className = "help" >
2020-07-08 00:24:24 +02:00
{ ! formDisabled && ! formValid ? (
2022-02-01 23:02:30 +01:00
< PublishFormErrors mode = { mode } waitForFile = { waitingForFile } overMaxBitrate = { overMaxBitrate } / >
2020-07-08 00:24:24 +02:00
) : (
< I18nMessage
tokens = { {
lbry _terms _of _service : (
< Button
button = "link"
2021-10-08 21:22:03 +02:00
href = "https://odysee.com/$/tos"
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 ;