2024-02-05 14:11:04 +01:00
import {
Accordion ,
AccordionButton ,
AccordionIcon ,
AccordionItem ,
AccordionPanel ,
FormLabel ,
Stack ,
} from '@chakra-ui/react'
2023-03-03 09:01:11 +01:00
import { CodeEditor } from '@/components/inputs/CodeEditor'
2023-11-08 15:34:16 +01:00
import { FileInputBlock , Variable } from '@typebot.io/schemas'
2023-09-19 15:42:33 +02:00
import { TextInput } from '@/components/inputs'
2023-03-03 09:01:11 +01:00
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'
import { VariableSearchInput } from '@/components/inputs/VariableSearchInput'
2024-01-30 08:02:10 +01:00
import {
defaultFileInputOptions ,
fileVisibilityOptions ,
} from '@typebot.io/schemas/features/blocks/inputs/file/constants'
2023-12-29 08:42:50 -03:00
import { useTranslate } from '@tolgee/react'
2024-01-30 08:02:10 +01:00
import { DropdownList } from '@/components/DropdownList'
2022-06-12 17:34:33 +02:00
type Props = {
2023-11-08 15:34:16 +01:00
options : FileInputBlock [ 'options' ]
onOptionsChange : ( options : FileInputBlock [ 'options' ] ) = > void
2022-06-12 17:34:33 +02:00
}
export const FileInputSettings = ( { options , onOptionsChange } : Props ) = > {
2023-12-29 08:42:50 -03:00
const { t } = useTranslate ( )
2022-06-12 17:34:33 +02:00
const handleButtonLabelChange = ( button : string ) = >
2023-11-08 15:34:16 +01:00
onOptionsChange ( { . . . options , labels : { . . . options ? . labels , button } } )
2023-01-20 08:12:59 +01:00
2022-06-12 17:34:33 +02:00
const handlePlaceholderLabelChange = ( placeholder : string ) = >
2023-11-08 15:34:16 +01:00
onOptionsChange ( { . . . options , labels : { . . . options ? . labels , placeholder } } )
2023-01-20 08:12:59 +01:00
2022-06-25 09:24:47 +02:00
const handleMultipleFilesChange = ( isMultipleAllowed : boolean ) = >
2022-06-12 17:34:33 +02:00
onOptionsChange ( { . . . options , isMultipleAllowed } )
2023-01-20 08:12:59 +01:00
2022-06-12 17:34:33 +02:00
const handleVariableChange = ( variable? : Variable ) = >
onOptionsChange ( { . . . options , variableId : variable?.id } )
2023-01-20 08:12:59 +01:00
2022-06-25 09:24:47 +02:00
const handleRequiredChange = ( isRequired : boolean ) = >
onOptionsChange ( { . . . options , isRequired } )
2023-01-20 08:12:59 +01:00
const updateClearButtonLabel = ( clear : string ) = >
2023-11-08 15:34:16 +01:00
onOptionsChange ( { . . . options , labels : { . . . options ? . labels , clear } } )
2023-01-20 08:12:59 +01:00
const updateSkipButtonLabel = ( skip : string ) = >
2023-11-08 15:34:16 +01:00
onOptionsChange ( { . . . options , labels : { . . . options ? . labels , skip } } )
2023-01-20 08:12:59 +01:00
2024-01-30 08:02:10 +01:00
const updateVisibility = (
visibility : ( typeof fileVisibilityOptions ) [ number ]
) = > onOptionsChange ( { . . . options , visibility } )
2024-02-05 14:11:04 +01:00
const updateSingleFileSuccessLabel = ( single : string ) = >
onOptionsChange ( {
. . . options ,
labels : {
. . . options ? . labels ,
success : { . . . options ? . labels ? . success , single } ,
} ,
} )
const updateMultipleFilesSuccessLabel = ( multiple : string ) = >
onOptionsChange ( {
. . . options ,
labels : {
. . . options ? . labels ,
success : { . . . options ? . labels ? . success , multiple } ,
} ,
} )
2022-06-12 17:34:33 +02:00
return (
< Stack spacing = { 4 } >
2022-06-25 09:24:47 +02:00
< SwitchWithLabel
2023-12-29 08:42:50 -03:00
label = { t ( 'blocks.inputs.file.settings.required.label' ) }
2023-11-08 15:34:16 +01:00
initialValue = { options ? . isRequired ? ? defaultFileInputOptions . isRequired }
2022-06-25 09:24:47 +02:00
onCheckChange = { handleRequiredChange }
/ >
2022-06-12 17:34:33 +02:00
< SwitchWithLabel
2023-12-29 08:42:50 -03:00
label = { t ( 'blocks.inputs.file.settings.allowMultiple.label' ) }
2023-11-08 15:34:16 +01:00
initialValue = {
options ? . isMultipleAllowed ? ?
defaultFileInputOptions . isMultipleAllowed
}
2022-06-25 09:24:47 +02:00
onCheckChange = { handleMultipleFilesChange }
2022-06-12 17:34:33 +02:00
/ >
< Stack >
2023-12-29 08:42:50 -03:00
< FormLabel mb = "0" >
{ t ( 'blocks.inputs.settings.placeholder.label' ) }
< / FormLabel >
2022-06-12 17:34:33 +02:00
< CodeEditor
lang = "html"
onChange = { handlePlaceholderLabelChange }
2023-11-08 15:34:16 +01:00
defaultValue = {
options ? . labels ? . placeholder ? ?
defaultFileInputOptions . labels . placeholder
}
2022-06-12 17:34:33 +02:00
height = { '100px' }
withVariableButton = { false }
/ >
< / Stack >
2024-02-05 14:11:04 +01:00
< Accordion allowToggle >
< AccordionItem >
< AccordionButton justifyContent = "space-between" >
Labels
< AccordionIcon / >
< / AccordionButton >
< AccordionPanel as = { Stack } spacing = { 4 } >
< TextInput
label = { t ( 'blocks.inputs.settings.button.label' ) }
defaultValue = {
options ? . labels ? . button ? ? defaultFileInputOptions . labels . button
}
onChange = { handleButtonLabelChange }
withVariableButton = { false }
/ >
{ options ? . isMultipleAllowed && (
< TextInput
label = { t ( 'blocks.inputs.file.settings.clear.label' ) }
defaultValue = {
options ? . labels ? . clear ? ? defaultFileInputOptions . labels . clear
}
onChange = { updateClearButtonLabel }
withVariableButton = { false }
/ >
) }
{ ! ( options ? . isRequired ? ? defaultFileInputOptions . isRequired ) && (
< TextInput
label = { t ( 'blocks.inputs.file.settings.skip.label' ) }
defaultValue = {
options ? . labels ? . skip ? ? defaultFileInputOptions . labels . skip
}
onChange = { updateSkipButtonLabel }
withVariableButton = { false }
/ >
) }
< TextInput
label = "Single file success"
defaultValue = {
options ? . labels ? . success ? . single ? ?
defaultFileInputOptions . labels . success . single
}
onChange = { updateSingleFileSuccessLabel }
withVariableButton = { false }
/ >
{ options ? . isMultipleAllowed && (
< TextInput
label = "Multi files success"
moreInfoTooltip = "Include {total} to show the total number of files uploaded"
defaultValue = {
options ? . labels ? . success ? . multiple ? ?
defaultFileInputOptions . labels . success . multiple
}
onChange = { updateMultipleFilesSuccessLabel }
withVariableButton = { false }
/ >
) }
< / AccordionPanel >
< / AccordionItem >
< / Accordion >
2024-01-30 08:02:10 +01:00
< DropdownList
label = "Visibility:"
moreInfoTooltip = 'This setting determines who can see the uploaded files. "Public" means that anyone who has the link can see the files. "Private" means that only a members of this workspace can see the files.'
2024-02-21 18:04:18 +01:00
currentItem = { options ? . visibility ? ? defaultFileInputOptions . visibility }
2024-01-30 08:02:10 +01:00
onItemSelect = { updateVisibility }
items = { fileVisibilityOptions }
/ >
2022-06-12 17:34:33 +02:00
< Stack >
< FormLabel mb = "0" htmlFor = "variable" >
2023-12-29 08:42:50 -03:00
{ options ? . isMultipleAllowed
? t ( 'blocks.inputs.file.settings.saveMultipleUpload.label' )
: t ( 'blocks.inputs.file.settings.saveSingleUpload.label' ) }
2022-06-12 17:34:33 +02:00
< / FormLabel >
< VariableSearchInput
2023-11-08 15:34:16 +01:00
initialVariableId = { options ? . variableId }
2022-06-12 17:34:33 +02:00
onSelectVariable = { handleVariableChange }
/ >
< / Stack >
< / Stack >
)
}