SCGR commited on
Commit
057a4e0
·
1 Parent(s): 07e4b61

text box completion

Browse files
frontend/src/components/upload/GeneratedTextSection.tsx CHANGED
@@ -1,3 +1,4 @@
 
1
  import { Container, TextArea, Button, IconButton } from '@ifrc-go/ui';
2
  import { DeleteBinLineIcon } from '@ifrc-go/icons';
3
  import styles from '../../pages/UploadPage/UploadPage.module.css';
@@ -31,17 +32,40 @@ export default function GeneratedTextSection({
31
  isPerformanceConfirmed = false,
32
  isSubmitting = false,
33
  }: GeneratedTextSectionProps) {
 
 
 
 
 
 
 
 
 
34
  const handleTextChange = (value: string | undefined) => {
35
- if (value) {
 
 
 
36
  const lines = value.split('\n');
37
  const descIndex = lines.findIndex(line => line.startsWith('Description:'));
38
  const analysisIndex = lines.findIndex(line => line.startsWith('Analysis:'));
39
  const actionsIndex = lines.findIndex(line => line.startsWith('Recommended Actions:'));
40
 
41
  if (descIndex !== -1 && analysisIndex !== -1 && actionsIndex !== -1) {
42
- onDescriptionChange(lines.slice(descIndex + 1, analysisIndex).join('\n').trim());
43
- onAnalysisChange(lines.slice(analysisIndex + 1, actionsIndex).join('\n').trim());
44
- onRecommendedActionsChange(lines.slice(actionsIndex + 1).join('\n').trim());
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
  }
47
  };
@@ -57,7 +81,7 @@ export default function GeneratedTextSection({
57
  <div>
58
  <TextArea
59
  name="generatedContent"
60
- value={`Description:\n${description || 'AI-generated description will appear here...'}\n\nAnalysis:\n${analysis || 'AI-generated analysis will appear here...'}\n\nRecommended Actions:\n${recommendedActions || 'AI-generated recommended actions will appear here...'}`}
61
  onChange={handleTextChange}
62
  rows={12}
63
  placeholder="AI-generated content will appear here..."
 
1
+ import { useState, useEffect } from 'react';
2
  import { Container, TextArea, Button, IconButton } from '@ifrc-go/ui';
3
  import { DeleteBinLineIcon } from '@ifrc-go/icons';
4
  import styles from '../../pages/UploadPage/UploadPage.module.css';
 
32
  isPerformanceConfirmed = false,
33
  isSubmitting = false,
34
  }: GeneratedTextSectionProps) {
35
+ // Local state to maintain the textarea value and cursor position
36
+ const [textareaValue, setTextareaValue] = useState('');
37
+
38
+ // Update local state when props change (e.g., when AI generates new content)
39
+ useEffect(() => {
40
+ const formattedText = `Description:\n${description || 'AI-generated description will appear here...'}\n\nAnalysis:\n${analysis || 'AI-generated analysis will appear here...'}\n\nRecommended Actions:\n${recommendedActions || 'AI-generated recommended actions will appear here...'}`;
41
+ setTextareaValue(formattedText);
42
+ }, [description, analysis, recommendedActions]);
43
+
44
  const handleTextChange = (value: string | undefined) => {
45
+ if (value !== undefined) {
46
+ setTextareaValue(value);
47
+
48
+ // Parse the text and update the individual sections
49
  const lines = value.split('\n');
50
  const descIndex = lines.findIndex(line => line.startsWith('Description:'));
51
  const analysisIndex = lines.findIndex(line => line.startsWith('Analysis:'));
52
  const actionsIndex = lines.findIndex(line => line.startsWith('Recommended Actions:'));
53
 
54
  if (descIndex !== -1 && analysisIndex !== -1 && actionsIndex !== -1) {
55
+ const newDescription = lines.slice(descIndex + 1, analysisIndex).join('\n').trim();
56
+ const newAnalysis = lines.slice(analysisIndex + 1, actionsIndex).join('\n').trim();
57
+ const newRecommendedActions = lines.slice(actionsIndex + 1).join('\n').trim();
58
+
59
+ // Update parent state only if values have changed
60
+ if (newDescription !== description) {
61
+ onDescriptionChange(newDescription);
62
+ }
63
+ if (newAnalysis !== analysis) {
64
+ onAnalysisChange(newAnalysis);
65
+ }
66
+ if (newRecommendedActions !== recommendedActions) {
67
+ onRecommendedActionsChange(newRecommendedActions);
68
+ }
69
  }
70
  }
71
  };
 
81
  <div>
82
  <TextArea
83
  name="generatedContent"
84
+ value={textareaValue}
85
  onChange={handleTextChange}
86
  rows={12}
87
  placeholder="AI-generated content will appear here..."