11import { FC , useState , useEffect } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
33import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
4- import { Button , FormTextarea } from 'components' ;
4+ import { Button , FormTextarea , Switch } from 'components' ;
55import { ButtonAppearanceTypes , ToastTypes } from 'enums/commonEnums' ;
66import CircularSpinner from 'components/molecules/CircularSpinner/CircularSpinner' ;
7- import { getPromptConfiguration , savePromptConfiguration } from 'services/promptConfiguration' ;
7+ import { getPromptConfiguration , savePromptConfiguration , disablePromptConfiguration } from 'services/promptConfiguration' ;
88import { promptConfigurationQueryKeys } from 'utils/queryKeys' ;
99import { useToast } from 'hooks/useToast' ;
1010import './PromptConfigurations.scss' ;
@@ -15,6 +15,7 @@ const PromptConfigurations: FC = () => {
1515 const queryClient = useQueryClient ( ) ;
1616 const [ promptText , setPromptText ] = useState ( '' ) ;
1717 const [ isUpdating , setIsUpdating ] = useState ( false ) ;
18+ const [ isEnabled , setIsEnabled ] = useState ( false ) ;
1819
1920 // Fetch prompt configuration
2021 const { data : promptConfig , isLoading } = useQuery ( {
@@ -25,9 +26,14 @@ const PromptConfigurations: FC = () => {
2526
2627 // Update promptText when data is loaded
2728 useEffect ( ( ) => {
28- if ( promptConfig && promptConfig . length > 0 ) {
29- setPromptText ( promptConfig [ 0 ] . prompt || '' ) ;
29+ if ( promptConfig && promptConfig . length > 0 && promptConfig [ 0 ] . prompt ) {
30+ setPromptText ( promptConfig [ 0 ] . prompt ) ;
3031 setIsUpdating ( true ) ;
32+ setIsEnabled ( true ) ;
33+ } else {
34+ setPromptText ( '' ) ;
35+ setIsUpdating ( promptConfig !== undefined && promptConfig . length > 0 ) ;
36+ setIsEnabled ( false ) ;
3137 }
3238 } , [ promptConfig ] ) ;
3339
@@ -52,13 +58,47 @@ const PromptConfigurations: FC = () => {
5258 } ,
5359 } ) ;
5460
61+ // Disable prompt mutation (saves empty prompt)
62+ const disableMutation = useMutation ( {
63+ mutationFn : disablePromptConfiguration ,
64+ onSuccess : ( ) => {
65+ queryClient . invalidateQueries ( { queryKey : promptConfigurationQueryKeys . current ( ) } ) ;
66+ setPromptText ( '' ) ;
67+ setIsEnabled ( false ) ;
68+ toast . open ( {
69+ type : ToastTypes . SUCCESS ,
70+ title : t ( 'toast.success.title' ) ,
71+ message : t ( 'promptConfigurations.deleteSuccess' ) ,
72+ } ) ;
73+ } ,
74+ onError : ( error : any ) => {
75+ console . error ( 'Error disabling prompt:' , error ) ;
76+ toast . open ( {
77+ type : ToastTypes . ERROR ,
78+ title : t ( 'toast.error.title' ) ,
79+ message : t ( 'promptConfigurations.deleteError' ) ,
80+ } ) ;
81+ } ,
82+ } ) ;
83+
5584 const handleSubmit = ( ) => {
5685 if ( ! promptText . trim ( ) ) {
5786 return ;
5887 }
5988 saveMutation . mutate ( promptText ) ;
6089 } ;
6190
91+ const handleToggleChange = ( checked : boolean ) => {
92+ if ( ! checked ) {
93+ // Disable: save empty prompt to clear configuration
94+ setPromptText ( '' ) ;
95+ if ( isUpdating ) {
96+ disableMutation . mutate ( ) ;
97+ }
98+ }
99+ setIsEnabled ( checked ) ;
100+ } ;
101+
62102 if ( isLoading ) {
63103 return < CircularSpinner /> ;
64104 }
@@ -71,6 +111,20 @@ const PromptConfigurations: FC = () => {
71111 </ div >
72112
73113 < div className = "prompt-form" >
114+ < div className = "toggle-header" >
115+ < span > { t ( 'promptConfigurations.enableToggleLabel' ) } </ span >
116+ < Switch
117+ label = ""
118+ hideLabel = { true }
119+ checked = { isEnabled }
120+ onCheckedChange = { handleToggleChange }
121+ name = "enableCustomPrompt"
122+ onLabel = ""
123+ offLabel = ""
124+ />
125+ </ div >
126+ < div className = "separator" > </ div >
127+
74128 < FormTextarea
75129 label = { t ( 'promptConfigurations.promptLabel' ) }
76130 name = "promptText"
@@ -84,7 +138,7 @@ const PromptConfigurations: FC = () => {
84138 < Button
85139 appearance = { ButtonAppearanceTypes . PRIMARY }
86140 onClick = { handleSubmit }
87- disabled = { saveMutation . isLoading || ! promptText . trim ( ) }
141+ disabled = { ! isEnabled || saveMutation . isLoading || ! promptText . trim ( ) }
88142 >
89143 { saveMutation . isLoading
90144 ? ( isUpdating ? t ( 'promptConfigurations.updating' ) : t ( 'promptConfigurations.saving' ) )
@@ -98,4 +152,4 @@ const PromptConfigurations: FC = () => {
98152 ) ;
99153} ;
100154
101- export default PromptConfigurations ;
155+ export default PromptConfigurations ;
0 commit comments