2021-01-28 12:08:57 +01:00
import React , { useState , useContext , useEffect } from 'react' ;
2021-01-31 09:48:39 +01:00
import { Button , Tooltip } from 'antd' ;
2021-01-31 04:25:44 +01:00
import { CopyOutlined , RedoOutlined } from '@ant-design/icons' ;
2021-01-31 09:58:27 +01:00
import { TEXTFIELD_TYPE_NUMBER , TEXTFIELD_TYPE_PASSWORD } from './form-textfield' ;
2021-01-31 09:48:39 +01:00
import TextFieldWithSubmit from './form-textfield-with-submit' ;
2021-01-28 12:08:57 +01:00
import { ServerStatusContext } from '../../../utils/server-status-context' ;
2021-02-04 08:24:12 +01:00
import { AlertMessageContext } from '../../../utils/alert-message-context' ;
2021-01-31 10:38:20 +01:00
import {
TEXTFIELD_PROPS_FFMPEG ,
TEXTFIELD_PROPS_RTMP_PORT ,
TEXTFIELD_PROPS_STREAM_KEY ,
TEXTFIELD_PROPS_WEB_PORT ,
} from './constants' ;
2021-01-28 12:08:57 +01:00
2021-01-30 10:39:58 +01:00
import { UpdateArgs } from '../../../types/config-section' ;
2021-01-28 12:08:57 +01:00
export default function EditInstanceDetails() {
const [ formDataValues , setFormDataValues ] = useState ( null ) ;
const serverStatusData = useContext ( ServerStatusContext ) ;
2021-02-04 08:24:12 +01:00
const { setMessage } = useContext ( AlertMessageContext ) ;
2021-01-28 12:08:57 +01:00
const { serverConfig } = serverStatusData || { } ;
const { streamKey , ffmpegPath , rtmpServerPort , webServerPort } = serverConfig ;
2021-01-31 10:38:20 +01:00
const [ copyIsVisible , setCopyVisible ] = useState ( false ) ;
2021-01-31 04:25:44 +01:00
const COPY_TOOLTIP_TIMEOUT = 3000 ;
2021-01-28 12:08:57 +01:00
useEffect ( ( ) = > {
setFormDataValues ( {
2021-01-31 10:38:20 +01:00
streamKey ,
ffmpegPath ,
rtmpServerPort ,
webServerPort ,
2021-01-28 12:08:57 +01:00
} ) ;
} , [ serverConfig ] ) ;
if ( ! formDataValues ) {
return null ;
}
2021-01-30 10:39:58 +01:00
const handleFieldChange = ( { fieldName , value } : UpdateArgs ) = > {
2021-01-28 12:08:57 +01:00
setFormDataValues ( {
. . . formDataValues ,
[ fieldName ] : value ,
} ) ;
2021-01-31 10:38:20 +01:00
} ;
2021-01-28 12:08:57 +01:00
2021-02-04 08:24:12 +01:00
const showConfigurationRestartMessage = ( ) = > {
2021-02-04 17:04:00 +01:00
setMessage ( 'Updating server settings requires a restart of your Owncast server.' ) ;
} ;
2021-02-04 08:24:12 +01:00
2021-02-04 21:41:35 +01:00
const showStreamKeyChangeMessage = ( ) = > {
setMessage ( 'Changing your stream key will log you out of the admin and block you from streaming until you change the key in your broadcasting software.' ) ;
} ;
const showFfmpegChangeMessage = ( ) = > {
if ( serverStatusData . online ) {
setMessage ( 'The updated ffmpeg path will be used when starting your next live stream.' ) ;
}
} ;
2021-01-31 10:38:20 +01:00
function generateStreamKey() {
2021-01-31 04:25:44 +01:00
let key = '' ;
2021-01-31 10:38:20 +01:00
for ( let i = 0 ; i < 3 ; i += 1 ) {
2021-01-31 04:25:44 +01:00
key += Math . random ( ) . toString ( 36 ) . substring ( 2 ) ;
}
handleFieldChange ( { fieldName : 'streamKey' , value : key } ) ;
}
2021-01-31 10:38:20 +01:00
function copyStreamKey() {
navigator . clipboard . writeText ( formDataValues . streamKey ) . then ( ( ) = > {
setCopyVisible ( true ) ;
setTimeout ( ( ) = > setCopyVisible ( false ) , COPY_TOOLTIP_TIMEOUT ) ;
} ) ;
2021-01-31 04:25:44 +01:00
}
2021-01-31 10:38:20 +01:00
return (
2021-02-01 08:40:39 +01:00
< div className = "edit-public-details-container" >
2021-02-01 09:36:27 +01:00
< div className = "field-container field-streamkey-container" >
< div className = "left-side" >
< TextFieldWithSubmit
fieldName = "streamKey"
{ . . . TEXTFIELD_PROPS_STREAM_KEY }
value = { formDataValues . streamKey }
initialValue = { streamKey }
type = { TEXTFIELD_TYPE_PASSWORD }
onChange = { handleFieldChange }
2021-02-04 21:41:35 +01:00
onSubmit = { showStreamKeyChangeMessage }
2021-02-01 09:36:27 +01:00
/ >
< div className = "streamkey-actions" >
2021-02-01 23:12:26 +01:00
< Tooltip title = "Generate a stream key" >
< Button icon = { < RedoOutlined / > } size = "small" onClick = { generateStreamKey } / >
< / Tooltip >
< Tooltip
className = "copy-tooltip"
title = { copyIsVisible ? 'Copied!' : 'Copy to clipboard' }
>
2021-02-01 09:36:27 +01:00
< Button icon = { < CopyOutlined / > } size = "small" onClick = { copyStreamKey } / >
< / Tooltip >
< / div >
< / div >
2021-01-28 12:08:57 +01:00
< / div >
2021-02-01 08:40:39 +01:00
< TextFieldWithSubmit
fieldName = "ffmpegPath"
{ . . . TEXTFIELD_PROPS_FFMPEG }
value = { formDataValues . ffmpegPath }
initialValue = { ffmpegPath }
onChange = { handleFieldChange }
2021-02-04 21:41:35 +01:00
onSubmit = { showFfmpegChangeMessage }
2021-02-01 08:40:39 +01:00
/ >
< TextFieldWithSubmit
fieldName = "webServerPort"
{ . . . TEXTFIELD_PROPS_WEB_PORT }
value = { formDataValues . webServerPort }
initialValue = { webServerPort }
type = { TEXTFIELD_TYPE_NUMBER }
onChange = { handleFieldChange }
2021-02-04 08:24:12 +01:00
onSubmit = { showConfigurationRestartMessage }
2021-02-01 08:40:39 +01:00
/ >
< TextFieldWithSubmit
fieldName = "rtmpServerPort"
{ . . . TEXTFIELD_PROPS_RTMP_PORT }
value = { formDataValues . rtmpServerPort }
initialValue = { rtmpServerPort }
type = { TEXTFIELD_TYPE_NUMBER }
onChange = { handleFieldChange }
2021-02-04 08:24:12 +01:00
onSubmit = { showConfigurationRestartMessage }
2021-02-01 08:40:39 +01:00
/ >
2021-01-31 10:38:20 +01:00
< / div >
) ;
2021-01-28 12:08:57 +01:00
}