2023-04-06 06:07:16 -07:00
import { useEffect , useState } from 'react' ;
2023-04-02 18:45:41 -04:00
import TextareaAutosize from 'react-textarea-autosize' ;
import { Label } from '~/components/ui/Label.tsx' ;
import { Checkbox } from '~/components/ui/Checkbox.tsx' ;
2023-04-06 05:47:37 -07:00
import SelectDropDown from '../../ui/SelectDropDown' ;
2023-04-02 18:45:41 -04:00
import { cn } from '~/utils/' ;
2023-04-06 08:55:25 -07:00
import useDebounce from '~/hooks/useDebounce' ;
import { useUpdateTokenCountMutation } from '~/data-provider' ;
2023-04-02 18:45:41 -04:00
const defaultTextProps =
'rounded-md border border-gray-200 focus:border-slate-400 focus:bg-gray-50 bg-transparent text-sm shadow-[0_0_10px_rgba(0,0,0,0.05)] outline-none placeholder:text-gray-400 focus:outline-none focus:ring-gray-400 focus:ring-opacity-20 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-500 dark:bg-gray-700 focus:dark:bg-gray-600 dark:text-gray-50 dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] dark:focus:border-gray-400 dark:focus:outline-none dark:focus:ring-0 dark:focus:ring-gray-400 dark:focus:ring-offset-0' ;
function Settings ( props ) {
2023-04-05 02:30:14 +08:00
const { readonly , context , systemMessage , jailbreak , toneStyle , setOption } = props ;
2023-04-04 10:04:21 -04:00
const [ tokenCount , setTokenCount ] = useState ( 0 ) ;
2023-04-04 01:04:33 +08:00
const showSystemMessage = jailbreak ;
2023-04-03 21:18:19 -04:00
const setContext = setOption ( 'context' ) ;
const setSystemMessage = setOption ( 'systemMessage' ) ;
const setJailbreak = setOption ( 'jailbreak' ) ;
2023-04-05 02:30:14 +08:00
const setToneStyle = value => setOption ( 'toneStyle' ) ( value . toLowerCase ( ) ) ;
2023-04-06 08:55:25 -07:00
const debouncedContext = useDebounce ( context , 250 ) ;
const updateTokenCountMutation = useUpdateTokenCountMutation ( ) ;
2023-04-03 21:18:19 -04:00
2023-04-06 08:55:25 -07:00
useEffect ( ( ) => {
if ( ! debouncedContext || debouncedContext . trim ( ) === '' ) {
2023-04-04 10:04:21 -04:00
setTokenCount ( 0 ) ;
return ;
}
const handleTextChange = context => {
2023-04-06 08:55:25 -07:00
updateTokenCountMutation . mutate ( { text : context } , {
onSuccess : data => {
2023-04-04 10:04:21 -04:00
setTokenCount ( data . count ) ;
}
} ) ;
} ;
2023-04-06 08:55:25 -07:00
handleTextChange ( debouncedContext ) ;
} , [ debouncedContext ] ) ;
2023-04-04 10:04:21 -04:00
2023-04-02 18:45:41 -04:00
return (
< >
< div className = "grid gap-6 sm:grid-cols-2" >
< div className = "col-span-1 flex flex-col items-center justify-start gap-6" >
< div className = "grid w-full items-center gap-2" >
2023-04-05 03:07:46 +08:00
< Label
htmlFor = "toneStyle-dropdown"
className = "text-left text-sm font-medium"
>
Tone Style < small className = "opacity-40" > ( default : fast ) < / small >
< / Label >
2023-04-06 05:47:37 -07:00
< SelectDropDown
2023-04-03 21:18:19 -04:00
id = "toneStyle-dropdown"
2023-04-05 03:07:46 +08:00
title = { null }
2023-04-04 10:04:21 -04:00
value = { ` ${ toneStyle . charAt ( 0 ) . toUpperCase ( ) } ${ toneStyle . slice ( 1 ) } ` }
2023-04-05 03:07:46 +08:00
setValue = { setToneStyle }
availableValues = { [ 'Creative' , 'Fast' , 'Balanced' , 'Precise' ] }
disabled = { readonly }
2023-04-03 21:18:19 -04:00
className = { cn (
defaultTextProps ,
2023-04-05 03:07:46 +08:00
'flex w-full resize-none focus:outline-none focus:ring-0 focus:ring-opacity-0 focus:ring-offset-0'
2023-04-03 21:18:19 -04:00
) }
containerClassName = "flex w-full resize-none"
/ >
2023-04-05 03:07:46 +08:00
< / div >
< div className = "grid w-full items-center gap-2" >
2023-04-02 18:45:41 -04:00
< Label
htmlFor = "context"
className = "text-left text-sm font-medium"
>
Context < small className = "opacity-40" > ( default : blank ) < / small >
< / Label >
< TextareaAutosize
id = "context"
2023-04-04 01:12:14 +08:00
disabled = { readonly }
2023-04-02 18:45:41 -04:00
value = { context || '' }
onChange = { e => setContext ( e . target . value || null ) }
2023-04-04 12:53:41 -04:00
placeholder = "Bing can use up to 7k tokens for 'context', which it can reference for the conversation. The specific limit is not known but may run into errors exceeding 7k tokens"
2023-04-02 18:45:41 -04:00
className = { cn (
defaultTextProps ,
2023-04-03 21:18:19 -04:00
'flex max-h-[300px] min-h-[100px] w-full resize-none px-3 py-2'
2023-04-02 18:45:41 -04:00
) }
/ >
2023-04-05 02:30:14 +08:00
< small className = "mb-5 text-black dark:text-white" > { ` Token count: ${ tokenCount } ` } < / small >
2023-04-02 18:45:41 -04:00
< / div >
< / div >
< div className = "col-span-1 flex flex-col items-center justify-start gap-6" >
< div className = "grid w-full items-center gap-2" >
2023-04-05 03:07:46 +08:00
< Label
htmlFor = "jailbreak"
className = "text-left text-sm font-medium"
>
Enable Sydney < small className = "opacity-40" > ( default : false ) < / small >
< / Label >
< div className = "flex h-[40px] w-full items-center space-x-3" >
2023-04-02 18:45:41 -04:00
< Checkbox
id = "jailbreak"
2023-04-04 01:12:14 +08:00
disabled = { readonly }
2023-04-04 01:04:33 +08:00
checked = { jailbreak }
2023-04-03 21:18:19 -04:00
className = "focus:ring-opacity-20 dark:border-gray-500 dark:bg-gray-700 dark:text-gray-50 dark:focus:ring-gray-600 dark:focus:ring-opacity-50 dark:focus:ring-offset-0"
2023-04-04 10:04:21 -04:00
onCheckedChange = { setJailbreak }
2023-04-02 18:45:41 -04:00
/ >
< label
htmlFor = "jailbreak"
className = "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70 dark:text-gray-50"
>
2023-04-05 03:07:46 +08:00
Jailbreak < small > To enable Sydney < / small >
2023-04-02 18:45:41 -04:00
< / label >
2023-04-05 03:07:46 +08:00
< / div >
< / div >
{ showSystemMessage && (
< div className = "grid w-full items-center gap-2" >
2023-04-04 01:04:33 +08:00
< Label
htmlFor = "systemMessage"
2023-04-05 03:07:46 +08:00
className = "text-left text-sm font-medium"
2023-04-04 01:04:33 +08:00
style = { { opacity : showSystemMessage ? '1' : '0' } }
>
2023-04-03 21:18:19 -04:00
< a
href = "https://github.com/danny-avila/chatgpt-clone/blob/main/client/defaultSystemMessage.md"
target = "_blank"
className = "text-blue-500 transition-colors duration-200 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-500"
>
System Message
< / a > { ' ' }
2023-04-05 03:07:46 +08:00
< small className = "opacity-40 dark:text-gray-50" > ( default : blank ) < / small >
2023-04-04 01:04:33 +08:00
< / Label >
2023-04-05 03:07:46 +08:00
< TextareaAutosize
id = "systemMessage"
disabled = { readonly }
value = { systemMessage || '' }
onChange = { e => setSystemMessage ( e . target . value || null ) }
placeholder = "WARNING: Misuse of this feature can get you BANNED from using Bing! Click on 'System Message' for full instructions and the default message if omitted, which is the 'Sydney' preset that is considered safe."
className = { cn (
defaultTextProps ,
'flex max-h-[300px] min-h-[100px] w-full resize-none px-3 py-2 placeholder:text-red-400'
) }
/ >
2023-04-02 18:45:41 -04:00
< / div >
2023-04-05 03:07:46 +08:00
) }
2023-04-02 18:45:41 -04:00
< / div >
< / div >
< / >
) ;
}
export default Settings ;