@@ -85,6 +85,9 @@ const ProfileInfoTabContainer = ({
8585 const [ isDeletingAccount , setIsDeletingAccount ] = useState ( false ) ;
8686 const [ deleteAccountError , setDeleteAccountError ] = useState ( '' ) ;
8787 const isInitialized = useRef ( false ) ;
88+ const savedCustomPromptRef = useRef ( '' ) ;
89+ const [ isPromptDirty , setIsPromptDirty ] = useState ( false ) ;
90+ const [ isSavingPrompt , setIsSavingPrompt ] = useState ( false ) ;
8891
8992 const updateProfileMutation = useMutation ( {
9093 mutationFn : async ( profileData : ProfileUpdateData ) => {
@@ -173,7 +176,6 @@ const ProfileInfoTabContainer = ({
173176 display_name : fullDisplayName ,
174177 username : username || null ,
175178 email : email ?. trim ( ) || null ,
176- custom_prompt : customPrompt || null ,
177179 } ;
178180 try {
179181 const timeoutPromise = new Promise ( ( _ , reject ) =>
@@ -189,30 +191,44 @@ const ProfileInfoTabContainer = ({
189191 onErrorProfileMessage ( 'Speichern dauert zu lange. Bitte versuchen Sie es erneut.' ) ;
190192 }
191193 }
192- } , [
193- displayName ,
194- username ,
195- email ,
196- customPrompt ,
197- user ?. username ,
198- profile ,
199- updateProfile ,
200- onErrorProfileMessage ,
201- ] ) ;
194+ } , [ displayName , username , email , user ?. username , profile , updateProfile , onErrorProfileMessage ] ) ;
195+
196+ const handleCustomPromptChange = useCallback ( ( value : string ) => {
197+ setCustomPrompt ( value ) ;
198+ setIsPromptDirty ( value !== savedCustomPromptRef . current ) ;
199+ } , [ ] ) ;
200+
201+ const handleSaveCustomPrompt = useCallback ( async ( ) => {
202+ if ( ! profile || ! isInitialized . current ) return ;
203+ setIsSavingPrompt ( true ) ;
204+ try {
205+ await updateProfile ( { custom_prompt : customPrompt || null } ) ;
206+ savedCustomPromptRef . current = customPrompt ;
207+ setIsPromptDirty ( false ) ;
208+ setErrorProfile ( '' ) ;
209+ onSuccessMessage ( 'Anweisungen gespeichert' ) ;
210+ } catch ( error ) {
211+ console . error ( 'Custom prompt save error:' , error ) ;
212+ setErrorProfile ( 'Fehler beim Speichern der Anweisungen.' ) ;
213+ onErrorProfileMessage ( 'Fehler beim Speichern der Anweisungen.' ) ;
214+ } finally {
215+ setIsSavingPrompt ( false ) ;
216+ }
217+ } , [ customPrompt , profile , updateProfile , onSuccessMessage , onErrorProfileMessage ] ) ;
202218
203219 const { resetTracking } = useAutosave ( {
204220 saveFunction : async ( ) => {
205221 await stateBasedSave ( ) ;
206222 } ,
207223 formRef : {
208- getValues : ( ) => ( { displayName, username, email, customPrompt } ) ,
224+ getValues : ( ) => ( { displayName, username, email } ) ,
209225 watch : ( callback : ( value : Record < string , unknown > , { name } : { name ?: string } ) => void ) => ( {
210226 unsubscribe : ( ) => { } ,
211227 } ) ,
212228 } ,
213229 enabled : profile && isInitialized . current ,
214230 debounceMs : 2000 ,
215- getFieldsToTrack : ( ) => [ 'displayName' , 'username' , 'email' , 'customPrompt' ] ,
231+ getFieldsToTrack : ( ) => [ 'displayName' , 'username' , 'email' ] ,
216232 onError : ( error : unknown ) => {
217233 console . error ( 'Profile autosave failed:' , error ) ;
218234 setErrorProfile ( 'Automatisches Speichern fehlgeschlagen.' ) ;
@@ -226,7 +242,9 @@ const ProfileInfoTabContainer = ({
226242 setDisplayName ( formFields . displayName ) ;
227243 setUsername ( formFields . username ) ;
228244 setEmail ( formFields . email ) ;
229- setCustomPrompt ( ( profile as { custom_prompt ?: string } ) ?. custom_prompt || '' ) ;
245+ const initialPrompt = ( profile as { custom_prompt ?: string } ) ?. custom_prompt || '' ;
246+ setCustomPrompt ( initialPrompt ) ;
247+ savedCustomPromptRef . current = initialPrompt ;
230248 isInitialized . current = true ;
231249 setTimeout ( ( ) => resetTracking ( ) , 100 ) ;
232250 }
@@ -239,7 +257,7 @@ const ProfileInfoTabContainer = ({
239257 } , 100 ) ;
240258 return ( ) => clearTimeout ( timer ) ;
241259 }
242- } , [ displayName , username , email , customPrompt , resetTracking , profile ] ) ;
260+ } , [ displayName , username , email , resetTracking , profile ] ) ;
243261
244262 useEffect ( ( ) => {
245263 if ( profileUpdateError ) {
@@ -341,7 +359,10 @@ const ProfileInfoTabContainer = ({
341359 username = { username }
342360 setUsername = { setUsername }
343361 customPrompt = { customPrompt }
344- setCustomPrompt = { setCustomPrompt }
362+ setCustomPrompt = { handleCustomPromptChange }
363+ isPromptDirty = { isPromptDirty }
364+ isSavingPrompt = { isSavingPrompt }
365+ onSaveCustomPrompt = { handleSaveCustomPrompt }
345366 errorProfile = { errorProfile }
346367 isErrorProfileQuery = { isErrorProfileQuery }
347368 errorProfileQueryMessage = { errorProfileQuery ?. message }
0 commit comments