|
1 | 1 | import React, { useState, useEffect, useRef } from 'react'; |
2 | 2 | import { Sparkles } from 'lucide-react'; |
| 3 | +import { UseFormReturn } from 'react-hook-form'; |
3 | 4 | import { cn } from '~/utils'; |
4 | 5 | import { useLocalize } from '~/hooks'; |
5 | 6 | import { TooltipAnchor } from '~/components/ui/Tooltip'; |
6 | 7 | import { dataService } from 'librechat-data-provider'; |
7 | 8 |
|
8 | 9 | interface EnhancePromptProps { |
9 | 10 | textAreaRef: React.RefObject<HTMLTextAreaElement>; |
| 11 | + methods: UseFormReturn<{ text: string }>; |
10 | 12 | disabled?: boolean; |
11 | 13 | className?: string; |
12 | 14 | hasText?: boolean; |
13 | 15 | } |
14 | 16 |
|
15 | | -export default function EnhancePrompt({ textAreaRef, disabled = false, className, hasText = false }: EnhancePromptProps) { |
| 17 | +export default function EnhancePrompt({ textAreaRef, methods, disabled = false, className, hasText = false }: EnhancePromptProps) { |
16 | 18 | const localize = useLocalize(); |
17 | 19 | const [isEnhancing, setIsEnhancing] = useState(false); |
18 | 20 | const [isSparklingIntro, setIsSparklingIntro] = useState(false); |
@@ -70,14 +72,19 @@ export default function EnhancePrompt({ textAreaRef, disabled = false, className |
70 | 72 | setIsEnhancing(true); |
71 | 73 | try { |
72 | 74 | const response = await dataService.enhanceMessage(currentText); |
73 | | - if (response.enhancedMessage && textAreaRef.current) { |
74 | | - textAreaRef.current.value = response.enhancedMessage; |
75 | | - // Trigger input event to update form state |
76 | | - const event = new Event('input', { bubbles: true }); |
77 | | - textAreaRef.current.dispatchEvent(event); |
78 | | - // Force resize |
79 | | - textAreaRef.current.style.height = 'auto'; |
80 | | - textAreaRef.current.style.height = textAreaRef.current.scrollHeight + 'px'; |
| 75 | + if (response.enhancedMessage) { |
| 76 | + // Update form state using React Hook Form's setValue |
| 77 | + methods.setValue('text', response.enhancedMessage, { |
| 78 | + shouldValidate: true, |
| 79 | + shouldDirty: true, |
| 80 | + shouldTouch: true |
| 81 | + }); |
| 82 | + |
| 83 | + // Force textarea resize |
| 84 | + if (textAreaRef.current) { |
| 85 | + textAreaRef.current.style.height = 'auto'; |
| 86 | + textAreaRef.current.style.height = textAreaRef.current.scrollHeight + 'px'; |
| 87 | + } |
81 | 88 | } |
82 | 89 | } catch (error) { |
83 | 90 | console.error('Failed to enhance message:', error); |
|
0 commit comments