2020-07-23 06:42:22 +03:00
import * as React from "react" ;
import * as Constants from "~/common/constants" ;
import * as Strings from "~/common/strings" ;
import { css } from "@emotion/react" ;
import { DescriptionGroup } from "~/components/system/components/fragments/DescriptionGroup" ;
import { SelectMenu } from "~/components/system/components/SelectMenus" ;
import { Toggle } from "~/components/system/components/Toggle" ;
import { Input } from "~/components/system/components/Input" ;
import { CheckBox } from "~/components/system/components/CheckBox" ;
import { ButtonPrimary } from "~/components/system/components/Buttons" ;
import { CardTabGroup } from "~/components/system/components/CardTabGroup" ;
const STYLES _GROUP = css `
display : flex ;
align - items : center ;
justify - content : space - between ;
width : 100 % ;
overflow - wrap : break - word ;
white - space : pre - wrap ;
max - width : 768 px ;
` ;
const STYLES _SUBGROUP = css `
padding - left : 24 px ;
width : 100 % ;
overflow - wrap : break - word ;
white - space : pre - wrap ;
` ;
const STYLES _LEFT = css `
padding : 12 px 0 0 0 ;
min - width : 10 % ;
overflow - wrap : break - word ;
white - space : pre - wrap ;
` ;
const STYLES _RIGHT = css `
padding - left : 48 px ;
padding - top : 24 px ;
flex - shrink : 0 ;
` ;
const TAB _GROUP = [
{ value : "general" , label : "General" } ,
{ value : "hot" , label : "Hot Storage" } ,
2020-07-25 07:07:37 +03:00
{ value : "cold" , label : "Cold Storage" } ,
2020-07-23 06:42:22 +03:00
] ;
export class FilecoinSettings extends React . Component {
state = {
tabGroup : "general" ,
2020-07-25 07:07:37 +03:00
settings _deals _auto _approve : this . props . autoApprove ,
addrsList : [ ] ,
fetchedAddrs : false ,
fetchedConfig : false ,
} ;
2020-07-25 22:39:51 +03:00
componentDidMount = ( ) => {
let newState = null ;
if ( this . props . defaultStorageConfig ) {
newState = this . unbundleConfig ( ) ;
}
if ( this . props . addrsList ) {
if ( newState ) {
newState . addrsList = this . props . addrsList ;
newState . fetchedAddrs = true ;
} else {
newState = { addrsList : this . props . addrsList , fetchedAddrs : true } ;
}
}
if ( newState ) {
this . setState ( newState ) ;
}
} ;
2020-07-25 07:07:37 +03:00
componentDidUpdate = ( prevProps ) => {
2020-07-25 22:39:51 +03:00
if ( ! this . state . fetchedAddrs || ! this . state . fetchedConfig ) {
2020-07-25 07:07:37 +03:00
let newState = null ;
if ( this . props . defaultStorageConfig != prevProps . defaultStorageConfig ) {
2020-07-25 22:39:51 +03:00
newState = this . unbundleConfig ( ) ;
}
if ( this . props . addrsList != prevProps . addrsList ) {
if ( newState ) {
newState . addrsList = this . props . addrsList ;
newState . fetchedAddrs = true ;
} else {
newState = { addrsList : this . props . addrsList , fetchedAddrs : true } ;
2020-07-25 07:07:37 +03:00
}
}
if ( newState ) {
this . setState ( newState ) ;
}
}
2020-07-23 06:42:22 +03:00
} ;
2020-07-25 22:39:51 +03:00
unbundleConfig = ( ) => {
let config = {
settings _hot _enabled : this . props . defaultStorageConfig . hot . enabled ,
settings _hot _allow _unfreeze : this . props . defaultStorageConfig . hot
. allowUnfreeze ,
settings _hot _ipfs _add _timeout : this . props . defaultStorageConfig . hot . ipfs
. addTimeout ,
settings _cold _enabled : this . props . defaultStorageConfig . cold . enabled ,
settings _cold _default _address : this . props . defaultStorageConfig . cold
. filecoin . addr ,
settings _cold _default _duration : this . props . defaultStorageConfig . cold
. filecoin . dealMinDuration ,
settings _cold _default _replication _factor : this . props . defaultStorageConfig
. cold . filecoin . repFactor ,
settings _cold _default _excluded _miners : this . props . defaultStorageConfig
. cold . filecoin . excludedMinersList ,
settings _cold _default _trusted _miners : this . props . defaultStorageConfig . cold
. filecoin . trustedMinersList ,
settings _cold _country _codes _list : this . props . defaultStorageConfig . cold
. filecoin . countryCodesList ,
settings _cold _default _max _price : this . props . defaultStorageConfig . cold
. filecoin . maxPrice ,
settings _cold _default _auto _renew : this . props . defaultStorageConfig . cold
. filecoin . renew . enabled ,
settings _cold _default _auto _renew _threshold : this . props
. defaultStorageConfig . cold . filecoin . renew . threshold ,
settings _repairable : this . props . defaultStorageConfig . repairable ,
fetchedConfig : true ,
} ;
return config ;
} ;
2020-07-23 06:42:22 +03:00
_handleSave = async ( ) => {
this . props . onSave ( {
data : {
settings _deals _auto _approve : this . state . settings _deals _auto _approve ,
} ,
2020-07-25 07:07:37 +03:00
storageConfig : {
2020-07-23 06:42:22 +03:00
cold : {
enabled : this . state . settings _cold _enabled ,
filecoin : {
addr : this . state . settings _cold _default _address ,
2020-07-25 07:07:37 +03:00
countryCodesList : this . state . settings _cold _country _codes _list ,
2020-07-23 06:42:22 +03:00
dealMinDuration : this . state . settings _cold _default _duration ,
excludedMinersList : this . state
. settings _cold _default _excluded _miners ,
maxPrice : this . state . settings _cold _default _max _price ,
renew : {
enabled : this . state . settings _cold _default _auto _renew ,
2020-07-25 07:07:37 +03:00
threshold : this . state . settings _cold _default _auto _renew _threshold ,
2020-07-23 06:42:22 +03:00
} ,
2020-07-25 07:07:37 +03:00
repFactor : this . state . settings _cold _default _replication _factor ,
trustedMinersList : this . state . settings _cold _default _trusted _miners ,
} ,
} ,
hot : {
enabled : this . state . settings _hot _enabled ,
allowUnfreeze : this . state . settings _hot _allow _unfreeze ,
ipfs : {
addTimeout : this . state . settings _hot _ipfs _add _timeout ,
2020-07-23 06:42:22 +03:00
} ,
} ,
repairable : this . state . settings _repairable ,
} ,
} ) ;
} ;
_handleChange = ( e ) => {
this . setState ( { [ e . target . name ] : e . target . value } ) ;
} ;
render ( ) {
2020-07-25 07:07:37 +03:00
let addrsList = this . state . addrsList . map ( ( each ) => {
return {
value : each . addr ,
name : each . name ,
} ;
} ) ;
2020-07-23 06:42:22 +03:00
return (
< div >
< CardTabGroup
name = "tabGroup"
options = { TAB _GROUP }
value = { this . state . tabGroup }
onChange = { this . _handleChange }
/ >
< div style = { { padding : "16px" } } >
{ this . state . tabGroup === "general" ? (
< div >
< div css = { STYLES _GROUP } >
< div css = { STYLES _LEFT } >
< DescriptionGroup
label = "Automatically approve deals"
tooltip = "If you do not have enough Filecoin you will receive a warning, regardless of whether this is enabled."
description = "When enabled, every storage deal will be automatically approved without asking for confirmation."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_deals_auto_approve"
onChange = { this . _handleChange }
active = { this . state . settings _deals _auto _approve }
/ >
< / d i v >
< / d i v >
2020-07-25 07:07:37 +03:00
< div css = { STYLES _GROUP } >
< div css = { STYLES _LEFT } >
< DescriptionGroup
style = { { marginTop : 24 } }
label = "Repairable"
description = "If this is enabled and the network detects that a miner is no longer storing your file, it will automatically make a storage deal with a new miner to store the file."
tooltip = "Placeholder."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_repairable"
onChange = { this . _handleChange }
active = { this . state . settings _repairable }
/ >
< / d i v >
2020-07-23 06:42:22 +03:00
< / d i v >
< / d i v >
) : this . state . tabGroup === "cold" ? (
< div >
2020-07-25 07:07:37 +03:00
< DescriptionGroup
full
style = { { marginTop : 24 } }
label = "What is cold storage?"
description = " Cold storage is storage on the Filecoin network . Think of it as
storing your files in a long term safety deposit vault , where
you have proof that they are being stored and can know they are
secure . However , cold storage is slower to retrieve from and costs
money . "
/ >
< DescriptionGroup
full
description = " Even if both cold and hot storage are enabled , Slate will try
and save you money by retrieving from hot storage when it can . "
/ >
< div css = { STYLES _GROUP } style = { { marginTop : 24 } } >
2020-07-23 06:42:22 +03:00
< div css = { STYLES _LEFT } >
< DescriptionGroup
label = "Enable cold storage"
tooltip = "Placeholder"
description = "By enabling cold storage, every time you make a deal your data will be stored on the Filecoin Network."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_cold_enabled"
onChange = { this . _handleChange }
active = { this . state . settings _cold _enabled }
/ >
< / d i v >
< / d i v >
{ this . state . settings _cold _enabled ? (
< div >
< SelectMenu
full
containerStyle = { { marginTop : 24 } }
label = "Default Filecoin address"
description = "Deal payments will default to this address."
tooltip = "You will always have the option to choose a different wallet before you make a deal."
name = "settings_cold_default_address"
value = { this . state . settings _cold _default _address }
category = "address"
onChange = { this . _handleChange }
2020-07-25 07:07:37 +03:00
options = { addrsList }
2020-07-23 06:42:22 +03:00
/ >
< Input
full
containerStyle = { { marginTop : 24 } }
label = "Default deal duration"
2020-07-25 07:07:37 +03:00
description = "How long you would like your files stored for, before the deal must be renewed or the file will be removed from the Filecoin network."
tooltip = "Duration in epochs (~25 seconds)."
2020-07-23 06:42:22 +03:00
name = "settings_cold_default_duration"
2020-07-25 22:39:51 +03:00
unit = "epochs"
2020-07-28 02:56:03 +03:00
pattern = "^\d*$"
2020-07-23 06:42:22 +03:00
value = { this . state . settings _cold _default _duration }
onChange = { this . _handleChange }
/ >
< Input
full
containerStyle = { { marginTop : 24 } }
label = "Default replication factor"
description = "The number of miners that each file will be stored with."
tooltip = "A higher replication factor means your files are more secure against loss, but also costs more."
name = "settings_cold_default_replication_factor"
value = { this . state . settings _cold _default _replication _factor }
2020-07-25 22:39:51 +03:00
unit = "miners"
2020-07-28 02:56:03 +03:00
pattern = "^\d*$"
2020-07-23 06:42:22 +03:00
onChange = { this . _handleChange }
/ >
< Input
full
containerStyle = { { marginTop : 24 } }
label = "Max Filecoin price"
2020-07-25 07:07:37 +03:00
description = "The maximum price in Filecoin you're willing to pay to store 1 GB for 1 Epoch (~25 seconds)."
2020-07-23 06:42:22 +03:00
tooltip = "Slate will always try to find you the best price, regardless of how high you set this."
name = "settings_cold_default_max_price"
2020-07-28 02:56:03 +03:00
pattern = "^\d*$"
2020-07-23 06:42:22 +03:00
value = { this . state . settings _cold _default _max _price }
2020-07-25 22:39:51 +03:00
unit = "FIL/GB/epoch"
2020-07-23 06:42:22 +03:00
onChange = { this . _handleChange }
/ >
2020-07-25 07:07:37 +03:00
< div css = { STYLES _GROUP } >
< div css = { STYLES _LEFT } >
< DescriptionGroup
style = { { marginTop : 24 } }
label = "Auto renew deals"
description = "If enabled, your storage deal will be automatically renewed (and your wallet charged) once the deal duration is up. This ensures your files will remain stored even if you do not manually renew."
tooltip = "This does not protect your files in the event that your wallet lacks sufficient funds or if there are no miners willing to store it for less than your max auto renew price."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_cold_default_auto_renew"
onChange = { this . _handleChange }
active = { this . state . settings _cold _default _auto _renew }
/ >
< / d i v >
< / d i v >
2020-07-23 06:42:22 +03:00
2020-07-25 07:07:37 +03:00
{ this . state . settings _cold _default _auto _renew ? (
< div css = { STYLES _SUBGROUP } >
< Input
full
containerStyle = { { marginTop : 24 } }
label = "Auto renew threshold"
description = "How long before a deal expires should it auto renew."
tooltip = "Placeholder."
name = "settings_cold_default_auto_renew_threshold"
2020-07-28 02:56:03 +03:00
pattern = "^\d*$"
2020-07-25 07:07:37 +03:00
value = {
this . state . settings _cold _default _auto _renew _threshold
}
2020-07-25 22:39:51 +03:00
unit = "[unit]"
2020-07-25 07:07:37 +03:00
onChange = { this . _handleChange }
/ >
< / d i v >
) : null }
2020-07-23 06:42:22 +03:00
< / d i v >
) : null }
< / d i v >
) : (
< div >
2020-07-25 07:07:37 +03:00
< DescriptionGroup
full
style = { { marginTop : 24 } }
label = "What is hot storage?"
description = "Hot storage is storage on IPFS. Retrieval from IPFS is faster and is free for Slate users. It's a good everyday solution, but for more secure storage, cold storage can be a better option."
/ >
< div css = { STYLES _GROUP } style = { { marginTop : 24 } } >
2020-07-23 06:42:22 +03:00
< div css = { STYLES _LEFT } >
< DescriptionGroup
label = "Enable hot storage"
tooltip = "Placeholder"
description = "By enabling hot storage, every time you make a deal your data will be stored on IPFS."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_hot_enabled"
onChange = { this . _handleChange }
active = { this . state . settings _hot _enabled }
/ >
< / d i v >
< / d i v >
{ this . state . settings _hot _enabled ? (
< div >
2020-07-25 07:07:37 +03:00
< div css = { STYLES _GROUP } >
< div css = { STYLES _LEFT } >
< DescriptionGroup
style = { { marginTop : 24 } }
label = "Allow Unfreeze"
description = "If a file is in cold storage on the Filcoin network but not in hot storage on IPFS, a retrieval deal must be made in order to move it to hot storage. In order to allow this, allow unfreeze must be enabled."
tooltip = "This is applicable mainly in cases where hot storage was previously disabled then later enabled."
/ >
< / d i v >
< div css = { STYLES _RIGHT } >
< Toggle
name = "settings_hot_allow_unfreeze"
onChange = { this . _handleChange }
active = { this . state . settings _hot _allow _unfreeze }
/ >
< / d i v >
< / d i v >
2020-07-23 06:42:22 +03:00
2020-07-25 07:07:37 +03:00
{ this . state . settings _hot _allow _unfreeze ? (
< div css = { STYLES _SUBGROUP } >
< Input
full
containerStyle = { { marginTop : 24 } }
label = "Add timeout"
2020-07-28 02:56:03 +03:00
pattern = "^\d*$"
2020-07-25 07:07:37 +03:00
description = "How many seconds Slate will search for a file in hot storage before executing an unfreeze retrieval deal to get it from cold storage."
tooltip = "Placeholder."
name = "settings_hot_ipfs_add_timeout"
value = { this . state . settings _hot _ipfs _add _timeout }
2020-07-25 22:39:51 +03:00
unit = "seconds"
2020-07-25 07:07:37 +03:00
onChange = { this . _handleChange }
/ >
< / d i v >
) : null }
2020-07-23 06:42:22 +03:00
< / d i v >
) : null }
< / d i v >
) }
< div style = { { marginTop : 32 } } >
< ButtonPrimary onClick = { this . _handleSave } > Save < / B u t t o n P r i m a r y >
< / d i v >
< / d i v >
< / d i v >
) ;
}
}